PDA

View Full Version : MSG Boxes



ajhez
07-21-2015, 07:28 AM
Potentially stupid question here...

I am creating a userform for users to populate a document.

I want to use MSG boxes to stop a the form being submitted (and the various associated actions from occurring) until certain fields have been chosen.

E.g. there is a textbox for a name to be entered. This is absolutely required so I do not want the form to start and make any changes to the document until that textbox has a value within it. Similarly there are sections where the user needs to make a choice by selecting one or more checkboxes - if no checkboxes are selected then the form should not start.

Currently i have this linked to my OK command button - so i can get the boxes to come "Please Enter Name" but once all of the MSG boxes have been OK'd the form carries on and runs anyway.

What mistake am I making here? :think:

Cheers for any help!
AJHEZ

Paul_Hossler
07-21-2015, 08:06 AM
The Click event for the UF exit button could look something like this




Option Explicit

Private Sub ExitButton_Click()
With Me

If .CheckBox1.Value = False Then
MsgBox "Nothing in Check Box 1"
ElseIf .CheckBox2.Value = False Then
MsgBox "Nohting in Check Box 2"
ElseIf Len(.TextBox1.Text) = 0 Then
MsgBox "Textbox cannot be empty"
Else
.Hide
Unload Me
End If

End With
End Sub

ajhez
07-21-2015, 10:24 AM
Cheers, Paul! That has worked for the general exclamation msg boxes I have used.

However, I also want to use a 'Yes No' msg box in the same way. It will be used to ask a question "You have not selected a section. Are you sure?" if the user selects 'Yes' then I'm happy for them to proceed and the userform generates the document, however if they select 'No' I want to go back to the form to allow them to make their change(s).

Any ideas?

Thanks,
AJHEZ

Paul_Hossler
07-21-2015, 01:48 PM
I'd do something like this

Macro drv2 drives a little demo




Option Explicit
Private Sub UserForm_Initialize()
Me.OptionButton3.Value = True
End Sub

Private Sub ExitButton_Click()
Dim eResponse As VbMsgBoxResult
Dim bDataOK As Boolean

bDataOK = False

With Me

If .OptionButton1.Value Then
If Len(.TextBox1.Text) > 0 Then
bDataOK = True

Else
eResponse = _
MsgBox("Option 1 is selected, BUT there is no text entered in the Textbox" & vbCrLf & vbCrLf & _
"Click [Yes] to correct it, or [No] to continue the macro", vbQuestion + vbYesNo, "Another Demo")
End If
If eResponse = vbNo Then bDataOK = True

ElseIf .OptionButton2.Value Then
Call MsgBox("You selected Option 2, but only Option 1 works", vbCritical + vbOKOnly, "Another Demo")

ElseIf .OptionButton3.Value Then
Call MsgBox("You selected Option 3, but only Option 1 works", vbCritical + vbOKOnly, "Another Demo")

End If


If bDataOK Then
.Hide
Unload Me
End If

End With
End Sub