Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button enabled if text in textbox. 2

Status
Not open for further replies.

xollef

Technical User
Apr 23, 2002
30
0
0
Hello

I have a form and on it some buttons and button, the form button is disabled.

Because I have a textbox where you should fill in a value and then save that value before you can open the form. So when pressing the save button it saves the value and then enables the form button.

But then when opening the same record again where the value is allready filled in you anyway have to press save before you can open the form.

So how can I make the form button enabled if there is already text in the required field?

Should I use the on open event on the form to have an if code that checkes if there is text, if there is text it will enable the button and if not you will have to type text in it, press save to enable it.

If that is the case then how should the if function look like. I can't VB that vell :(

Could someone tell me if I am going the right direction and then maybe supply the if statement?

Thanks
 
Sounds to me like thatb is exactly what you want to do....

In the Form OnOpen event...

If IsNull(Me![nameoftextbox]) Then
Me![nameofbutton].Enabled = False
End If

In the AfterUpdate Event of the Text Box....

Me![nameofbutton].Enabled = True

Should set you straight. PLease let me know if you have any problems. It's not important that someone else can do in one step what it took you ten to do...the important thing is that you found a solution. [spin]

Robert L. Johnson III, A+, Network+, MCP
Access Developer/Programmer
 
Hello

Thanks for the answer, but (there is always a but : ) it doesn't work.

Ok to explain: I have assigned it to the on open event of the form. And it works if I leave out the if statement. Meaning that if I only enable it:

Me![nameofbutton].Enabled = True

it works but as you can figure it out it won't do.

But when I add the:

If IsNull (Me![nameoftextbox]) Then
Me![nameofbutton].Enabled = False
End If

If the If function is there it doesn't enable the button???

Don't know why it seems it should work but it doesn't enable. (There is text in the box)



 
xollef,

Don't have a clue.....I just tried it myself and you are right. It should work but it doesn't...will have to check my logic later.

But, here is the but for you!, I found a solution that does work. It uses the OnCurrent property and check the record at load. Here is the solution:

Take out the code you already have in the OnOpen event. You won't need it. Then set the enabled property of your button to no. This will make the button disabled by default. Place the follownig code in the Forms OnCurrent event. Be sure to change the field names to whatever you have....

If Not IsNull(Me![Text1]) Then
Me![Command0].Enabled = True
Else
Me![Command0].Enabled = False
End If

I made this work every time. I use both bound and unbound fields, with and without data and every time there was dat the button was available and when there was not the button was disable.

Please let me know fi you have any problems. It's not important that someone else can do in one step what it took you ten to do...the important thing is that you found a solution. [spin]

Robert L. Johnson III, A+, Network+, MCP
Access Developer/Programmer
 
Hello again.

Thank you again. No but this time it works BIG THANKS!

My solution became this:

This event that I put on the Oncurrent event works, Then I got an idea. As you can see there is a small difference between the IF statements in the events.

Onopen event:
If IsNull (Me![nameoftextbox]) Then
Me![nameofbutton].Enabled = False
End If

OnCurrent event:
If Not IsNull(Me![Text1]) Then
Me![Command0].Enabled = True
Else
Me![Command0].Enabled = False
End If

So I tested to change the OnOpen event to this:

If Not Isnull (Me![Text1]) Then
Me![nameofbutton].Enabled = True
End If

And it also worked.

Anyway would not have figured this out (this week anyway) without your help Thanks again.
 
A couple of other ideas:

You might try using the OnChange event of the text box instead of the AfterUpdate event. Your button will enable when the user types text in the text box instead of forcing them to exit the text box first. Place the following code in the OnChange event of the text box to do this:

If Nz(Nameoftextbox.Text) = "" Then
nameofbutton.Enabled = False
Else
nameofbutton.Enabled = True
End If

Note also that this will set the button back to disabled if the user deletes all the text in the text box.
_____

I noticed in Robert's first solution, using the OnOpen event, that the If structure didn't have code to set button.enabled to True. If your button is already disabled when you enter the form, it wouldn't enable on open. The following code would solve that issue.

If Nz(Me![nameoftextbox]) = "" Then
Me![nameofbutton].Enabled = False
Else
Me![nameofbutton].Enabled = True
End If

Note that the OnOpen event only runs when you open the form. It doesn't run when you change the record on a form. That is most likely why moving the code to the OnCurrent event solved the other issue.

___

You wrote:

[But when I add the:

If IsNull (Me![nameoftextbox]) Then
Me![nameofbutton].Enabled = False
End If

If the If function is there it doesn't enable the button???]

This is because the If structure doesn't have code to enable the button. It only disables it if nothing is in the text box. It doesn't enable the button if something is in the text box. Please read my comments above. Robert's second suggestion, using the OnCurrent event, both enables and disables the button appropriately. And since he suggested moving the code from the OnOpen to OnCurrent event, it solved both problems at once.

dz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top