Detect Whether a TextBox Has Become Blank

whdyck

Registered User.
Local time
Today, 14:32
Joined
Aug 8, 2011
Messages
169
I'm using Access 2003.

Is there a way to detect whether a user has begun typing in a textbox? Or whether, after typing something into it, they have erased what they have typed?

I have a Search form with a criteria value of type text that the user enters into a textbox. I also have other search criteria on the form (selected via combobox) that are mutually exclusive to the textbox criteria. (It wouldn't make sense to enter both types of criteria, and the form makes it clear that it's an either/or situation.) To that end, if the user enters text into the textbox and tabs out of the field, I disable the combobox fields to make it clear.

But I'd prefer to disable the combobox controls as soon as the user types the first character. Likewise, if they erase the data from the textbox I'd like to re-enable the combobox controls. The key is that I don't want to require that they tab out of the control to change the Enabled property of the combobox controls.

Can this be done?

I've looked at some of the events associated with a textbox, but I haven't found anything that works.

Thanks for any help you can give.

Wayne
 
You can use your textbox's OnChange event and test the lenght (Len()) of text typed.

ex.

Code:
Private Sub txtSearch_Change()
If Len(Me.txtSearch.[COLOR=red]Text[/COLOR] & "") > 0 Then
    Me.cboSearch.Enabled = False
Else
    Me.cboSearch.Enabled = True
End If
End Sub

Note the .text property it is vital to use in this event since the textbox's .value property isen't available until AfterUpdate fires.

JR
 
You can use your textbox's OnChange event and test the lenght (Len()) of text typed.

ex.

Code:
Private Sub txtSearch_Change()
If Len(Me.txtSearch.[COLOR=red]Text[/COLOR] & "") > 0 Then
    Me.cboSearch.Enabled = False
Else
    Me.cboSearch.Enabled = True
End If
End Sub

Note the .text property it is vital to use in this event since the textbox's .value property isen't available until AfterUpdate fires.

JR

This works great!

And I appreciate your pointing out why the .Text property is so important. (I prefer to understand how the code works.)

Thanks! Have a great day!

Wayne
 
I have a Search form with a criteria value of type text that the user enters into a textbox. I also have other search criteria on the form (selected via combobox) that are mutually exclusive to the textbox criteria. (It wouldn't make sense to enter both types of criteria, and the form makes it clear that it's an either/or situation.) To that end, if the user enters text into the textbox and tabs out of the field, I disable the combobox fields to make it clear.

Because you are indicating that "both are mutually exclusive" you would need to insure that if the user enteres anything in the textbox you would not only disable the combo box but you would need to make sure that there was not value still seleted in the combo box and the opposite is true for the text box.

You might want code in the On Change event of your text box so the code for that event would look like:
Code:
Private Sub txtCriteria_Change()
If Me.txtCriteria.Text <> "" Then
    With Me.cboCriteria
        .Value = Null
        .Enabled = False
    End With
Else
    Me.cboCriteria.Enabled = True
End If
End Sub
Then you would also have code in the After Update event of the combo box that would look like:
Code:
Private Sub cboCriteria_AfterUpdate()
If Me.cboCriteria <> "" Then
    With Me.txtCriteria
        .Value = Null
        .Enabled = False
    End With
Else
    Me.txtCriteria.Enabled = True
End If
End Sub

Using code in both places will insure that your user only has criteria in of place or the other and cannot enter a value in both.
 
Because you are indicating that "both are mutually exclusive" you would need to insure that if the user enteres anything in the textbox you would not only disable the combo box but you would need to make sure that there was not value still seleted in the combo box and the opposite is true for the text box.

Using code in both places will insure that your user only has criteria in of place or the other and cannot enter a value in both.

You make a good point. Actually, I have it coded so that if the user selects anything from the combobox controls, I disable the textbox until they first blank out the combobox. So I think I'm OK there.

Thanks for the reminder. I'm gonna take another look at my code to ensure that these cases are all covered.

Wayne
 

Users who are viewing this thread

Back
Top Bottom