complete all the fields before exit

C

Carol Shu

that's say I have a form, there are 10 fields in this form, how do I make
user complete all the fields before exit the database, if user only enter 9
fields, the screen will popup a message like "can not exit unless all the
fields are compete.." please help, many thanks.
 
L

Larry Linson

Carol Shu said:
that's say I have a form, there are 10 fields in this form, how do I make
user complete all the fields before exit the database, if user only enter
9
fields, the screen will popup a message like "can not exit unless all the
fields are compete.." please help, many thanks.

Put code in the BeforeUpdate event of the Form to validate that none of the
fields are empty or null, if any are, set the Cancel argument to True
(cancels the update event), issue a MsgBox explaining why you are not
allowing the update, and exit.

Larry Linson
Microsoft Access MVP
 
L

Larry Linson

For a Form with TextBox Controls named txtTextID, txtItemName, txtType,
txtColor, txtWeight, txtHeight, txtWidth, txtDesc, txtSource, txtCategory,
each bound to a data Field in the Table/Query that is the RecordSource of
the Form, the following code in the BeforeUpdate event does what you want.
It cancels the update if in any of the TextBox controls are Null (nothing
ever entered) or a zero-length string (something entered and deleted).

(Note: this is not "finished" code -- it does not include error handling.
You have to determine what error handling you need and add it.)

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.txtTextID = "" Or IsNull(Me.txtTextID) = True Or _
Me.txtItemName = "" Or IsNull(Me.txtItemName) = True Or _
Me.txtType = "" Or IsNull(Me.txtType) = True Or _
Me.txtColor = "" Or IsNull(Me.txtColor) = True Or _
Me.txtWeight = "" Or IsNull(Me.txtWeight) = True Or _
Me.txtHeight = "" Or IsNull(Me.txtHeight) = True Or _
Me.txtWidth = "" Or IsNull(Me.txtWidth) = True Or _
Me.txtDesc = "" Or IsNull(Me.txtDesc) = True Or _
Me.txtSource = "" Or IsNull(Me.txtSource) = True Or _
Me.txtCategory = "" Or IsNull(Me.txtCategory) = True Then
MsgBox "All information must be filled in"
Cancel = True
End If
Exit_Proc: Exit Sub

End Sub

Larry Linson
Microsoft Access MVP
 
C

Carol Shu

Thank you dear.

Larry Linson said:
For a Form with TextBox Controls named txtTextID, txtItemName, txtType,
txtColor, txtWeight, txtHeight, txtWidth, txtDesc, txtSource, txtCategory,
each bound to a data Field in the Table/Query that is the RecordSource of
the Form, the following code in the BeforeUpdate event does what you want.
It cancels the update if in any of the TextBox controls are Null (nothing
ever entered) or a zero-length string (something entered and deleted).

(Note: this is not "finished" code -- it does not include error handling.
You have to determine what error handling you need and add it.)

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.txtTextID = "" Or IsNull(Me.txtTextID) = True Or _
Me.txtItemName = "" Or IsNull(Me.txtItemName) = True Or _
Me.txtType = "" Or IsNull(Me.txtType) = True Or _
Me.txtColor = "" Or IsNull(Me.txtColor) = True Or _
Me.txtWeight = "" Or IsNull(Me.txtWeight) = True Or _
Me.txtHeight = "" Or IsNull(Me.txtHeight) = True Or _
Me.txtWidth = "" Or IsNull(Me.txtWidth) = True Or _
Me.txtDesc = "" Or IsNull(Me.txtDesc) = True Or _
Me.txtSource = "" Or IsNull(Me.txtSource) = True Or _
Me.txtCategory = "" Or IsNull(Me.txtCategory) = True Then
MsgBox "All information must be filled in"
Cancel = True
End If
Exit_Proc: Exit Sub

End Sub

Larry Linson
Microsoft Access MVP
 
L

Larry Linson

Carol Shu said:
Thank you dear.

You are welcome.

It may even be quicker (and safer since enforced at the engine level) to
just set the "required" property on each of the data Fields in the Table
Definition, but I think you'll have a little less flexibility on handling
errors.

Larry Linson
Microsoft Access MVP
 
Top