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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate a Combobox in Word?

Status
Not open for further replies.

mustang98

Technical User
Dec 7, 2004
12
US
I need to populate a combobox located in a userform in Word but it must write the information to an Access database so that the next time Word is opened the combobox will be populated with the information that it previously stored in the database. Can someone help me with this?
Thanks
 
What kind of combobox?

If it is a formfield combobox (it is really a dropdown) it can be populated from any code module, as in:

Code:
With ActiveDocument.FormFields("Dropdown1").DropDown.ListEntries
    .Add "Hello"
    .Add "World"
    .Add "My"
    .Add "Name Is "
End With

If it is an ActiveX combobox, then it is populated in the ThisDocument module, as in:

[code}Sub MakeACTX()
Combobox1.AddItem "Hello"
Combobox1.AddItem "World"[/code]

The AddItem method is NOT exposed to other modules. You can not directly populate from other module as ActiveX are document specific. You Call to procedures in the ThisDocument module that will populate an ActiveX control, as in:

Code:
Sub tryme()
ThisDocument.MakeACTX
End Sub

The Sub is in a standard module, but calls a procedure in ThisDocument.

You can directly populate an ActiveX combobox in a standard module, by using the InLineShapes collection (which an ActiveX control is part of).

Also see thread707-931351

Gerry
 
Thank you for the response. I really am not familiar with VBA code and need a little more help. I'm using ActiveX combobox which I have found out how to populate by using DAO but have yet to figure out how to write any new data from the combobox into the database. I'm also using text boxes and would like any data that is entered stored into a database as well.
Here is an example that I found on this forum that helped me a little:

Private Sub ComboBox1_DropButtonClick()

Dim PhoneNumbers() As String
Dim PhoneNumbersEN As Integer
Dim FoundRecords As Boolean

PhoneNumbersEN = 0
FoundRecords = False

Set VodaDB = DBEngine(0).OpenDatabase("d:\Temp\db1.mdb")

Set VodaRS = VodaDB.OpenRecordset("Number")

Do While Not VodaRS.EOF
ReDim Preserve PhoneNumbers(PhoneNumbersEN)
PhoneNumbers(PhoneNumbersEN) = VodaRS!Number
PhoneNumbersEN = PhoneNumbersEN + 1
FoundRecords = True
VodaRS.MoveNext
Loop

ComboBox1.List = PhoneNumbers()

End Sub

I really appreciate your help.
Thanks
 
Not trying to be unhelpful, or sarcastic, but perhaps you should rephrase the question. The post was how to populate a combobox. Apparently you know how to do that, since you wrote that you ARE populating it. Sort of feel I wasted my time with the previous post.

If I undestand correctly, the REAL question is how do you update a database? The fact that it is information from a combobox, or a textbox, is irrelevant. You update records the same way you would update the records taking information froma spreadsheet cell, or text from a paragraph in a document. It is information.

You have a logical problem here. You say you are populating the combobox FROM the database records. OK. Good. You then ask
how to write any new data from the combobox into the database.

A textbox can get new data. Please explain how you can get new data from a combobox? It is true, you could have other code that changes the combobox items - that would give new data. However, you do not state this. A combobox has a list. The user selects from that list. The list remains the same, it does not change. What new data?

Could you check your post? It also seems that the code posted for the Change event is in fact the populating code itself, as it builds an array from the recordset, then makes the list in the combobox that list.

Gerry
 
Ok. I am sorry for confusing you, and I do appreciate the help. I populated the combobox by looking at a database in access, but if the user types something in the combobox I wanted that data to be stored in the database so that is will show up in the list the next time the user clicked on the combobox. The second part of the question is how to enter data from a text box and store it into a database. I know that this sounds simple, but I need actual code to show me how to do this since I am not familiar enough with code to have someone explain to me how.
Thanks
 
users do not type into comboboxes

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top