Need Help Event Procedure for OnClick Command Box

S

Sondra

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

I have a form with a new record button (user clicks on the button and an
empty version (new record) of the form opens.) I want to double check that
all fields have been completed prior to leaving current form.

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 a 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.
 
O

Ofer

On the before update event of the form, check if all the data been entered to
the required field

If Isnull(Me.Field1) or Isnull(Me.Field2) or Isnull(Me.Field3) then
msgbox "Must eneter values"
cancel = true ' wont let the user exit the form
end if
 
S

Sondra

I did as you suggested and got the correct error message, but it still
allowed the form to close. When the msg box appeared, I clicked ok and it
closed the form. Any advise.
 
O

Ofer

Did you put the code on the before update of the form, and did you put the code
cancel=true

Are the field bounded to a table? If not then put the code in the unload
event of the form.
 
S

Sondra

Yes, I did the code exactly as your identified.
Yes, the fields are bound to the table.

Still doesn't work.
 
S

Sondra

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.Deptartment) Or IsNull(Me.State) Or IsNull(Me.Initials) Or
IsNull(Me.Date) Or IsNull(Me.WBN) Or IsNull(Type_Des) Or IsNull(Me.DocNumber)
Then
MsgBox "You left a required field blank. Please go back and enter
the required information", vbCritical, "Field Required"
Cancel = True ' wont let the user exit the form
End If

End Sub
 
S

Sondra

Thank you so much!!! It works perfectly now.

Ofer said:
Are you using a button on the form to close the form, in that case put the
code also in the button event

If IsNull(Me.Deptartment) Or IsNull(Me.State) Or IsNull(Me.Initials) Or
IsNull(Me.Date) Or IsNull(Me.WBN) Or IsNull(Type_Des) Or IsNull(Me.DocNumber)
Then
MsgBox "You left a required field blank. Please go back and enter
the required information", vbCritical, "Field Required"

esle
docmd.close
End If
 
O

Ofer

Are you using a button on the form to close the form, in that case put the
code also in the button event

If IsNull(Me.Deptartment) Or IsNull(Me.State) Or IsNull(Me.Initials) Or
IsNull(Me.Date) Or IsNull(Me.WBN) Or IsNull(Type_Des) Or IsNull(Me.DocNumber)
Then
MsgBox "You left a required field blank. Please go back and enter
the required information", vbCritical, "Field Required"

esle
docmd.close
End If
 
Top