Enable a ComboBox using VBA

  • Thread starter Thread starter TonyK
  • Start date Start date
T

TonyK

Hi All

I have a combo box in a protected MS Word form which I want to enable only
if a particular entry in another combo box is equal to "X". I would
appreciate help with a piece of code to do this.
Many thanks in anticipation.
 
I would recommend that you use dropdown form fields - see
http://gregmaxey.mvps.org/Linked_DropDown_Fields.htm which demonstrates how
to populate one DD field based on the result of another. If you want to
disable the other field entirely (in the following example when 'D' is
selected) you need an extra line for each case then a few more lines at the
end to enable or disable the field

Sub OnExitDDListA()
Dim oDD As DropDown
Dim bEnable As Boolean
Set oDD = ActiveDocument.FormFields("SecondaryDD").DropDown
'Clear previous list
oDD.ListEntries.Clear
'Repopulate list based on user selection
Select Case ActiveDocument.FormFields("PrimaryDD").Result
Case "A"
With oDD.ListEntries
.Add "Apples"
.Add "Apricots"
.Add "Artichokes"
End With
bEnable = True
Case "B"
With oDD.ListEntries
.Add "Blueberries"
.Add "Beets"
.Add "Brocolli"
End With
bEnable = True
Case "C"
With oDD.ListEntries
.Add "Cherries"
.Add "Celery"
.Add "Cilantro"
End With
bEnable = True
Case "D"
bEnable = False
End Select
With ActiveDocument
.Unprotect Password:=""
.FormFields("SecondaryDD").Select
With Dialogs(wdDialogFormFieldOptions)
.Enable = bEnable
.Execute
End With
.Protect NoReset:=True, Password:="", _
Type:=wdAllowOnlyFormFields
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Excellent - thanks Graham
--
TonyK


Graham Mayor said:
I would recommend that you use dropdown form fields - see
http://gregmaxey.mvps.org/Linked_DropDown_Fields.htm which demonstrates how
to populate one DD field based on the result of another. If you want to
disable the other field entirely (in the following example when 'D' is
selected) you need an extra line for each case then a few more lines at the
end to enable or disable the field

Sub OnExitDDListA()
Dim oDD As DropDown
Dim bEnable As Boolean
Set oDD = ActiveDocument.FormFields("SecondaryDD").DropDown
'Clear previous list
oDD.ListEntries.Clear
'Repopulate list based on user selection
Select Case ActiveDocument.FormFields("PrimaryDD").Result
Case "A"
With oDD.ListEntries
.Add "Apples"
.Add "Apricots"
.Add "Artichokes"
End With
bEnable = True
Case "B"
With oDD.ListEntries
.Add "Blueberries"
.Add "Beets"
.Add "Brocolli"
End With
bEnable = True
Case "C"
With oDD.ListEntries
.Add "Cherries"
.Add "Celery"
.Add "Cilantro"
End With
bEnable = True
Case "D"
bEnable = False
End Select
With ActiveDocument
.Unprotect Password:=""
.FormFields("SecondaryDD").Select
With Dialogs(wdDialogFormFieldOptions)
.Enable = bEnable
.Execute
End With
.Protect NoReset:=True, Password:="", _
Type:=wdAllowOnlyFormFields
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Hi Graham

I thought I had it from your last post. This is my code but it does not
seem to work. This code is run when I select the FormField called
"ClinicType1".
Sub EnableCandABox2()
Dim bEnable As Boolean
Dim Check As String
Check = ""

With ActiveDocument
.FormFields("Specialty").Select
Check = .FormFields("Specialty").Result
If Check = "Children's & Adolescent Services" Then
.Unprotect Password:=""
.FormFields("ClinicType1").Select
With Dialogs(wdDialogFormFieldOptions)
.Enable = bEnable
.Execute
End With
.Protect NoReset:=True, Password:="", Type:=wdAllowOnlyFormFields
End If
End With
End Sub
Any help would be appreciated.
Thanks
 
You haven't set the value of bEnable and you don't need to select a field to
read its value. If you set the value of bEnable to false you need also to
set it to true when other options are selected.

Sub EnableCandABox2()
Dim bEnable As Boolean
Dim Check As String
Check = ""
With ActiveDocument
Check = .FormFields("Specialty").Result

If Check = "Children's & Adolescent Services" Then
bEnable = False
Else
bEnable = True
End If

.Unprotect Password:=""
.FormFields("ClinicType1").Select
With Dialogs(wdDialogFormFieldOptions)
.Enable = bEnable
.Execute
End With
.Protect NoReset:=True, Password:="", Type:=wdAllowOnlyFormFields
End With
End Sub

Neither have you cleared the values from ClinicType1 so it will always
display the default value, even though you will not be able to access it.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Back
Top