Do not save blank fields

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form where I do not want the user to be able to move to the next
record without making sure some fields have been filled in. I cannot set the
field to required in the table, because the table is being used for other
forms where the field is not required.

I have tried doing this in the event before update but this is not working
and even if it does, I'm not sure it would stop the saving of the record.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If strvoltage.value is null then
msgbox "Enter Voltage"
end if
End Sub
 
Cynthia said:
I have a form where I do not want the user to be able to move to the
next record without making sure some fields have been filled in. I
cannot set the field to required in the table, because the table is
being used for other forms where the field is not required.

I have tried doing this in the event before update but this is not
working and even if it does, I'm not sure it would stop the saving of
the record.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If strvoltage.value is null then
msgbox "Enter Voltage"
end if
End Sub

You're on the right track. You must use the IsNull function, instead of
"If <field> Is Null", and you should set the event procedure's Cancel
argument to True to prevent the update:

'----- start of example code -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me!strvoltage) Then
Cancel = True
MsgBox "Enter Voltage"
Me!strvoltage.SetFocus
End If

End Sub

'----- end of example code -----
 
I have a form where I do not want the user to be able to move to the next
record without making sure some fields have been filled in. I cannot set the
field to required in the table, because the table is being used for other
forms where the field is not required.

I have tried doing this in the event before update but this is not working
and even if it does, I'm not sure it would stop the saving of the record.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If strvoltage.value is null then
msgbox "Enter Voltage"
end if
End Sub

You're close. The syntax "is null" works in SQL Queries but not in
VBA. Also, you can set the value of the Cancel argument to True to
prevent the record from being added. Try

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me!strVoltage) Then
Cancel = True
MsgBox "Enter voltage"
Me!strVoltage.SetFocus ' move to the textbox
Exit Sub ' so they don't get a whole rash of prompts
End If
<similar checks on other fields>
End Sub



John W. Vinson[MVP]
 
Back
Top