Solved Enable disable button in a form based on a value of a text (1 Viewer)

Leo_Polla_Psemata

Registered User.
Local time
Today, 12:46
Joined
Mar 24, 2014
Messages
364
Hi there
I want to Enable or dis-enable a button based on the value of a text which is in the same form, i have typed the below however I am not sure where should i paste these five lines

Code:
If COUNAL.Value <> "" Then
    CommandButton2.Enabled = False
Else
    CommandButton2.Enabled = True
End If


 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:46
Joined
Feb 19, 2002
Messages
43,257
Code:
If Me.COUNAL & "" <> "" Then
    Me.cmdSomeNiceName.Enabled = False
Else
    Me.cmdSomeNiceName.Enabled = True
End If

The code must be executed from two events.
1. The Form's Current event
2. the Me.COUNAL's AfterUpdate event

OR ad conditional formatting to the command line that references the value in the textbox.
 

MarkK

bit cruncher
Local time
Today, 12:46
Joined
Mar 17, 2004
Messages
8,181
Note that if you have a boolean value A, and you need to set another boolean value B that varies directly with A, you don't need an If block. Rather, you can set B directly from A, for example...
Code:
Me.cmdButton.Enabled = Me.txtValue & "" = ""
In a single line of code, the button is enabled if the textbox is null or zero length, AND the button is disabled if there is data in the control. If you like leaner code, this might make sense for you going forward.
Code:
If IsTrue = True Then
    Result = True
Else
    Result = False
End If
...can be replaced with...
Code:
Result = IsTrue
More of the same pattern...
Code:
If IsTrue = False Then
    Result = True
Else
    Result = False
End If
...can be replaced with...
Code:
Result = Not IsTrue
 

Leo_Polla_Psemata

Registered User.
Local time
Today, 12:46
Joined
Mar 24, 2014
Messages
364
Hi there
When I used a control in the form like below, the enable/diasable function didn't work, when i changed the control with dlookup, it worked. What a crazy strange world we are living in...
=[Onesubform].[Form]![Onecontrol]
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:46
Joined
Feb 19, 2002
Messages
43,257
the enable/diasable function didn't work,
You need to post your code including the header so we can see what event and control it is in. dLookup() doesn't seem to make sense in this context.
 

Leo_Polla_Psemata

Registered User.
Local time
Today, 12:46
Joined
Mar 24, 2014
Messages
364
You need to post your code including the header so we can see what event and control it is in. dLookup() doesn't seem to make sense in this context.
Hi,
on my form, there is a button that runs an append query. Then, my table is updated.
The hazard is that i may click button for a second time and duplicate the records in the table.
To avoid this, i have added a text box on my form that counts the records in that table.
If text box is >1 then button is disabled, else, button = enabled.
To count the records i can use in this text box the
1. Dlookup function , or
2. count the subform records , =[Onesubform].[Form]![Onecontrol]

In both cases 1 or 2 , the text box outlines the correct number of records. say 59, which is >1 and therefore btn should turn to disable
HOWEVER in option 2, the update button doesn't turn to disable. It works only in option one , only by using dlookup

 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:46
Joined
Feb 19, 2002
Messages
43,257
I suggested a change to your code. Did you make it? Your code will NOT work if the control is null. It is hard enough for us to debug code without the database but it is impossible without seeing it at all.
 

Users who are viewing this thread

Top Bottom