Validate Text field in a form.

G

Guest

Hi,

I have a form to input data into my database table. I want to make sure
that my users input three of the most important field in my form. They are
Part #, Description and Country fields. I want to put a code in the form so
that when users failed to input one or more of the required field it will not
allow them to exit the form or go to the next empty form to input more data.
So a pop-up screen is required to notify my user that they are required to
input data for those empty fields. Thanks.
 
J

John Vinson

Hi,

I have a form to input data into my database table. I want to make sure
that my users input three of the most important field in my form. They are
Part #, Description and Country fields. I want to put a code in the form so
that when users failed to input one or more of the required field it will not
allow them to exit the form or go to the next empty form to input more data.
So a pop-up screen is required to notify my user that they are required to
input data for those empty fields. Thanks.

Use the Form's BeforeUpdate event (which can be cancelled). Code like

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
If IsNull(Me.[Part #]) Then
iAns = MsgBox("Please fill in the Part Number or click Cancel" _
& " to start over:", vbOKCancel)
Cancel = True
If iAns = vbCancel Then
Me.Undo ' erase all the user's changes
Else
Me.[Part #].SetFocus
End If
End If

<similar code for the other required fields>


John W. Vinson[MVP]
 
J

John Vinson

Hi John,

Thanks for the codes. I tried it and it partially works but when I click on
the next page button to go to the next empty text field a pop-up screen gives
tells me that I have not filled out that indicated field click OK or Cancel.
The cancel button works well but when I click on the OK button I get this
message:

Run-Time Error'438'
Object doesn't support this property or method
End, Debug, Help

When I click on the debug, it prompts me to the VB code page and with the
following highlighted yellow.

Me.[ISO #].SetFocus


Thanks for your help.

Do you have a Textbox (or other such control - combo box...?) named
[ISO #] on the Form? If not, then you can't set focus to it. Is [ISO
#] perhaps just the name of a field in your table, rather than the
name of an object on the form?

John W. Vinson[MVP]
 
J

John Vinson

John,

I modified your code. I have multiple field how do I use this code to
include multiple text field to validate I can do it by coping the field and
changing the Me.[text field name] but can I use (Me.[iso year]),
(Me.[country]) = true

If IsNull(Me.[iso year]) = True Then
MsgBox "You must enter a Year."
Me.[iso year].SetFocus
Exit Sub
End If
End Sub

The simplest way - though it might be a hassle for the user - is just
to put four If... End If blocks one after another. It will check all
of them. If the user corrects one and tries to save the record it will
warn them about the next one.

IsNull() returns either True or False, so you don't actually need the
= True. The IF statement will take the appropriate action depending on
whether IsNull() returns True or False.

It's ESSENTIAL to set the Cancel argument to True however! Otherwise
Access will go ahead and save the record.

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top