Form validation help

PRodgers4284

Registered User.
Local time
Today, 00:36
Joined
Feb 17, 2009
Messages
64
Im looking to add some validation to textboxs within a form ie if a textbox is empty/has not been completed output an error message. Can anyone provide me with an example of validation for this?
 
On the save command you could use ....

Code:
    If Len(Nz(Me!txtControlName, "")) = 0 Then
        MsgBox "You must enter the something.", , "The Something"
        Me!txtControlName.SetFocus
    ...

If alot of controls then you could loop through the controls collection of the form. Do a search in this forum for "loop through the controls on a form" or something along those lines.

Hope that helps,
-dK
 
On the save command you could use ....

Code:
    If Len(Nz(Me!txtControlName, "")) = 0 Then
        MsgBox "You must enter the something.", , "The Something"
        Me!txtControlName.SetFocus
    ...

If alot of controls then you could loop through the controls collection of the form. Do a search in this forum for "loop through the controls on a form" or something along those lines.

Hope that helps,
-dK

Hey thanks for the reply, im looking validation to add to an add command button that checks if the user has entered for example a person forename. Im new to the vb side of things and im looking for a basic example of textbox validation.
 
That was it ....

I made the assumption that you have a command button that saves and closes the form.

Suppose you already have one set up and the underlying VBA looked like the following ...

Code:
Private Sub cmdButtonName_Click()

   DoCmd.RunCommand acCmdSaveRecord
   DoCmd.Close
 
End Sub

You would want it to look like ....

Code:
Private Sub cmdButtonName_Click()
    If Len(Nz(Me!txtControlName, "")) = 0 Then
        MsgBox "You must enter the something.", , "The Something"
        Me!txtControlName.SetFocus
    Else
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Close
    End If
 End Sub

You will need to make sure to replace cmdButtonName with the command button name and txtControlName with the name of the control you are wanting to validate.

-dK
 
Save button or not, this kind of code is usually placed in the Form_BeforeUpdate event.
 

Users who are viewing this thread

Back
Top Bottom