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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing text box in the event of validation error

Status
Not open for further replies.

jethro11

Programmer
Sep 11, 2002
18
US
Hi,
I've set up my form so that the text box entries are validated before update and if the entry fails the validation test, an error message comes up. Is there a way to clear this entry from the text box and ensure that the record isn't saved if the validation test fails? For eg
My form has fields for start date/time and finish date/time to record downtimes. After the finish date/time is keyed in, if it is less than the start date/time I have an error message come up. How can I get the finish date field cleared of the wrong entry and ensure the record isn't saved? Thanks!
 
Hi

Assuming you are executing this code in teh Before Update event of the form see the CANCEL parameter in the event call?, set it to True, this will cancel the update

To set the cursor back on the offending field

MyField.SetFocus

(using your field name of course)

To 'wipe' the field

MyField = ""

Hope this helps Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Ken,
Thanks for your prompt reply,I tried to do what you said but I still get an error. To clarify the problem: I have a field called Stop_Date which is when the machine is stopped for some reason and Start_Date which is when the machine is restarted after the downtime. I still get an error message when the SetFocus line of code is executed as follows - Run time error:2108 You must save the field before you execute the GoToControl action the GoToControl method or the SetFocus method.You tried to move the focus to another control using the GoToControl action, the GoToControl method, or the SetFocus method.

Here is my code:

Private Sub Start_Date_BeforeUpdate(Cancel As Integer)

If Start_Date < Stop_Date Then
If MsgBox(&quot;Start Date can't be earlier than Stop Date, please re-enter data! Note, if you answer no, the record will not be saved!&quot;, vbYesNo) = vbYes Then
Cancel = True
Start_Date.SetFocus (Error occurs at this line)
Start_Date = &quot;&quot;
Else
Me.Undo
End If
End If

End Sub
 
Hi

Take out the setfocus line, I did expailn that I assumed you where executing the code in the before update event of the FORM, not the Control, and as the error message says

&quot;Run time error:2108 You must save the field before you execute the GoToControl action the GoToControl method or the SetFocus method.You tried to move the focus to another control using the GoToControl action, the GoToControl method, or the SetFocus method.&quot;

you cannot shift focus at that time Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top