Hiya,
I've come up with some code that goes and gets a recordset from ms access and I wanted to put the values into a list box or combo box.
This seems to just hang up the program (excel) and crash. Can anyone suggest where I'm going wrong?
Is it the repetition control loop that I'm doing wrong, in which case is it the 'Field' reference to rs.Fields or is it the AddItem (does this not apply in this case?) because I couldn't see how to use RowSource because I thought that only applies to worksheet ranges.
Please help!?
I've come up with some code that goes and gets a recordset from ms access and I wanted to put the values into a list box or combo box.
This seems to just hang up the program (excel) and crash. Can anyone suggest where I'm going wrong?
Code:
Public Sub PopulateListBox(rectype As Integer)
'Populate the list box with the correct set of values
Dim con As ADODB.Connection
Dim thisSQL As String
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
'Evaluate the args recieved from calling method
Select Case rectype
Case 1:
thisSQL = "SELECT DISTINCT Client FROM Sender WHERE " _
& "RecType = 1"
Case 2:
thisSQL = "SELECT DISTICT Client FROM Sender WHERE " _
& "RecType = 2"
Case Else
thisSQL = ""
End Select
'Get the connection
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& "D:\Data\LIVE\oppdagelse.mdb"
con.Open
'con.BeginTrans
'Execute query to return as resultset
Set rs = con.Execute(thisSQL)
'Add unique account refs from the resultset to the listbox in frm1
Do While Not rs.EOF
For Each Field In rs.Fields
UserForm1.UltimateListBox.AddItem
Next
Loop
con.Close
End Sub
Please help!?