Information Code

C

chooriang

Hi,
I have some fields in my form were may not leave blank ( Required properties
is set to yes ).
I want when a user leave the field in blank condition and try to saving the
record,a message box appear and the cursor automtically move to the blank
fields.How to build the code or what is the code?
Would somebody help.
 
G

Graham R Seach

Private Sub txtMyTextBox_Exit(Cancel As Integer)
If Len("" & Me!txtMyTextBox) = 0 Then
Cancel = True
MsgBox "You can't leave this field blank!"
End If
End Sub

Do the same to every field you want to check.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
J

John Vinson

Hi,
I have some fields in my form were may not leave blank ( Required properties
is set to yes ).
I want when a user leave the field in blank condition and try to saving the
record,a message box appear and the cursor automtically move to the blank
fields.How to build the code or what is the code?
Would somebody help.

I'd suggest using the Form's BeforeUpdate event to check the fields:

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
On Error GoTo Proc_Error
If IsNull(Me.txtThisField) Then
iAns = MsgBox("Please fill in data for ThisField or click Cancel", _
vbOKCancel)
Cancel = True
If iAns = vbCancel Then
Me.Undo
Else
Me.txtThisField.SetFocus
End If
GoTo Proc_Exit
End If
If IsNull(Me.txtThatField) Then
iAns = MsgBox("Please fill in data for ThatField or click Cancel", _
vbOKCancel)
Cancel = True
If iAns = vbCancel Then
Me.Undo
Else
Me.txtThatField.SetFocus
End If
GoTo Proc_Exit
End If
Proc_Exit:
Exit Sub
Proc_Error:
<error handling code here>
Resume Proc_Exit
End Sub

This can be made much more modular and elegant if you wish but it'll
give you a start...

John W. Vinson[MVP]
 
Top