Validating data

  • Thread starter Thread starter Greg
  • Start date Start date
G

Greg

Guest
I have created a form with a number of fields, all of which require data entry. All but a few of the initial fields require the user to enter from a short validated drop down list. All fields can be tabbed into. Each field has a macro that advises what data can be entered in that field if the wrong data is entered. In the event that the user attempts to leave the field without entering data, I wish the macro to run and return the user to the empty data field.

If the answer is to use the expression builder or the code builder, please list it as such, as I am still new enought that I may not pick up on which one is to be used.

Thanks in advance for your help.
 
sorry i don't have a specific answer for you as Im still new to this to but maybe this can help you get started.
If it were one field this is what I would do
If field name = "" Or IsNull(fieldname) Or IsEmpty(fieldname) Then
fieldname.setfocus

or something along those lines so that when it reads the field is empty it sets the focus back to that field, you said you have a bunch of fields so you'll need to write a short function to do this.Im sure someone else will have a better solution but I hope this helps you along the way for now

Rpb
 
You can also write this code using the code builder for the Exit event for each field.
If IsNull(FieldName) Then
Cancel = True
End If
 
Since you cannot force the user to enter each field, putting code in the exit event or BeforeUpdate event of a control will not solve your problem. You need to collect all the error checking code and place it in the BeforeUpdate event of the form. This is the last event fired before Access actually saves the record to the table. Use the following as a model. Replace "Field?IsValid with your own edit checks.

If Field1IsValid Then
Else
MsgBox "Field 1 is invalid, update is cancelled", vbOKOnly
Me.Field1.SetFocus = True
Cancel = True
Exit Sub
End If

If Field2IsValid Then
Else
MsgBox "Field 2 is invalid, update is cancelled", vbOKOnly
Me.Field2.SetFocus = True
Cancel = True
Exit Sub
End If

[This message has been edited by Pat Hartman (edited 06-06-2000).]
 
i tried to do this too, but i get a error message: Expected function or variable;

This is my coding:

If [debit amount] = 0 Then
MsgBox "You must enter the amount of the transaction.", 0, "Can not be saved. Missing Data"
Me.[debit amount].SetFocus = True

I tried to do DoCmd.GoToControl "debit amount"

it skips to the next record and goes to "debit amount"
 
i figured it out.

i use
DoCmd.GoToControl "[debit amount]"
cancel = true

it stays in the same record.
 
The correct syntax is debit_amount.SetFocus you don't need =True
HTH
 

Users who are viewing this thread

Back
Top Bottom