Inability to Move to next field

S

Sondra

I have a simple form that has several required fields in it. I've made the
fields "required" in the table, but I'm not sure how to make them "required"
in the form. Basically I want to make sure that when a user moves from field
"A" to field "B" that an error message occurs if they left the field null.

Users are currently able to close the form without completing the fields and
therefore its voiding my autonumbers.

Any help would be appreciated.

Thanks
 
A

Allen Browne

There is no sensible way to force a user to enter something before leaving a
control. If you did, they could never close the form unless they entered
another record.

The autonumber is assigned at the time when the user begins adding a record.
You need to let them abort the entry, and therefore you need to accept that
that assigned number will not be used again.

If the autonumber is being assigned as soon as the move to a new record, you
must be dirtying the record somewhere. For example, you may have code in the
Current event of the form, or in the Enter event of a control that is
assigning a value before it should be assigned.
 
S

Sondra

The autonumber isn't really my extreme problem. My problem is just wanting
to create a simple message that says you can not exit the form with null
values in fields.

Is there not a way??
 
A

Allen Browne

Ah, that's easy. Use the BeforeUpdate event of the form to test if any of
the required fields are null.

This kind of thing:

Private Sub Form_BeforeUpate(Cancel As Integer)
Dim strMsg As String

If IsNull(Me.Surname) Then
Cancel = True
strMsg = "Surname cannot be blank." & vbCrLf
End If

If IsNull(Me.City) Then

Cancel = True
strMsg = "City cannot be blank." & vbCrLf
End If

'etc. for other controls you wish to check.

If Cancel Then
strMsg = strMsg & vbCrLf & "Correct the entry, or press <Esc> to
undo."
MsgBox strMsg, vbExclamation, "Invalid Data"
End If
End Sub


If the user starts entering something into a required field, and backspaces
the entry, they might get stuck in the box. If that's the problem your
options are:
a) Teach the users to press <Esc> to undo the box (simplest solution).

b) Instead of setting the field's Required property to Yes in the table, set
its Validation Rule to:
Is No Null
and then set its Validation Text to the message you want to see.

c) Use the Error event of the form to capture the DataErr. Display your own
message, and set Response to acDataErrContinue.
 
W

Wolfgang Kais

Hello Sondra.

Sondra said:
I have a simple form that has several required fields in it. I've made
the fields "required" in the table, but I'm not sure how to make them
"required" in the form. Basically I want to make sure that when a user
moves from field "A" to field "B" that an error message occurs if they
left the field null.

Users are currently able to close the form without completing the
fields and therefore its voiding my autonumbers.

How about the Exit event for the textboxes? Try something like this:

Private Sub txtA_Exit(Cancel As Integer)
If IsNull(txtA) Then
MsgBox "Stop, enter data first!", vbCritical, "Field required"
Cancel = True
End If
End Sub
 
S

Sondra

Allen:

This seems to be very close; however, I have a cmd button on my form that
allows the user to go back to the main switchboard. When the user presses
the button the error message "Correct the entry, or press <Esc> to undo."
comes up. When I press the OK button the the error message, it closes my
form and returns to the main switchboard.

Any thoughts or suggestions?

Thanks

Sondra
 
S

Sondra

This works great, if Allen isn't able to come up with another idea, I most
definitely will use this option.

Thanks.
 
A

Allen Browne

Be sure to test how you go closing a form at the new record, without making
an entry.
 
W

Wolfgang Kais

Hello Allen!

Does your code prevent the form to be closed if a field is null?
Of course mine does neither.
Can this be done at all?
 
S

Sondra

Allen:

Not sure what you mean "test how you go closing a form at the new record..."

Please advise.

Thanks S
 
A

Allen Browne

Test what your code does if you have entered the record, moved to a new
record, and you have the focus in the text box when you close the form.

I would expect the control's Exit event to be triggered before the form
closes, and (because you are at a new record), the control is null at that
time. Just check this scenario yields an outcome that is acceptable to you.
 
S

Sondra

Allen:

I have to apologize for asking some real "simple answered" questions but I
need more advise on this:

My new record button On Click event reads:
*****************
Private Sub NewCSCR_Click()

On Error GoTo Err_NewCSCR_Click

DoCmd.GoToRecord , , acNewRec

Exit_NewCSCR_Click:
Exit Sub

Err_NewCSCR_Click:
MsgBox Err.Description
Resume Exit_NewCSCR_Click

End Sub
*************

From reading your web reference, I know I have to enter this somewhere, but
not sure where:
*********************
If (Me.Dirty) Then
Me.Dirty = False
End If
*************

Please advise.
 
Top