Empty fields

G

Guest

I have two fields (military hour format) on a form. A user sometimes forgets
to enter on the first field and enters only on the second field. I don't want
to required a fields, but I just want the user to be aware he/she didn't
enter on the first field

1st field 2nd field
Bed_time and Rec_time

Here is my code in the "Before Update" in the Rec_time Property , but it
doesn't work

If IsEmpty(BED_TIME) Then
MsgBox "Bed Time is Blank" & vbCrLf & _
"Enter Bed Time first!", vbCritical + vbOKOnly, _
"Invalid!"
Cancel = True
endif
 
G

Guest

First, you don't have fields on a form. Fields are object in tables. Forms
have controls. In this case, the controls are probably Text Boxes.
Next, the IsEmpty function only works with Variant variables, not with
controls. Change the IsEmpty to IsNull and it should be fine.
 
G

Guest

ooops! I clicked post too soon.
If you want to be really friendly to the user, right after cancel = True,
put in this line:

Me.BED_TIME.SetFocus

That will make BED_TIME the active control.

Also, I find it interesting the rec time comes after bed time :^)
 
G

Guest

Thanks, Just one problem I'm stuck in the Rec Time text field, I can't get
out to change the Bed Time.
 
G

Guest

Even if you click on another control? Is there any other code in events for
Rec Time? How about at the form level? Is there a validation rule on the
field in the table?
 
G

Guest

Nothing works, if I delete the time in the field and hit "Escape" I can move
out the box. I don't have any other event code and no validation rule. Just
an Input mask of 00:00;0;_

The is Error is:
Erroe #-2147352567
The marco or function set to the BeforeUpdate or ValidationRule property for
this field is preventing Microsoft Access from saving the data in the field.
 
G

Guest

Just to see where this is happening, remove the input mask and see what that
does.
If it does not fix it, put it back in and try commenting out the SetFocus in
the Before Update event and see what happens.
Let me know.
 
G

Guest

I took the set focus out and the mask. Now only the MsgBox pops up telling me
the that the Bed Time is blank. I can still press Escape and move out of the
test field
 
G

Guest

There is something there we are not seeing. This is a very common way to do
this kind of validation.
 
J

John Vinson

I have two fields (military hour format) on a form. A user sometimes forgets
to enter on the first field and enters only on the second field. I don't want
to required a fields, but I just want the user to be aware he/she didn't
enter on the first field

1st field 2nd field
Bed_time and Rec_time

Here is my code in the "Before Update" in the Rec_time Property , but it
doesn't work

You need to use the Form's BeforeUpdate event, not the Rec_Time: the
user might not even set focus to Rec_Time.

To make the check optional, try:

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
Dim strMsg As String
If IsNull(Me!Bed_Time) Then
strMsg = "Bed_Time should be entered. Click OK to do so, Cancel" _
& " to leave it blank anyway:"
iAns = MsgBox(strMsg, vbOKCancel)
If iAns = vbOK Then
Cancel = True
Me!Bed_Time.SetFocus
End If
End If
End Sub

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top