Command Button Enabled/Disabled Based on Text in Text Box

D

DoveArrow

I'm trying to create a form where, if there is text in a text box, a
pair of command buttons will be enabled. I have figured out how to
make this work if the text box has been updated, but I can't figure
out how to make it work if there's already text in the box when the
form opens. If it helps, here's the code I've written to make the
buttons enable after text has been entered into the text box.

Private Sub Class1_Change()
If IsNull(Class1) Then
Class1LvlAdd.Enabled = False
Class1LvlRemove.Enabled = False
Else
Class1LvlAdd.Enabled = True
Class1LvlRemove.Enabled = True
End If
End Sub

Note: Right now, I'm taking my first baby steps into using Visual
Basic, so if the above looks laughably amateur, please be gentle.
 
S

Steve

Put the following code in the Current event of the form:

If Is Not Null [Class1] Then
Me1Class1LvlAdd.Enabled = True
Me!Class1LvlRemove.Enabled = True
End If


PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
 
D

DoveArrow

Put the following code in the Current event of the form:

If Is Not Null [Class1] Then
Me1Class1LvlAdd.Enabled = True
Me!Class1LvlRemove.Enabled = True
End If

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)




I'm trying to create a form where, if there is text in a text box, a
pair of command buttons will be enabled. I have figured out how to
make this work if the text box has been updated, but I can't figure
out how to make it work if there's already text in the box when the
form opens. If it helps, here's the code I've written to make the
buttons enable after text has been entered into the text box.
Private Sub Class1_Change()
If IsNull(Class1) Then
Class1LvlAdd.Enabled = False
Class1LvlRemove.Enabled = False
Else
Class1LvlAdd.Enabled = True
Class1LvlRemove.Enabled = True
End If
End Sub
Note: Right now, I'm taking my first baby steps into using Visual
Basic, so if the above looks laughably amateur, please be gentle.- Hide quoted text -

- Show quoted text -

Fantastic! That works great! Thank you!
 
S

Steve

A couple of comments about your code ---
1. Class1 is the name of a control on your form and should be enclosed in
square brackets not parantheses.
2. When referencing a control on a form, the control's name should be
proceeded by Me!
Me!Class1
Me!Class1LvlAdd
Me!Class1LvlRemove

(Buttons are controls!)

3. Indent your code lines between If and Else and Else and End If for
easier reading. Do the same for loops.

PS Apparently you caught my typo in Me1Class1LvlAdd.Enabled = True. The 1
after Me should be !.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)



DoveArrow said:
Put the following code in the Current event of the form:

If Is Not Null [Class1] Then
Me1Class1LvlAdd.Enabled = True
Me!Class1LvlRemove.Enabled = True
End If

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)




I'm trying to create a form where, if there is text in a text box, a
pair of command buttons will be enabled. I have figured out how to
make this work if the text box has been updated, but I can't figure
out how to make it work if there's already text in the box when the
form opens. If it helps, here's the code I've written to make the
buttons enable after text has been entered into the text box.
Private Sub Class1_Change()
If IsNull(Class1) Then
Class1LvlAdd.Enabled = False
Class1LvlRemove.Enabled = False
Else
Class1LvlAdd.Enabled = True
Class1LvlRemove.Enabled = True
End If
End Sub
Note: Right now, I'm taking my first baby steps into using Visual
Basic, so if the above looks laughably amateur, please be gentle.- Hide
quoted text -

- Show quoted text -

Fantastic! That works great! Thank you!
 
G

Guest

In addition to Steve's comments, you might want to consider using a standard
naming convention for the controls on your forms. The default names are
either totally without meaning (text1) or match with the field name. It is
far easier when you are looking at code to determine whether you are dealing
with a field or a control if you use a naming convention. Check out this
one: http://www.mvps.org/access/general/gen0012.htm

another note. Although some people believe that using the If Then to do
what you are doing makes sense, I prefer another technique. I find this
technique to be easily readable, and shorter.

Private Sub txt_Class1_Change()

Dim bEnabled as boolean

