Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Message Box Response Action

Status
Not open for further replies.

jrobin5881

Technical User
Mar 10, 2004
228
US
Good Morning,

I have a user form with several command buttons on the bottom and two option buttons. When my user clicks a button I am checking to make sure that they've made at least one selection in the option buttons before continuing on with the program. I wrote the sub below and inserted into the topline of each of the command button Click event. It works fine in that the message pops if the user has not selected any of my option buttons but my problem is that it the code continues on in the Click event after running the sub routine. My question is how do I get the code to stay in the Optcheck sub without moving back to the click event and until my user selects a option button? Do I need a looping statement? and if so where?


Code:
Private Sub Optcheck()

    Dim response As Long
  
    If opt1.Value = False And opt2.Value = False Then _
    
    response = MsgBox("Please select a reporting period", vbRetryCancel + vbCritical + vbDefaultButton1, "Performannce Planning Tool")

    If response = vbCancel Then
    Exit Sub
    
    ElseIf response = vbRetry Then
    
    frmMain.fra1.SetFocus
                
                
    End If
    
    End If

End Sub
 
What about this ?
Code:
...
If opt1.Value = False And opt2.Value = False Then
  response = MsgBox("Please select a reporting period", vbRetryCancel + vbCritical + vbDefaultButton1, "Performannce Planning Tool")
  If response = vbRetry Then
    frmMain.fra1.SetFocus
  End If
  Exit Sub
End If
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

No its still not working. When I click retry I want it to move the focus over to the option buttons and stop the code until my user makes a selection.

The code runs through the check and when I click the retry button it falls out of the optionchk sub and drops down to the next line in my command button.

 
Change OptCheck to a function so it passes a return value based on what's happened in the function. You can then check for that in your button click (i.e.
Code:
ret = Optcheck

If ret <> 0 then
..run the rest of the code
)

Only run the rest of the button click if the value is a value you want (using an if/select).

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
drops down to the next line in my command button
What is YOUR actual code ?
In my suggestion the next line is 'Exit Sub'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Harley,

Okay I see what your getting at. I did some reading on the Funtion Procedure. My question now becomes how do I pass a value to ret? Do I put a variable in the Optcheck function that says ret = response?
 
PHV,

Here's the whole coding. The click event fires everything off.


When I click" Retry in the messagebox the code drops backdown below to the line Select Case line in the cmd1_Click() sub.

I need it to stop at the fra1 control so that I can make a option selection then re-test to make sure the user selected an option and then continue on with the code. If the re-test fails the message box should re-appear.


Code:
Private Sub Optcheck()
    Dim response As Long

    If opt1.Value = False And opt2.Value = False Then _
    
    response = MsgBox("Please select a reporting period", vbRetryCancel + vbCritical + vbDefaultButton1, "Performannce Planning Tool")

    If response = vbCancel Then
    Exit Sub
    
    ElseIf response = vbRetry Then
    
    frmMain.fra1.SetFocus
                
                
    End If
    
    End If

End Sub









Private Sub cmd1_Click()
    
Optcheck

    Select Case frmMain.opt1
    
    Case Is = True
    
        Frm204.MultiPage1.Pages(0).txt2041.SetFocus
        
        Frm204.MultiPage1.Pages(1).Visible = False
    
    Case Is = False
     
       
        Frm204.MultiPage1.Pages(1).txt2045.SetFocus
        
        Frm204.MultiPage1.Pages(0).Visible = False
        
        End Select
    
   
    Frm204.Show
   
End Sub







 
Your code is very different from my suggestion ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

I can't get your solution to work either. Here's the coding that I tried with yours.



Code:
Private Sub Optcheck()

If opt1.Value = False And opt2.Value = False Then
  response = MsgBox("Please select a reporting period", vbRetryCancel + vbCritical + vbDefaultButton1, "Performannce Planning Tool")
  If response = vbRetry Then
    frmMain.fra1.SetFocus
  End If
  Exit Sub
End If
End Sub



Private Sub cmd1_Click()
    
