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

Cant Enter Null Values in Forms 1

Status
Not open for further replies.

bjm027

MIS
Mar 7, 2003
59
US
I have a form for data entry in Microsoft Access. When users are entering data there are certain fields I want that must have data entered in them (they cant go on without entering something). I imagine there is coding for a certain event. Any help/suggestions? I imagine it is rather simple...

 
Hi,

Try this:

For each text box's "After Update" event drop in this code (please change Text1 to reflect the name of your text box on your form):

If IsNull(Me!Text1) Then
MsgBox "You cannot leave this field blank!"
Me!Text1.SetFocus
End If

 
Either set the default value for the control (assuring that a value will be there) or you can put "Validate" in the Tag property of control and insert the following into the Save button's event procedure:

Dim currctl As Integer, numctls As Integer
Dim ctl As Control

numctls = Screen.ActiveForm.Count

For currctl = 0 To numctls - 1
Set ctl = Me(currctl)
' Check the status of Controls with the Validate Tag
If ctl.Tag = "Validate" Then
If IsNull(ctl) Then
MsgBox "Please fill in the field(s) highlighted in red and click the Save button again.", vbOKOnly + vbCritical + vbDefaultButton1, "FIELD(S) EMPTY"
ctl.BackColor = 255
ctl.SetFocus
Exit Sub
ElseIf Not IsNull(ctl) Then
ctl.BackColor = Me!BackColor.BackColor 'Set backcolor of control back to original
End If

End If
Next currctl
End If


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

This will look at all controls on the form and turn an empty control (with the Validate Tag) red if it is not filled in and abort the save until it is.
 
Hi bjm027,

I made one slight mistake in my code! You should set this on your form's "On Close" event. Your code should look like this: (Change "Form" to the name of your form)

Add a If.. Then.. Statement for every text box that needs a value
********************************************************
Private Sub Form_Close()

If IsNull(Me!Text1) Then
MsgBox "You cannot leave this field blank!"
Me!Text1.SetFocus
End If

End Sub
********************************************************
Sorry about the mess!

jbehrne
 
Thanks jbehrne.

It was also working in the after_update but not as effectivly as on_close event

thanks a lot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top