Clearing a textbox after inputting a wrong data

odrap

Registered User.
Local time
Today, 23:30
Joined
Dec 16, 2008
Messages
156
I my form i have a textbox into which the user can enter a value that will serve as criteria in a query.
In the beforeupdate of this textbox there is a check about the validity of the input. If this input is wrong, a message tells the user what's wrong .
In such a case i like to cancel the event but in the same time clearing the textbox.
Canceling isn't a problem, but automatically clearing the wrong input seems not to be so evident.
How can i do that ?
 
Try Undo right after cancelling..
Code:
Private Sub controlName_BeforeUpdate(Cancel As Integer)
    If yourCondition = True Then
        MsgBox "Wrong input. Try again"
        Cancel = True
        Me.controlName[B].Undo[/B]
    End If
End Sub
 
Thank You for the help, but ....

I followed your advice, see code beneath, but that doesn't work as i want it to work.
After this event, the whole input in the textbox remains and is selected (black background).
I work with Access 2007

Private Sub txtJaartal_BeforeUpdate(Cancel As Integer)
If Len(txtJaartal) > 2 Then
Box ("Het jaartal mag niet meer dan twee getallen bevatten")
Cancel = True
Me.txtJaartal.Undo
End If
End Sub
 
Hmmm.. That's strange.. Is it possible to upload a stripped DB?

How to Upload a Stripped DB.

To create a Sample DB (to be uploaded for other users to examine); please follow the steps..

1. Create a backup of the file, before you proceed..
2. Delete all Forms/Queries/Reports that are not in Question (except the ones that are inter-related)
3. Delete auxiliary tables (that are hanging loose with no relationships).
4. If your table has 100,000 records, delete 99,990 records.
5. Replace the sensitive information like Telephone numbers/email with simple UPDATE queries.
6. Perform a 'Compact & Repair' it would have brought the Size down to measly KBs..
7. (If your Post count is less than 10 ZIP the file and) Upload it..

Finally, please include instructions of which Form/Query/Code we need to look at. The preferred Access version would be A2003-A2007 (.mdb files)
 
Just an unbound form with a textbox and the code in beforeupdate of the textbox.
When you fill in more than two numbers of letters, you get the message and after that you see the wrong input selected in the textbox
 

Attachments

So far I know the Undo function only works if the control is bound to a field in a table. And you can't change the value in the control in the before update event, so you must move test to the after update event and set the control's value to "".

Code:
Private Sub txtJaartal_AfterUpdate()
 
 If Len(txtJaartal) > 2 Then
     MsgBox ("Het jaartal mag niet meer dan twee getallen bevatten")
     Me.txtJaartal = ""
  End If

End Sub
 

Users who are viewing this thread

Back
Top Bottom