Dear all,
I am linking my Access Database to a UserForm in Word and have written below code. When I test the macro there are no errors, but the ComboBox is empty. Can anyone tell me what I'm doing wrong here?
I am linking my Access Database to a UserForm in Word and have written below code. When I test the macro there are no errors, but the ComboBox is empty. Can anyone tell me what I'm doing wrong here?
Code:
Private Sub UserForm1_Initialize()
On Error GoTo UserForm1_Initialize_Err
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\ClientDatabase\ClientDatabase.mdb"
rst.Open "SELECT DISTINCT [ClientName] FROM tblClients ORDER BY [ClientName];"
cnn , adOpenStatic
rst.MoveFirst
With Me.ComboBox1
.Clear
Do
.AddItem rst![ClientName]
rst.MoveNext
Loop Until rst.EOF
End With
UserForm1_Initialize_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
UserForm1_Initialize_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume UserForm1_Initialize_Exit
End Sub