Code to stop form opening on a condition

J

JohnB

Hi. I'm sure this one will be simple. I have an On Open event (see below) in
Form called frmSetImage that's designed to check a field value and stop the
form opening if there is content in the field. I've got as far as showing a
message but when I click on the MsgBox OK button, the form opens. What could
I add that shows the message and then stops the form opening after OK is
clicked? Or have I get too far by then? I could check for content in the
same field in form frmStudents, which has a command button that calls up
frmSetImage but I don't know which event to use. Using On Lost Focus dosen't
work.

Thanks for any help. JohnB

Private Sub Form_Open(Cancel As Integer)
If Not IsNull(Me![txtImagePathAndFile]) Then
MsgBox "Photo already allocated"
End If
End Sub
 
R

Rick Brandt

JohnB said:
Hi. I'm sure this one will be simple. I have an On Open event (see
below) in Form called frmSetImage that's designed to check a field
value and stop the form opening if there is content in the field.
I've got as far as showing a message but when I click on the MsgBox
OK button, the form opens. What could I add that shows the message
and then stops the form opening after OK is clicked? Or have I get
too far by then? I could check for content in the same field in form
frmStudents, which has a command button that calls up frmSetImage but
I don't know which event to use. Using On Lost Focus dosen't work.

Thanks for any help. JohnB

Private Sub Form_Open(Cancel As Integer)
If Not IsNull(Me![txtImagePathAndFile]) Then
MsgBox "Photo already allocated"
End If
End Sub

Notice that the Open event above has a Cancel argument. You just need to
set that to True.

Private Sub Form_Open(Cancel As Integer)

If Not IsNull(Me![txtImagePathAndFile]) Then
MsgBox "Photo already allocated"
Cancel = True
End If

End Sub
 
J

JohnB

Thanks Rick. That works fine but I now get a second message that I could do
without:

"The OpenForm action was cancelled blah blah"

How can I stop that appearing?

Thanks again, JohnB

Rick Brandt said:
JohnB said:
Hi. I'm sure this one will be simple. I have an On Open event (see
below) in Form called frmSetImage that's designed to check a field
value and stop the form opening if there is content in the field.
I've got as far as showing a message but when I click on the MsgBox
OK button, the form opens. What could I add that shows the message
and then stops the form opening after OK is clicked? Or have I get
too far by then? I could check for content in the same field in form
frmStudents, which has a command button that calls up frmSetImage but
I don't know which event to use. Using On Lost Focus dosen't work.

Thanks for any help. JohnB

Private Sub Form_Open(Cancel As Integer)
If Not IsNull(Me![txtImagePathAndFile]) Then
MsgBox "Photo already allocated"
End If
End Sub

Notice that the Open event above has a Cancel argument. You just need to
set that to True.

Private Sub Form_Open(Cancel As Integer)

If Not IsNull(Me![txtImagePathAndFile]) Then
MsgBox "Photo already allocated"
Cancel = True
End If

End Sub
 
R

Rick Brandt

JohnB said:
Thanks Rick. That works fine but I now get a second message that I
could do without:

"The OpenForm action was cancelled blah blah"

How can I stop that appearing?

The code that was attmeptong to oepn the form needs to have an error trap
set up to ignore error number 2501.

Sub SomeButton_Click
On Error GoTo ErrHandler

DoCmd.OpenForm "FormName"

Egress:
Exit Sub

ErrHandler
Select Case Err.Number
Case 2501
'ignore
Case Else
(normal error hadling code)
End Select
Resume Egress
End Sub
 
J

JohnB

Hi Rick. I've converted your code to what I think is needed but the message
still appears. Have I understood you correctly? Here is my full code: Thanks
for the speedy responses. JohnB

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open
If Not IsNull(Me![txtImagePathAndFile]) Then
MsgBox "Photo already allocated"
Cancel = True
End If
Exit_Form_Open:
Exit Sub
Err_Form_Open:
Select Case Err.Number
Case 2501
'ignore
Case Else
'(normal error hadling code)
End Select
Resume Exit_Form_Open
End Sub
 
R

Rick Brandt

JohnB said:
Hi Rick. I've converted your code to what I think is needed but the
message still appears. Have I understood you correctly? Here is my
full code: Thanks for the speedy responses. JohnB

The error trap for error 2501 goes in the code that is calling the form, not the
open event of the form. That is where the error is being reported.
 

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