bEnabled = NOT ISNULL(me.txt_Class1)
Me.cmd_Class1LvlAdd.Enabled = bEnabled
me.cmd_Class1LvlRemove.Enabled = bEnabled

End sub

HTH
Dale

--
Email address is not valid.
Please reply to newsgroup only.


Steve said:
A couple of comments about your code ---
1. Class1 is the name of a control on your form and should be enclosed in
square brackets not parantheses.
2. When referencing a control on a form, the control's name should be
proceeded by Me!
Me!Class1
Me!Class1LvlAdd
Me!Class1LvlRemove

(Buttons are controls!)

3. Indent your code lines between If and Else and Else and End If for
easier reading. Do the same for loops.

PS Apparently you caught my typo in Me1Class1LvlAdd.Enabled = True. The 1
after Me should be !.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)



DoveArrow said:
Put the following code in the Current event of the form:

If Is Not Null [Class1] Then
Me1Class1LvlAdd.Enabled = True
Me!Class1LvlRemove.Enabled = True
End If

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)





I'm trying to create a form where, if there is text in a text box, a
pair of command buttons will be enabled. I have figured out how to
make this work if the text box has been updated, but I can't figure
out how to make it work if there's already text in the box when the
form opens. If it helps, here's the code I've written to make the
buttons enable after text has been entered into the text box.

Private Sub Class1_Change()
If IsNull(Class1) Then
Class1LvlAdd.Enabled = False
Class1LvlRemove.Enabled = False
Else
Class1LvlAdd.Enabled = True
Class1LvlRemove.Enabled = True
End If
End Sub

Note: Right now, I'm taking my first baby steps into using Visual
Basic, so if the above looks laughably amateur, please be gentle.- Hide
quoted text -

- Show quoted text -

Fantastic! That works great! Thank you!
 
D

DoveArrow

In addition to Steve's comments, you might want to consider using a standard
naming convention for the controls on your forms. The default names are
either totally without meaning (text1) or match with the field name. It is
far easier when you are looking at code to determine whether you are dealing
with a field or a control if you use a naming convention. Check out this
one: http://www.mvps.org/access/general/gen0012.htm

another note. Although some people believe that using the If Then to do
what you are doing makes sense, I prefer another technique. I find this
technique to be easily readable, and shorter.

Private Sub txt_Class1_Change()

Dim bEnabled as boolean

bEnabled = NOT ISNULL(me.txt_Class1)
Me.cmd_Class1LvlAdd.Enabled = bEnabled
me.cmd_Class1LvlRemove.Enabled = bEnabled

End sub

HTH
Dale

--
Email address is not valid.
Please reply to newsgroup only.



Steve said:
A couple of comments about your code ---
1. Class1 is the name of a control on your form and should be enclosed in
square brackets not parantheses.
2. When referencing a control on a form, the control's name should be
proceeded by Me!
Me!Class1
Me!Class1LvlAdd
Me!Class1LvlRemove
(Buttons are controls!)
3. Indent your code lines between If and Else and Else and End If for
easier reading. Do the same for loops.
PS Apparently you caught my typo in Me1Class1LvlAdd.Enabled = True. The 1
after Me should be !.
PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
DoveArrow said:
Put the following code in the Current event of the form:
If Is Not Null [Class1] Then
Me1Class1LvlAdd.Enabled = True
Me!Class1LvlRemove.Enabled = True
End If
PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)

I'm trying to create a form where, if there is text in a text box, a
pair of command buttons will be enabled. I have figured out how to
make this work if the text box has been updated, but I can't figure
out how to make it work if there's already text in the box when the
form opens. If it helps, here's the code I've written to make the
buttons enable after text has been entered into the text box.
Private Sub Class1_Change()
If IsNull(Class1) Then
Class1LvlAdd.Enabled = False
Class1LvlRemove.Enabled = False
Else
Class1LvlAdd.Enabled = True
Class1LvlRemove.Enabled = True
End If
End Sub
Note: Right now, I'm taking my first baby steps into using Visual
Basic, so if the above looks laughably amateur, please be gentle.- Hide
quoted text -
- Show quoted text -
Fantastic! That works great! Thank you!- Hide quoted text -

