Click here to Skip to main content
16,016,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Public 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.OLEDB.4.0;" & "Data Source=" & Application.StartupPath & "\Attendance.mdb")
rst.Open("SELECT DISTINCT Department FROM Attendance;")
cnn.adOpenStatic()

rst.MoveFirst()

With Me.ComboBox1
.Items.Clear()

Do

.Items.Add(rst![Departments])
rst.MoveNext()
Loop Until rst.EOF
End With
UserForm_Initialize_Exit:
On Error Resume Next
rst.Close()
cnn.Close()
rst = Nothing
cnn = Nothing
Exit Sub
UserForm_Initialize_Err:
MsgBox(Err.Number & vbCrLf & Err.Description, vbCritical, "Error!")
Resume UserForm_Initialize_Exit

What I have tried:

.Items.Add(rst![Departments])
changed this .Items.Add(rst.fields())
Posted
Updated 7-Aug-16 15:30pm

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

With the debugger ensure that your code do what you expect, ensure that the data is what you think and narrow the research.
 
Share this answer
 
can you change this :

rst.Open("SELECT DISTINCT Department FROM Attendance;")
cnn.adOpenStatic()

to this:
cnn.adOpenStatic()
rst.Open("SELECT DISTINCT Department FROM Attendance;")
 
Share this answer
 
Comments
Member 12674139 8-Aug-16 1:03am    
I tried above solution but still the list does not show up in combobox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900