diable command button based on field value

B

barrynichols

I have a command button which I would like to disable based on the
following rules:

If A = 1 then button is disabled
If B = 2 then button is enabled
 
K

Klatuu

Where you put the code depends on how the form works. If this needs to be
checked for each record, use the form current event:

If A = 1 Then
Me.Button.Enabled = False
ElseIf B = 2 Then
Me.Button.Enabled = True
End If

But, what if A is not 1 and B is not 2?
 
B

barrynichols

Where you put the code depends on how the form works.  If this needs to be
checked for each record, use the form current event:

    If A = 1 Then
        Me.Button.Enabled = False
    ElseIf B = 2 Then
         Me.Button.Enabled = True
    End If

But, what if A is not 1 and B is not 2?

--
Dave Hargis, Microsoft Access MVP






- Show quoted text -

Dave,

Thanks for this.

It should have read

If A=1 then..
A=2 then....

Sorry for the confusion.

I've got the code working, however, is there a way to get it to work
"live", i.e. without having to change records and then go back to the
one I was working on for the button to change? Saving the current
record doesn't seem to help.

Thanks
 
K

Klatuu

If you put the code in the Form Current event, it should take care of
resetting the button for each record. If that is not what you are trying to
do, then I don't understand the question.
 
B

barrynichols

If you put the code in the Form Current event, it should take care of
resetting the button for each record.  If that is not what you are trying to
do, then I don't understand the question.
--
Dave Hargis, Microsoft Access MVP










- Show quoted text -

It does reset the button for each record, however the change to the
button is not instant.

So the default setting for the field is A=1 thus button is disabled.

When I change to A=2, the button is not enabled until I go to another
record and then return to the same record. (i.e. save and refresh)
 
L

Linq Adams via AccessMonster.com

You need your code in the AfterUpdate event of your textbox A for the change
to take place immediately. You still need it, of course, in the Form_Current
event to persist this when returning to records.
 
K

Klatuu

Linq is correct. I would suggest you put the code in a sub in your form
module and call it from the After Update event of A and from the Form Current
event. That way you only have one place to make changes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top