- Show quoted text -

I do actually use a naming convention. If it's a text box with a
Control Source, I name the text box after the field it's linked to, if
it's a label, I just use the default text, and if it's calculating
information from several text boxes, I use a name like LvlTotal, or
something.

As far as using brackets, Me!, and other stuff like that, maybe it's
something I'm doing wrong, but I've found that a lot of times it
doesn't work correctly when I use it. However, if I take them out, it
will. The example you gave me is a perfect example of that. It didn't
work in my report when I just copied it in (yes, I caught the typo),
so I rewrote it the 'incorrect way', and it worked fine. Any thoughts
on why that would be?
 
S

Steve

Open your form in design view and go to the code window. Click on Tools -
References. See if you have Microsoft ADO XXX checked. If you do, uncheck
it. See if you have Microsoft DAO XXX checked. If you don't, scroll down the
list and find the one with the highest version for XXX. Check it. Close the
References dialog. Open it again and you should now see Microsoft DAO XXX in
the list and it should be checked.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)




DoveArrow said:
In addition to Steve's comments, you might want to consider using a
standard
naming convention for the controls on your forms. The default names are
either totally without meaning (text1) or match with the field name. It
is
far easier when you are looking at code to determine whether you are
dealing
with a field or a control if you use a naming convention. Check out this
one: http://www.mvps.org/access/general/gen0012.htm

another note. Although some people believe that using the If Then to do
what you are doing makes sense, I prefer another technique. I find this
technique to be easily readable, and shorter.

Private Sub txt_Class1_Change()

Dim bEnabled as boolean

bEnabled = NOT ISNULL(me.txt_Class1)
Me.cmd_Class1LvlAdd.Enabled = bEnabled
me.cmd_Class1LvlRemove.Enabled = bEnabled

End sub

HTH
Dale

--
Email address is not valid.
Please reply to newsgroup only.



Steve said:
A couple of comments about your code ---
1. Class1 is the name of a control on your form and should be enclosed
in
square brackets not parantheses.
2. When referencing a control on a form, the control's name should be
proceeded by Me!
Me!Class1
Me!Class1LvlAdd
Me!Class1LvlRemove
(Buttons are controls!)
3. Indent your code lines between If and Else and Else and End If for
easier reading. Do the same for loops.
PS Apparently you caught my typo in Me1Class1LvlAdd.Enabled = True.
The 1
after Me should be !.
PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
Put the following code in the Current event of the form:
If Is Not Null [Class1] Then
Me1Class1LvlAdd.Enabled = True
Me!Class1LvlRemove.Enabled = True
End If
PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
I'm trying to create a form where, if there is text in a text box,
a
pair of command buttons will be enabled. I have figured out how to
make this work if the text box has been updated, but I can't
figure
out how to make it work if there's already text in the box when
the
form opens. If it helps, here's the code I've written to make the
buttons enable after text has been entered into the text box.
Private Sub Class1_Change()
If IsNull(Class1) Then
Class1LvlAdd.Enabled = False
Class1LvlRemove.Enabled = False
Else
Class1LvlAdd.Enabled = True
Class1LvlRemove.Enabled = True
End If
End Sub
Note: Right now, I'm taking my first baby steps into using Visual
Basic, so if the above looks laughably amateur, please be gentle.-
Hide
quoted text -
- Show quoted text -
Fantastic! That works great! Thank you!- Hide quoted text -

- Show quoted text -

I do actually use a naming convention. If it's a text box with a
Control Source, I name the text box after the field it's linked to, if
it's a label, I just use the default text, and if it's calculating
information from several text boxes, I use a name like LvlTotal, or
something.

As far as using brackets, Me!, and other stuff like that, maybe it's
something I'm doing wrong, but I've found that a lot of times it
doesn't work correctly when I use it. However, if I take them out, it
will. The example you gave me is a perfect example of that. It didn't
work in my report when I just copied it in (yes, I caught the typo),
so I rewrote it the 'incorrect way', and it worked fine. Any thoughts
on why that would be?
 

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