Link to home
Start Free TrialLog in
Avatar of Starbuck67
Starbuck67Flag for United States of America

asked on

access 2007 need to clear unbound text box

I have an unbound text box in a form that I use to go to a particular record. What I do is enter a part of a person's last name in the text box and then press a button next to it which reads the text in the text box and then takes me to the first record that matches the partial text string. What I want to do is the next time I go to that unbound text box to enter a new text string I would like to have the text box cleared first.
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

In the textbox's OnEnter event, add the following:
Me.NameOfTextbox = Null

Ron
You could put the following code into the "Got Focus" event, changing "Text1" for the name of the text box you want to clear.  Then, whenever you enter that text box, it will clear whatever is in there:

Me.Text1.Value = ""

Open in new window

Avatar of Starbuck67

ASKER

IrogSinta

Do you mean that I should type "Me.NameOfTextbox = Null" in the OnEnter Box on the OnEnter line?

Robert Sherman

Again do I type your suggestion on the GotFocus line?
IrogSinta

I did as you suggested (see attachments) and ended up with an error
Access-2007-2-8-15---2.jpg
Access-2007-2-8-15---3.jpg
When you click on the On Enter property, you will see two buttons appear to the right.  One with a down arrow and the other with ellipses.  You could do one of two things.
Click on the down arrow, select [Event Procedure], then click on the ellipses.
Click on the ellipses, select Code Builder
In the code window that opens up, add the code I suggested.  It should look like this:
Private Sub FindLname_Enter()
    Me.FindLname = Null
    
End Sub

Open in new window

Ron
IrogSinta

I entered the code as shown on the attached image. When I click in the unbound text box, the existing text does not go away.
Access-2007-2-8-15---4.jpg
Does the On Enter property of the textbox say [Event Procedure]?
Yes
The problem is that you use the method Screen.PreviousControl.Setfocus which sets the focus to the FindLname textbox.  Since the textbox already has the focus, the OnEnter event does not fire.

Another issue I see is your use of DoCmd.RunCommand accmdFind.  This will not find the name you have in your textbox.  All this command does is bring up the Find Dialog Box (similar to pressing CTRL-F on your keyboard).  You probably meant to use DoCmd.FindRecord instead.  However, take note that FindRecord set to acEntire will stop as soon as it finds the string inside your textbox, so that textbox needs to be cleared first before doing the find.  Your whole code could simply be replaced with the following:
Private Sub Find_Click()
    Dim strFindThis As String
    
    strFindThis = Me.FindLname
    Me.FindLname = Null
    DoCmd.FindRecord strFindThis, acAnywhere, False, acSearchAll, True, acAll, True
    
End Sub

Open in new window


Now if you still want to have the textbox show your original find string and then clear out when you click on it, you can modify your code to this:
Private Sub Find_Click()
    Dim strFindThis As String
    
    strFindThis = Me.FindLname
    Me.FindLname = Null
    DoCmd.FindRecord strFindThis, acAnywhere, False, acSearchAll, True, acAll, True
    Me.FindLname = strFindThis
    
End Sub

Private Sub Lname_Enter()
    Me.FindLname = Null
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Starbuck67
Starbuck67
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial