PDA

View Full Version : Userform needs Mandatory input



JC501
12-12-2010, 03:02 AM
Hi, I have a Userform with 3 check boxes, 2 text boxes and 3 combo boxes.

When check box 3 is checked I want to ensure that combobox 1 is actioned.

Text box 1 is also mandatory but not text box 2.

Combo box 2 is not mandatory. Combo box 3 is.

I have used message boxes to try to prompt people to fill in but they just ignore them!!

VBA

Private Sub CommandButton1_Click()
Selection.GoTo What:=wdGoToBookmark, Name:="Manager"

If CheckBox1.Value = True Then
Selection.TypeText Text:="Yes"
Else
Selection.TypeText Text:=""
End If

Selection.GoTo What:=wdGoToBookmark, Name:="Depthead"

If CheckBox2.Value = True Then
Selection.TypeText Text:="Yes"
Else
Selection.TypeText Text:=""
End If

Selection.GoTo What:=wdGoToBookmark, Name:="AuthSig"

If CheckBox3.Value = True Then
Selection.TypeText Text:="Yes"
Else
Selection.TypeText Text:=""
End If

Application.ScreenUpdating = True
Selection.GoTo What:=wdGoToBookmark, Name:="Location1"
Selection.Text = UserForm4.TextBox1.Text

Selection.GoTo What:=wdGoToBookmark, Name:="Dept"
Selection.Text = UserForm4.TextBox2.Text


With ActiveDocument

.Bookmarks("Authsigtype").Range.Text = ComboBox1.Value
.Bookmarks("Floor").Range.Text = ComboBox2.Value
.Bookmarks("Service").Range.Text = ComboBox3.Value

End With

Unload UserForm4

End Sub
Private Sub CommandButton2_Click()
End
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
With ComboBox1

.AddItem "Up to £1,000"
.AddItem "Up to £5,000"
.AddItem "Up to £10,000"
.AddItem "Up to £20,000"

End With
With ComboBox2

.AddItem "Basement"
.AddItem "Ground"
.AddItem "1st Floor"
.AddItem "2nd Floor"
.AddItem "3rd Floor"
.AddItem "4th Floor"

End With

With ComboBox3
.AddItem "Helpdesk"
.AddItem "Information"
.AddItem "Contact Centre"
.AddItem "Lending"
.AddItem "Insurance"
.AddItem "Arrears"

End With
End Sub

gmaxey
12-12-2010, 07:52 AM
Why don't you:
CommandButton1.Enabled = False

in your initialization even and then add CommandButton1.Enabled = True
statements in Control Change event procedures when your conditions are met.

JC501
12-12-2010, 11:39 AM
Thank you for taking the time to reply to me. Unfortunately, I do not understand your instruction, or where I should put the command. I am trying to put in but returning errors. Hoping that if I perserve enough, I will get there!!

gmaxey
12-12-2010, 12:01 PM
See the thread in this forumn "Listbox - force selection" for an example.

fumei
12-13-2010, 10:07 AM
JC501, let us know if you can not figure it out from the other thread.

Essentially you disable the commandbuttons until the conditions you require ARE met.

Are you possibly needing any repeat of actions inserting text at the bookmark? If so, you may want to reconsider:
.Bookmarks("Authsigtype").Range.Text = ComboBox1.Value
This deletes the bookmark, so it can never be used again. This may, or may not be an issue. There are ways to retain the bookmark.

Also, while not wrong, it is a good practice to use the value of what you want. You want the text from the ComboBox...yes?

Then use:
.Bookmarks("Authsigtype").Range.Text = ComboBox1.Text


Yes, in this case .Value and .Text return the same string. However, what .Value actually returns is the default property of the control, whatever it is. And it is possible, when you are using other controls, that you do not know what the default property is...and get a surprise.

My point being is that if you want TEXT, ask for TEXT.