Don't Close Form if Field is null

G

Guest

Hello,
I've have been looking at other questions similar to mine, but I still can't
get this to work. I have a button to close a form. On click of that button it
needs to check - If [RptDate] is not null And [Contact] is null, then give
the message that Contact Must be Entered and go to the Null Contact field.
Otherwise, Close the Form. Here is what I have. Thanks!

Private Sub CloseForm_Click()
On Error GoTo Err_CloseForm_Click

If Me.RptDate Is Not Null And IsNull(Me.Contact) Then MsgBox "Contact must
be entered."
DoCmd.GoToControl "Me.Contact"

Else
DoCmd.Close

Exit_CloseForm_Click:
Exit Sub

Err_CloseForm_Click:
MsgBox Err.Description
Resume Exit_CloseForm_Click

End Sub
 
R

RuralGuy

Hello,
I've have been looking at other questions similar to mine, but I still
can't get this to work. I have a button to close a form. On click of
that button it needs to check - If [RptDate] is not null And [Contact]
is null, then give the message that Contact Must be Entered and go to
the Null Contact field. Otherwise, Close the Form. Here is what I
have. Thanks!

Private Sub CloseForm_Click()
On Error GoTo Err_CloseForm_Click

If Me.RptDate Is Not Null And IsNull(Me.Contact) Then MsgBox "Contact
must be entered."
DoCmd.GoToControl "Me.Contact"

Else
DoCmd.Close

Exit_CloseForm_Click:
Exit Sub

Err_CloseForm_Click:
MsgBox Err.Description
Resume Exit_CloseForm_Click

End Sub

Hi Emmy,

Did you cut and paste this code directly from your form?

If not I will be troubleshooting typing errors but here goes.

With If...Then...Else clauses NOTHING should be after the THEN
but comments so move the MsgBox to the next line.

The IF should be changed from:
If Me.RptDate Is Not Null And IsNull(Me.Contact) Then
To:
If Not IsNull(Me.RptDate) AND IsNull(Me.Contact) Then

Are "RptDate" and "Contact" controls on your form or fields in
your record or both?

We can get this working if I have a little more information.

Post back.
 
G

Guest

RptDate and Contact are fields in the table and the names of the controls on
the form. Now I get the message: Object Required. The only option is Okay and
it doesn't do anything. This is what I have now. Thanks for your help.

Private Sub CloseForm_Click()
On Error GoTo Err_CloseForm_Click

If Me.RptDate Is Not Null And IsNull(Me.Contact) Then

MsgBox "Contact must be entered."
DoCmd.GoToControl "Me.Contact"
Else
DoCmd.Close
End If

Exit_CloseForm_Click:
Exit Sub

Err_CloseForm_Click:
MsgBox Err.Description
Resume Exit_CloseForm_Click

End Sub

RuralGuy said:
Hello,
I've have been looking at other questions similar to mine, but I still
can't get this to work. I have a button to close a form. On click of
that button it needs to check - If [RptDate] is not null And [Contact]
is null, then give the message that Contact Must be Entered and go to
the Null Contact field. Otherwise, Close the Form. Here is what I
have. Thanks!

Private Sub CloseForm_Click()
On Error GoTo Err_CloseForm_Click

If Me.RptDate Is Not Null And IsNull(Me.Contact) Then MsgBox "Contact
must be entered."
DoCmd.GoToControl "Me.Contact"

Else
DoCmd.Close

Exit_CloseForm_Click:
Exit Sub

Err_CloseForm_Click:
MsgBox Err.Description
Resume Exit_CloseForm_Click

End Sub

Hi Emmy,

Did you cut and paste this code directly from your form?

If not I will be troubleshooting typing errors but here goes.

With If...Then...Else clauses NOTHING should be after the THEN
but comments so move the MsgBox to the next line.

The IF should be changed from:
If Me.RptDate Is Not Null And IsNull(Me.Contact) Then
To:
If Not IsNull(Me.RptDate) AND IsNull(Me.Contact) Then

Are "RptDate" and "Contact" controls on your form or fields in
your record or both?

We can get this working if I have a little more information.

Post back.
 
R

RuralGuy

See inline:
RptDate and Contact are fields in the table and the names of the
controls on the form. Now I get the message: Object Required. The only
option is Okay and it doesn't do anything. This is what I have now.
Thanks for your help.

Private Sub CloseForm_Click()
On Error GoTo Err_CloseForm_Click

If Me.RptDate Is Not Null And IsNull(Me.Contact) Then
***Emmy, change this line so it looks like the following!***
If Not IsNull(Me.RptDate) AND IsNull(Me.Contact) Then

MsgBox "Contact must be entered."
DoCmd.GoToControl "Me.Contact"
Else
DoCmd.Close
End If

Exit_CloseForm_Click:
Exit Sub

Err_CloseForm_Click:
MsgBox Err.Description
Resume Exit_CloseForm_Click

End Sub

I personally try to use a naming convention so the name identifies
the control. It also helps Access from getting confused on occasion.
For example I would have prefaced each TextBox with txt
ie: txtRptDate, txtContact

It makes it easier for a stranger like me to read the code and for you
several years later when you go to modify the code.

Make the change to the "IF" statement line and let's see what happens.
 
G

Guest

This is what I have now. I don't get an error message, but I also don't get
the message box. It just closes. Thanks!!

Private Sub CloseForm_Click()
On Error GoTo Err_CloseForm_Click

If Not IsNotNull(Me.txtRptDate) And IsNull(Me.txtContact) Then
MsgBox "Contact must be entered."
DoCmd.GoToControl "Me.txtContact"
Else
DoCmd.Close
End If

Exit_CloseForm_Click:
Exit Sub

Err_CloseForm_Click:
MsgBox Err.Description
Resume Exit_CloseForm_Click

End Sub
 

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