PDA

View Full Version : ActiveX Combobox Question



junglej815
04-14-2010, 07:20 AM
Hello,

I curerntly have a Word Document that has an ActiveX Combobox on it with the following code...

Public Sub Document_Open()
With Me.ComboBox2
.AddItem "Conference(Default)"
.AddItem "Classroom"
.AddItem "Lecture"
End With

End Sub

...I am adding another ActiveX Combobox with items in it and for some reason I am having a hard time figuring it out and where I put code for the new combobox.

I tried using the same code as stated above but with " Combobox1" and an error comes up as " Ambiguous name detected: Document_open"

I tried putting it inside the same code as above not really knowing what I am doing so that didn't really work.

Any help would be appreciated.

Thanks

lucas
04-14-2010, 08:44 AM
Ambiguous name means there is already a sub with that name in the document.

It would really help if you could post the document without any real information.

click go advanced and then scroll down an look for a button that says manage attachments.

If you are putting the combobox on the sheet while in design view, you should be able to click on it to get to the click code for the control which will give you the name of the combobox or you can change it in the properties by right clicking on the control.

fumei
04-14-2010, 10:20 AM
The following in the ThisDocument code module will populate the comboboxes.

Public Sub Document_Open()
With Me.ComboBox1
.AddItem "Whatever"
.AddItem "Blah"
.AddItem "HooHa"
End With

With Me.ComboBox2
.AddItem "Conference(Default)"
.AddItem "Classroom"
.AddItem "Lecture"
End With

With Me.ComboBox3
.AddItem "The third"
.AddItem "Yoda"
.AddItem "Bare"
End With
End Sub


Things to note.

1. REiteration of a population procedure will ADD new items. If you want to replace items, you need to use .Clear

2. As the code stands, there will be nothing showing in the combobox. To have an item showing, use .ListIndex.

With Me.ComboBox1
.AddItem "Select an item"
.AddItem "Whatever"
.AddItem "Blah"
.AddItem "HooHa"
.ListIndex = 0
End With


The combobox will displays "Select an item". This is commonly done so users can see that there is something there. Then you use logic to determine what (if they did) was selected. For example:

If ComboBox1.Text = "Select an item" Then
MsgBox "Nothing was selected!"
Exit Sub
Else
ActiveDocument.Variables("FromCombo1").Value = _
ComboBox1.Text
End if
If the user has not selected something, they get a message; if they have the item selected is put into the DOCVARIABLE "FromCombo1".

Or whatever.

junglej815
04-15-2010, 06:55 AM
Thanks for the help guys. You're a big help. So I've been working on this document that I have and I think I am just going around in circles with what I want to do and it's kind of driving me a little crazy, I have most of it but I'm getting caught up somewhere. I've attached the document that I am talking about.

This is what I am looking to do ( Its kind of involved and I hope it makes sense):

- Document Opens up and all of the ActiveX Check Boxes are enabled.

- With the "Building" ActiveX ComboBox ( ComboBox1 ) clear, if you check "No Equipment Needed" ( CheckBox15 ) - All of the other checkboxes and textboxes become disabled with "0's" inserted into the textboxes.....When you uncheck "No Equipment Neded" ( CheckBox15 ) all of the other checkboxes and textboxes become enabled and the "0's" go away.

- When you choose "4" in the "Building" ActiveX ComboBox ( ComboBox1 ), the "Video Conferencing" checkbox ( CheckBox7 ) becomes disabled with all of the other checkboxes are enabled. If you then check the "No Equipment Needed" ( CheckBox15) it has the same effect where all of the other boxes become disabled. Then if you were to uncheck "No Equipment Needed" ( CheckBox15 ) everything becomes enabled except for "Video Conferencing" ( CheckBox7 ).

- When you choose "13" in the "Building" ActiveX ComboBox ( ComboBox1 ), the "Riser" checkbox ( CheckBox13 ) becomes disabled with all of the other checkboxes are enabled. If you then check the "No Equipment Needed" ( CheckBox15) it has the same effect where all of the other boxes become disabled. Then if you were to uncheck "No Equipment Needed" ( CheckBox15 ) everything becomes enabled except for "Riser" ( CheckBox13 ).

The problem I'm having is when you uncheck the the "No Equipment Neded" box it doesn't keep the proper checkbox disabled.

- I also was thinking about maybe having the " No Equipment Needed" checkbox ( CheckBox15) pop up a message saying that you need to select a Building if you try to check it without selecting a building.

Below is the code that i have so far...


