Iff Statement within A message box within a Parent Message Box (1 Viewer)

BettyWho

Registered User.
Local time
Yesterday, 18:53
Joined
Jun 7, 2013
Messages
37
Hi


I'm not sure if i am going about this the right way, but basically I want a message box on close to either action another message box with an iff statement or simply to close. This is what I have so far, I haven't toyed with any message box iff statements before so I'm not sure quite how to get the desired outcome. I may have gone about it the wrong way. I was able to get the if statement working without a secondary message box and that's where I've come undone :O. Thanks in advance.



Private Sub Form_Close()

Dim Answer As String

If MsgBox("Did you Select Client Contact Service from Type of Service?", vbQuestion + vbYesNo) = vbYes Then

Select Case Answer

Case vbYes

If MsgBox("Did you Check Sample Checkbox?", vbQuestion + vbYesNo) = vbYes Then

Select Case Answer

Case vbYes

DoCmd.RunCommand acCmdSaveRecord

DoCmd.RunCommand acCmdClose

Case vbNo

MsgBox ("Make sure you check Counselling and Therapy Checkbox!")


(hopefully this leaves the form open and can then be checked before closing.)


End If

Case vbNo
DoCmd.RunCommand acCmdClose
End If


End Select

End If

End Sub
 

Minty

AWF VIP
Local time
Today, 01:53
Joined
Jul 26, 2013
Messages
10,355
You have a couple of issues;
The responses from the message boxes are actually integer numbers not strings

You have to close the case statements separately , not just the once - it's easier to see if you indent the various stages;
Code:
    Dim Answer As Integer

    If MsgBox("Did you Select Client Contact Service from Type of Service?", vbQuestion + vbYesNo) = vbYes Then

        Select Case Answer

            Case vbYes
                If MsgBox("Did you Check Sample Checkbox?", vbQuestion + vbYesNo) = vbYes Then
      
                    Select Case Answer
        
                        Case vbYes
                            DoCmd.RunCommand acCmdSaveRecord
                            DoCmd.RunCommand acCmdClose
                        
                        Case vbNo
                            MsgBox ("Make sure you check Counselling and Therapy Checkbox!")
                    
                    End Select                       '(hopefully this leaves the form open and can then be checked before closing.)
            
                End If
            Case vbNo
                DoCmd.RunCommand acCmdClose
        End Select
    End If
 

CJ_London

Super Moderator
Staff member
Local time
Today, 01:53
Joined
Feb 19, 2013
Messages
16,553
please surround your code with the code buttons (highlight and click the # button) to preserve indenting. Otherwise makes it difficult to read.

Select Case Answer
you have not assigned a value to answer - and answer needs to be declared as integer or long. However you can simply use

Code:
if msgbox(….)=vbYes then
    do something vbYes related
else
    do something not vbYes related
end if
Or you would use

Code:
select case msgbox(….)
    case vbYes
        do something vbYes related
    case vbNo
        do something vbNo related
    case

end select
 

Micron

AWF VIP
Local time
Yesterday, 21:53
Joined
Oct 20, 2018
Messages
3,476
Why pester someone who has provided all of the necessary data with these questions? Not only will it quickly become annoying, the tendency will eventually be to dismiss the question and move on regardless as it will become a habit. Thus one can proceed without providing the info. IMHO, you ought to be validating that the required info was provided, prompt only if it is missing and if so, prevent advancement through the process.
 
Last edited:

BettyWho

Registered User.
Local time
Yesterday, 18:53
Joined
Jun 7, 2013
Messages
37
please surround your code with the code buttons (highlight and click the # button) to preserve indenting. Otherwise makes it difficult to read.

you have not assigned a value to answer - and answer needs to be declared as integer or long. However you can simply use


Hi Minty I am slightly confused what is the value I need to provide? I've tried various variations of the vbYes/Vbno but I in all honesty have no idea what I have not done. Could you put it in laymans terms for me? :( Thanks in advance!! I think my brain might be going to melt slightly
 

Minty

AWF VIP
Local time
Today, 01:53
Joined
Jul 26, 2013
Messages
10,355
I'll try - the responses from a message box are actually returned as numbers, however to save you trying to remember what the numbers are Access conveniently assigns a user friendly VBA constant to them. Some of them are;
Code:
vbOK		1	OK button pressed
vbCancel	2	Cancel button pressed
vbAbort		3	Abort button pressed
vbRetry		4	Retry button pressed
vbIgnore	5	Ignore button pressed
vbYes		6	Yes button pressed
vbNo		7	No button pressed

The full list is here :https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-constants

So instead of you having to remember that Yes = 6 you can simply refer to vbYes.

So taking on board that you code can more simply be;
Code:
If MsgBox("Did you Select Client Contact Service from Type of Service?", vbQuestion + vbYesNo) = vbYes Then

        If MsgBox("Did you Check Sample Checkbox?", vbQuestion + vbYesNo) = vbYes Then
      
            DoCmd.RunCommand acCmdSaveRecord
            DoCmd.RunCommand acCmdClose
        Else
            MsgBox ("Make sure you check Counselling and Therapy Checkbox!")
             
        End If  '(hopefully this leaves the form open and can then be checked before closing.)
            
    Else
        DoCmd.RunCommand acCmdClose
        
    End If

However I'm with Micron on this - prompt only when information is missing, and required, and/ or simply add one message - "Your Record is saved , the form will now close!"
 

Micron

AWF VIP
Local time
Yesterday, 21:53
Joined
Oct 20, 2018
Messages
3,476
Should have paid more attention to prior post. I recalled there was a statement prior that numbers had to be used, but I saw that on my phone outside of this session. As a new member, not sure if I should be removing my verbal diarrhea or doing what I'm doing here. As such, I'll leave what I posted for now:


I don't agree that the numbers have to be used when testing or assigning to a result. Try this
Code:
Private Sub testConstants()
Dim result As Integer

result = MsgBox("yes or no", vbYesNo)
If result = vbYes Then MsgBox "yes is " & result
If result = vbNo Then MsgBox "No is " & result

End Sub
Two If's rather than one with Else ensures that for such a simple test, there is no message if neither is true. Just trying to keep it simple.
 

Users who are viewing this thread

Top Bottom