Data Entry Check

Emma35

Registered User.
Local time
Today, 10:06
Joined
Sep 18, 2012
Messages
497
Hi Guys,
I have a form i use to enter information on equipment assessments. There are three text boxes on the form which i would like to make sure that the user actually fills in. I've tried using the data validation thing in the table properties but that doesn't give me an error until the whole form is filled in. I'd like something that will alert the user as soon as he/she moves out of the text box in question. I've been using the code below in the AfterUpdate event of the Form itself but no luck.

Code:
Private Sub Form_AfterUpdate()
If IsNull(Me.AssessmentID) Then
MsgBox "You must enter an Assessment Number."
Me.AssessmentID.SetFocus
Cancel = True
End If
End Sub

I'd appreciate any advice,
Thanks,
Emma
 
Look at "DemoForm2A2000.mdb" (attachment, zip).
Open Form and try to enter a new record, and try to skip a field F1.
F1 field is Required, (look at Table1 F1 field properties).
Look at VBA, adapt it in your mdb.
 

Attachments

Try putting your code in the Form's Before Update event.
 
John...tried the BeforeUpdate but didn't work.

MStef....I used your code for the first text box and it worked great. However when i try to extend it to the second of the three boxes i'd like filled in i'm getting an error message when i run the form.
The fields on my form are AssessmentID, AssessmentName, and AssessmentDate in that order. The code so far is

Code:
Private Sub AssessmentName_GotFocus()
If IsNull(Me.AssessmentID) Then
   Er1
End If
End Sub
 
Private Sub Form_Current()
Me.AssessmentID.SetFocus
End Sub
Sub Er1()
  MsgBox "You must enter an Assessment Number.", vbOKOnly, "Attention!"
  Me.AssessmentID.SetFocus
Exit Sub
End Sub

Private Sub AssessmentDate_GotFocus()
If IsNull(Me.AssessmentName) Then
   Er1
End If
End Sub
Private Sub Form_Current()
Me.AssessmentName.SetFocus
End Sub
Sub Er1()
  MsgBox "You must enter an Assessment Name.", vbOKOnly, "Attention!"
  Me.AssessmentName.SetFocus
Exit Sub
End Sub

I'm attaching a pic of the error message

Thanks
 

Attachments

  • Error.jpg
    Error.jpg
    32.4 KB · Views: 121
If the field "AssessmentDate" is a DATE field, in that case
you must write a VBA differently.
Look at Demo a new one (attachment, zip).
Open form and try.
What VBA you have got in the Current event ???
 

Attachments

Last edited:
Hi MStef,
I'm having a lot of trouble applying your code to my form due to my lack of VBA knowledge. I can get it to work on one field but once i try to extend it to work on several fields i'm getting error messages. Here's my code in the forms OnCurrent event as it stands.

Code:
Private Sub AssessmentDate_GotFocus()
If IsNull(Me.AssessmentID) Then
   Er1
End If
End Sub
Private Sub AssessmentName_GotFocus()
If IsNull(Me.AssessmentID) Then
   Er1
   Exit Sub
End If
If IsDate(Me.AssessmentDate) Then
   'nothing
Else
   Er2
End If
End Sub
Private Sub Form_Current()
Me.AssessmentID.SetFocus
End Sub
Sub Er1()
  MsgBox "You must enter an Assessment Number!", vbOKOnly, "Attention!"
  Me.AssessmentID.SetFocus
Exit Sub
End Sub
Sub Er2()
  MsgBox "You must enter an Assessment Date."
  Me.AssessmentDate.SetFocus
End Sub
Sub Er3()
  MsgBox "You must enter an Assessment Name."
  Me.AssessmentName.SetFocus
End Sub

If it helps, the tab order for the controls is
AssessmentID
AssessmentName
AssessmentDate

Thanks
 
Send a short example of your mdb (access 2000 or 2002-2003),
and tell what you want to do.
 
Hi MStef....i've managed to do it using a macro attached to the On Lost Focus event of the controls themselves so everything's fine.

Thanks again for your help :)
 
Hi, I'm stuck with almost the same problem.

I have a mainform with a tabbed control that each tab has a subform in it. I have a bound textbox control on mainform named "txtLoadID."

What I want to do is when "txtLoadID" loses focus and "is null," a message will be displayed that "txtLoadID" needs a value, then cursor goes back to the "txtLoadID" control.

I added an "Event Procedure" on the "On Lost Focus" event of "txtLoadID"

Code:
Private Sub txtLoadID_LostFocus()

If IsNull(Me.txtLoadID) Then

MsgBox "Please enter an Load Number before adding any Load or Invoice items."

Me.txtLogpID.SetFocus

End If

End Sub

But this only displayed the message, but did not set the focus back to "txtLoadID"

Hope you guys can help.
 
Last edited:
May be just work around -

Private Sub txtLogpID_LostFocus()

If IsNull(Me.txtLogpID) Then

MsgBox "Please enter an LOGP Number before adding any Load or Invoice items."

Me.SomeOtherField.SetFocus
Me.txtLogpID.SetFocus

End If

End Sub
 
I see, I get what you mean, I actually setfocus to another control then bring it back to "txtLoadID"... ok, I will try it out. Be back in a bit...

Update:

:D It worked! A simple yet awesome workaround mahenkj2!

What I was doing before is putting this code on the "On Enter" event of the subforms.

Code:
If IsNull(Me.txtLoadID) Then

    If MsgBox("Please enter an LoadID Number before adding any Load or Invoice items.", vbOKOnly, "LOAD Number") = vbOK Then
        
        Me.txtLoadID.SetFocus
    
    End If
End If

End Sub

I would put this code in every "On Enter" event, which was a pain and very in-efficient.

Thanks!
 
Last edited:

Users who are viewing this thread

Back
Top Bottom