Thank rheitzman
For some strange reason, it only seems to work after pressing F8 and stepping through the lines?
When opening the document for the first time, it just doesn't display anything?
Main Topics
Browse All TopicsHi all -
I have a word document (Word 2003) which has a combobox. I'm trying to get the names from my database (Access 2003) to be populated within the drop down list. The Database resides in my C Drive.
(Full Path; C:\db\Names.mdb)
In the database, I have a table called "tblNames" and within that table, I have a field name called "StaffNames" which its data type is set to "Text".
In my word document, I have entered the following code (Below), and setup the references.
But, the "StaffNames" are not being populated in the word document combobox?
Does anyone know why and perhaps can help me with the coding?
Thank you.
..........................
Private Sub UserForm_Initialize()
On Error GoTo UserForm_Initialize_Err
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OL
"Data Source=C:\db\Names.mdb"
rst.Open "SELECT DISTINCT [StaffNames] FROM tblNames ORDER BY [StaffNames];", _
cnn, adOpenStatic
rst.MoveFirst
With Me.ComboBox1
.Clear
Do
.AddItem rst![StaffNames]
rst.MoveNext
Loop Until rst.EOF
End With
UserForm_Initialize_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
UserForm_Initialize_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume UserForm_Initialize_Exit
End Sub
..........................
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I forgot to include the test code:
Sub x()
Dim frm As New UserForm1
frm.Show
End Sub
Place this sub in the ThisDocument module, place cursor in the code, and hit F5 to run.
It may be that your Initialize code isn't getting executed.
Did you change the .AddItem to use the .Value property?
Try using F5 instead of F8 and see if the form displays with the combobox populated.
Sorry, I forgot to mention that yes, I have changed the .Additem to use the.Value property.
This is how my code now looks;
Sub x()
Dim frm As New UserForm
frm.Show
End Sub
Private Sub UserForm_Initialize()
On Error GoTo UserForm_Initialize_Err
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OL
"Data Source=C:\db\Names.mdb"
rst.Open "SELECT DISTINCT [StaffNames] FROM tblNames ORDER BY [StaffNames];", _
cnn, adOpenStatic
rst.MoveFirst
With Me.ComboBox1
.Clear
Do
.AddItem rst![StaffNames].Value
rst.MoveNext
Loop Until rst.EOF
End With
UserForm_Initialize_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
UserForm_Initialize_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume UserForm_Initialize_Exit
End Sub
Try again to run the code from the ThisDocument module:
Sub x()
Dim frm As New UserForm1
frm.Show
MsgBox frm.ComboBox1.Text & ""
End Sub
You may have a different name for your form instead of UserForm1
The test here is to see if the Initialize event fires on your form. Place a Stop or Breakpoint in the Initialize code if the combobox list isn't getting populated.
Have done this and it just doesn't work. Does using the code I posted above work for you?
I have just created a new document with a combobox and pasted the code in and still it doesn't work unless I manually run the code from the Code View.
If I put your code "sub x()... in the "ThisDocument" modile, I get compile errors.
I posted the above before I saw your next post.
Yes, the combobox is in a Document.
So this is how it looks now; (Still not working though)...
Private Sub Document_New()
On Error GoTo Document_New_Err
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OL
"Data Source=C:\db\Names.mdb"
rst.Open "SELECT DISTINCT [StaffNames] FROM tblNames ORDER BY [StaffNames];", _
cnn, adOpenStatic
rst.MoveFirst
With Me.ComboBox1
.Clear
Do
.AddItem rst![StaffNames].Value
rst.MoveNext
Loop Until rst.EOF
End With
Document_New_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
Document_New_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume Document_New_Exit
End Sub
Business Accounts
Answer for Membership
by: rheitzmanPosted on 2008-07-01 at 10:24:09ID: 21909732
Works fine for me (with modified SQL of course.
Display the code in VBE and use F8 to step through the lines. Make sure the SQL is returning data.
BTW you should leave off the cursor param ( adOpenStatic ) if all you are going to do is read the data from first record to last.
Probably not an issue here but to be safe you should use the ,Value property of the recordset:
.AddItem rst![Last_Name].Value
or
.AddItem rst![Last_Name] & ""
otherwise there are cases where the recordset object and not the value will be assigned. This is a common problem with collections. Perhaps you version of Word has this problem with combobox collections (I'm using 2002).