Open form code

T

Tony Williams

I have a form that when it opens asks the user if they have performed a task
before they continue. If they answer "No" to the message box then the
current form should close and a new form opens. I have used this code but it
the first form doesn't close and the new one doesn't open. I thought this
was a simple action but I can't see what I'm doing wrong? Can anyone help?
Thanks
Tony
Private Sub Form_Open(Cancel As Integer)
Dim stformname As String
stformname = "frmeurovalue"
DoCmd.Restore
If MsgBox("Have you updated the Euro Value table for this quarter?",
vbYesNo, "Euro Update?") = No Then
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm stformname, acNormal

End If

End Sub
 
A

Allen Browne

Just cancel the event instead of closing the form:
Cancel = True

(If you used OpenForm in another event, that one will be notified with error
2501 that the form was cancelled, so that event will need error handling.)
 
T

Tony Williams

Thanks Allen I've changed my code to this and it still doesn't work, the
form stays open and the other form doesn't open. This seemed should a simple
thing?
Tony
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open
Dim stformname As String
stformname = "frmeurovalue"
DoCmd.Restore
If MsgBox("Have you updated the Euro Value table for this quarter?",
vbYesNo, "Euro Update?") = No Then
DoCmd.OpenForm stformname, acNormal
Cancel = True
Exit_Err_Form_Open:
Exit Sub

Err_Form_Open:
MsgBox Err.Description
Resume Exit_Err_Form_Open

End If

End Sub
 
A

Allen Browne

1. Dump the Restore (or move it later in the procedure.)

2. Test if the MsgBox returns vbNo, not No.

3. Move the End If into the right place.

4. Comment out the error handler until you get it working.

Private Sub Form_Open(Cancel As Integer)
'On Error GoTo Err_Form_Open

If MsgBox("Have you updated the Euro Value table for this quarter?", _
vbYesNo, "Euro Update?") = vbNo Then
DoCmd.OpenForm "frmeurovalue"
Cancel = True
Else
DoCmd.Restore
End If

Exit_Err_Form_Open:
Exit Sub

Err_Form_Open:
MsgBox Err.Description
Resume Exit_Err_Form_Open
End Sub
 
T

Tony Williams

That worked fine, thanks Allen
Tony
Allen Browne said:
1. Dump the Restore (or move it later in the procedure.)

2. Test if the MsgBox returns vbNo, not No.

3. Move the End If into the right place.

4. Comment out the error handler until you get it working.

Private Sub Form_Open(Cancel As Integer)
'On Error GoTo Err_Form_Open

If MsgBox("Have you updated the Euro Value table for this quarter?", _
vbYesNo, "Euro Update?") = vbNo Then
DoCmd.OpenForm "frmeurovalue"
Cancel = True
Else
DoCmd.Restore
End If

Exit_Err_Form_Open:
Exit Sub

Err_Form_Open:
MsgBox Err.Description
Resume Exit_Err_Form_Open
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