Optcheck

    Select Case frmMain.opt1
    
    Case Is = True
    
        Frm204.MultiPage1.Pages(0).txt2041.SetFocus
        
        Frm204.MultiPage1.Pages(1).Visible = False
    
    Case Is = False
     
       
        Frm204.MultiPage1.Pages(1).txt2045.SetFocus
        
        Frm204.MultiPage1.Pages(0).Visible = False
        
        End Select
    
   
    Frm204.Show
   
End Sub
 
Ok, I see now.
Code:
Private Function Optcheck() As Boolean
Optcheck = True
If opt1.Value = False And opt2.Value = False Then
  response = MsgBox("Please select a reporting period", vbRetryCancel + vbCritical + vbDefaultButton1, "Performannce Planning Tool")
  If response = vbRetry Then
    frmMain.fra1.SetFocus
  Else
    Optcheck = False
  End If
End If
End Function

Private Sub cmd1_Click()
If Optcheck Then
  Select Case frmMain.opt1
    Case Is = True
      Frm204.MultiPage1.Pages(0).txt2041.SetFocus
      Frm204.MultiPage1.Pages(1).Visible = False
    Case Else
      Frm204.MultiPage1.Pages(1).txt2045.SetFocus
      Frm204.MultiPage1.Pages(0).Visible = False
  End Select
  Frm204.Show
End If
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

It's still not working but I think we're getting closer...

I followed the coding using the F8 key and found the following: The click event throws the coding up to the Optcheck function. The Optcheck variable is changed to True. The testing of the option box values is done and both are found to be false. The message box appears and I hit the Retry button. The code then drops to frmMain.fra1.SetFocus. Next it drops back out of the Function Procedure to the Click event to the line just below Optcheck = True and continues on with the code.

I feel like something is missing it the Optcheck = True statement (highlighted in red in sample).

What do you think?

Code:
Private Function Optcheck() As Boolean
Optcheck = True
If opt1.Value = False And opt2.Value = False Then
  response = MsgBox("Please select a reporting period", vbRetryCancel + vbCritical + vbDefaultButton1, "Performannce Planning Tool")
  If response = vbRetry Then
    frmMain.fra1.SetFocus
  Else
    Optcheck = False
  End If
End If
End Function

Private Sub cmd1_Click()
[COLOR=red]If Optcheck Then[/color]
  Select Case frmMain.opt1
    Case Is = True
      Frm204.MultiPage1.Pages(0).txt2041.SetFocus
      Frm204.MultiPage1.Pages(1).Visible = False
    Case Else
      Frm204.MultiPage1.Pages(1).txt2045.SetFocus
      Frm204.MultiPage1.Pages(0).Visible = False
  End Select
  Frm204.Show
End If
End Sub
 
OK, I misunderstood.
Perhaps this ?
Code:
Private Function Optcheck() As Boolean
Optcheck = True
If opt1.Value = False And opt2.Value = False Then
  response = MsgBox("Please select a reporting period", vbRetryCancel + vbCritical + vbDefaultButton1, "Performannce Planning Tool")
  If response = vbRetry Then
    frmMain.fra1.SetFocus
  End If
  Optcheck = False
End If
End Function

Private Sub cmd1_Click()
If Optcheck Then
  Select Case frmMain.opt1
    Case Is = True
      Frm204.MultiPage1.Pages(0).txt2041.SetFocus
      Frm204.MultiPage1.Pages(1).Visible = False
    Case Else
      Frm204.MultiPage1.Pages(1).txt2045.SetFocus
      Frm204.MultiPage1.Pages(0).Visible = False
  End Select
  Frm204.Show
End If
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

That's It!!! Thanks so much for hanging in there with me. I appreciate your help and I learned alot from this project so far.

Thanks again.
 
but my problem is that it the code continues on in the Click event after running the sub routine. "

Just as a possible further suggestion, you may want to read up on the concept of Scope.

As a possible alternative design, I would - if I understand what you are doing - simply toss the user back to the option buttons. However, I would have a Label with the Option buttons (or you could change the frame caption perhaps). Normally, the Label could be blank, but if both option buttons are nothing....
Code:
Sub cmd1_Click()
If opt1.Value = False And opt2.Value = False Then
    lblOptButton.Caption = _
        "A reporting period must be selected."
    frmMain.fra1.SetFocus
    Exit Sub
Else
   '  the other stuff if a option button IS selected
End If
End Sub

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top