Open Form in Add Mode

B

Boreal2009

Hi Everyone,
I am VERY new to VBA so please bear with me! I am using Access 2003 and I
have a form that when it is loaded it prompts the user to answer a yes or no
question. When yes is selected I want the form to open in Edit mode and when
no is selected I want the form to open in Add mode.

Below is the code that is attached to the On Load Event. It works perfectly
when yes is selected and opens the form in Edit mode. However, when no is
selected, the form still opens in Edit mode. I have tried the same code in
the On Open Event with the same results. It should also be noted that the
form contains a subform.

I've been searching most of the day to try and figure this one out. Can
anyone provide me with detailed directions on how to solve this problem?
Thanks in advance!

Private Sub Form_Load()
If MsgBox("Is this a Re-Capture?", vbYesNo + vbInformation, "Information
Needed") = vbYes Then
DoCmd.RunMacro "Search Message MACRO"
DoCmd.RunCommand acCmdFind
Else
DoCmd.OpenForm "FORM - Male Capture Information", acNormal, , , acFormAdd,
acWindowNormal, OpenArgs
End If
End Sub
 
D

Daniel Pineault

If you indent your code the problem might become easier to spot.

Private Sub Form_Load()
If MsgBox("Is this a Re-Capture?", vbYesNo + vbInformation, _
"InformationNeeded ") = vbYes Then
DoCmd.RunMacro "Search Message MACRO"
DoCmd.RunCommand acCmdFind
Else
DoCmd.OpenForm "FORM - Male Capture Information", acNormal, _
, , acFormAdd, acWindowNormal, OpenArgs
End If
End Sub

According to your code if the user clicks YES then
DoCmd.RunMacro "Search Message MACRO"
DoCmd.RunCommand acCmdFind
and if the User clicks NO then
DoCmd.OpenForm "FORM - Male Capture Information", acNormal, _
, , acFormAdd, acWindowNormal, OpenArgs

You are actively opening the form in the NO case! You need to review your
if clause.

You might want to try something more like:

Private Sub Form_Load()
On Error GoTo Error_Handler

If MsgBox("Is this a Re-Capture?", vbYesNo + vbInformation, _
"InformationNeeded ") = vbYes Then
'If we are here it is because the user selected Yes
DoCmd.RunMacro "Search Message MACRO"
DoCmd.RunCommand acCmdFind
'Open the form in Edit Mode
DoCmd.OpenForm "FORM - Male Capture Information", acNormal, _
, , acFormEdit, acWindowNormal, OpenArgs
Else
'If we are here it is because the user selected No
'Open the form in Add Mode
DoCmd.OpenForm "FORM - Male Capture Information", acNormal, _
, , acFormAdd, acWindowNormal, OpenArgs
End If

Exit Sub

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: Form_Load" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Sub
End Sub

PS: Add Error handling code!
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 

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