Private Sub CheckBox15_Change()
If CheckBox15.Value = True Then
CheckBox5.Value = False
CheckBox5.Enabled = False

CheckBox6.Value = False
CheckBox6.Enabled = False

CheckBox7.Value = False
CheckBox7.Enabled = False

CheckBox8.Value = False
CheckBox8.Enabled = False

CheckBox9.Value = False
CheckBox9.Enabled = False

CheckBox10.Value = False
CheckBox10.Enabled = False

CheckBox13.Value = False
CheckBox13.Enabled = False

CheckBox14.Value = False
CheckBox14.Enabled = False

CheckBox11.Value = False
CheckBox11.Enabled = False

CheckBox12.Value = False
CheckBox12.Enabled = False

TextBox10.Value = 0
TextBox10.Enabled = False

TextBox11.Value = 0
TextBox11.Enabled = False

TextBox13.Value = 0
TextBox13.Enabled = False

Else
CheckBox5.Enabled = True
CheckBox6.Enabled = True
CheckBox7.Enabled = True
CheckBox8.Enabled = True
CheckBox9.Enabled = True
CheckBox10.Enabled = True
CheckBox14.Enabled = True
CheckBox11.Enabled = True
CheckBox12.Enabled = True
CheckBox13.Enabled = True
TextBox10.Enabled = True
TextBox11.Enabled = True
TextBox13.Enabled = True
TextBox10.Text = " "
TextBox11.Text = " "
TextBox13.Text = " "

End If
End Sub

Private Sub ComboBox1_Change()
If ComboBox1.Value = " " Then
CheckBox15.Value = False
CheckBox15.Enabled = False
CheckBox13.Enabled = True
CheckBox7.Enabled = True
TextBox10.Text = " "
TextBox11.Text = " "
TextBox13.Text = " "
Else
CheckBox7.Enabled = True
End If

If ComboBox1.Value = "4" Then
CheckBox15.Value = False
CheckBox15.Enabled = True
CheckBox13.Enabled = True
CheckBox7.Enabled = False
TextBox10.Text = " "
TextBox11.Text = " "
TextBox13.Text = " "
Else
CheckBox7.Value = False
CheckBox7.Enabled = False

End If

If ComboBox1.Value = "13" Then
CheckBox15.Value = False
CheckBox15.Enabled = True
CheckBox13.Value = False
CheckBox13.Enabled = False
CheckBox7.Enabled = True
TextBox10.Text = " "
TextBox11.Text = " "
TextBox13.Text = " "

Else
CheckBox15.Enabled = True

End If
End Sub

Private Sub ComboBox2_Change()

End Sub

Public Sub Document_Open()
With Me.ComboBox2
.AddItem " Banquet"
.AddItem "Conference(Default)"
.AddItem "Classroom"
.AddItem "Lecture"
.AddItem "Newsmaker"
End With
With Me.ComboBox1
.AddItem " "
.AddItem "4"
.AddItem "13"
End With

End Sub

Thanks for all the help.

EDIT: VBA TAGS ADDED TO CODE

lucas
04-15-2010, 07:31 AM
I want to comment on the professional appearance of your document and ask why you aren't using a userform and bookmarks so you don't have all the activeX textboxes, etc. to show in a printout?

If this is a document that you generate regularly you could make it into a template and it would be much easier to use and maintain.

just asking the obvious question.

junglej815
04-15-2010, 07:48 AM
Hmm...userform??....bookmarks???...have to say I'm not entirely sure how to go about using those...so sorry but I guess not that obvious..

lucas
04-15-2010, 08:12 AM
You will run into the same problems you are having here but they can be dealt with. I just don't see how you can use what you have as a template.

for dealing with your checboxes try something like this in the
Private Sub CheckBox15_Change()

If ComboBox1.Value = "4" Then
CheckBox7.Enabled = False
Else
CheckBox7.Enabled = True
End If
comment out your other two or three calls to checkbox7 in the same procedure.

lucas
04-15-2010, 08:41 AM
Try this: See attached

fumei
04-15-2010, 10:45 AM
A userform is a displayed dialog, with whatever you want on it. In other words, it would be a dedicated "application" within Word whose purpose is to get the information you want. Once it has it, that information is then written into the document. This:

1. allows logic operations (such as make this enabled/disabled IF that is going);

2. puts ONLY the finished information into the document, thus avoiding those messy looking boxes in the document.

They take a bit of work/effort - especially with something with as many items as you have, but they look and perform much more professionally.

Bookmarks are simply identified and explicit ranges (location) in a document. They are totally defined by the designer.