What is wrong with my code?

G

Golfinray

This code does not pause and allow the user to pick a sort method on form
"area form." I brings it up for a second and goes right on to the report.
What do I need to do? Even if I pick "no" it briefly shows the area form and
then goes right on to the report. Thanks!!!
Private Sub Command25_Click()
On Error GoTo Err_Command25_Click

Dim stDocName As String
stDocName = "06-07 Active Projects"
MsgBox "would you like to sort by area?", vbYesNo, continue
If vbYes Then DoCmd.OpenForm "area form", acNormal
If vbNo Then DoCmd.OpenReport "06-07 active projects", acViewPreview


Exit_Command25_Click:
Exit Sub

Err_Command25_Click:
MsgBox Err.Description
Resume Exit_Command25_Click

End Sub
 
C

Chris O'C via AccessMonster.com

The form needs to open in dialog mode. The code will pause as long as the
form is open, when you close it the code continues.

If vbYes Then DoCmd.OpenForm "area form",acNormal,,,,acDialog

Chris
Microsoft MVP
 
G

Golfinray

Thanks, Chris, that did the trick.

Chris O'C via AccessMonster.com said:
The form needs to open in dialog mode. The code will pause as long as the
form is open, when you close it the code continues.

If vbYes Then DoCmd.OpenForm "area form",acNormal,,,,acDialog

Chris
Microsoft MVP
 
G

Golfinray

Chris, one more question, now when I say "no" it goes to the "area form"
anyway. I want it to go straight to the report. Thanks!!
 
D

Dirk Goldgar

Golfinray said:
This code does not pause and allow the user to pick a sort method on form
"area form." I brings it up for a second and goes right on to the report.
What do I need to do? Even if I pick "no" it briefly shows the area form
and
then goes right on to the report. Thanks!!!
Private Sub Command25_Click()
On Error GoTo Err_Command25_Click

Dim stDocName As String
stDocName = "06-07 Active Projects"
MsgBox "would you like to sort by area?", vbYesNo, continue
If vbYes Then DoCmd.OpenForm "area form", acNormal
If vbNo Then DoCmd.OpenReport "06-07 active projects", acViewPreview


Exit_Command25_Click:
Exit Sub

Err_Command25_Click:
MsgBox Err.Description
Resume Exit_Command25_Click

End Sub


You're not actually retrieving and checking the return code of the MsgBox
function. Your If statements are just examining the values of the defined
constants vbYes and vbNo. Neither of these values is 0, so both statements
get executed. Try this:

If MsgBox( _
"would you like to sort by area?", vbYesNo, continue) _
= vbYes _
Then
DoCmd.OpenForm "area form", acNormal
Else
DoCmd.OpenReport "06-07 active projects", acViewPreview
End If

Note that this logic doesn't open the report at all if the user replied Yes.
That follows the logic you wrote, but may not be what you had in mind.
 
C

Chris O'C via AccessMonster.com

Sorry, my bad. I didn't look very closely at your code. The vbYes and vbNo
aren't being compared to anything so Access is using the implicit comparison:

if vbYes <> 0 then

which is always true, same for vbNo.


This code will work better:

Dim mbresult As Integer

mbresult = MsgBox("would you like to sort by area?", vbYesNo, "continue")
If mbresult = vbYes Then
DoCmd.OpenForm "area form", acNormal,,,,acDialog
Else
DoCmd.OpenReport "06-07 active projects",acViewPreview
End If


Chris
Microsoft MVP
 

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