populating combobox using VBA
Need help please, i am getting error 80020005 (type mismatch), getting at rs.MoveNext. Also how do i connect to .accdb, this code dosent work then.
My Code:
Private Sub UserForm_Initialize()
Set con = CreateObject("ADODB.connection")
Set rs = CreateObject("ADODB.recordset")
Set cmd = CreateObject("ADODB.command")
con.Provider = ("Microsoft.Jet.OLEDB.4.0;Data Source=D:\Documents and Settings\Tool.mdb;Persist Security Info=False")
con.Open "D:\Documents and Settings\Tool.mdb"
'rs.Open "select * from Table1 where date between # " & dta & " # and # " & dtb & " #", con
rs.Open "select DISTINCT [Region] from Main", con
'Sheet1.Range("a2").CopyFromRecordset (rs)
rs.MoveFirst
With Me.ComboBox1
.Clear
Do
.AddItem rs![Region]
rs.MoveNext
Loop Until rs.EOF
End With
UserForm_Initialize_exit:
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Exit Sub
End Sub
Need help please, i am getting error 80020005 (type mismatch), getting at rs.MoveNext. Also how do i connect to .accdb, this code dosent work then.
My Code:
Private Sub UserForm_Initialize()
Set con = CreateObject("ADODB.connection")
Set rs = CreateObject("ADODB.recordset")
Set cmd = CreateObject("ADODB.command")
con.Provider = ("Microsoft.Jet.OLEDB.4.0;Data Source=D:\Documents and Settings\Tool.mdb;Persist Security Info=False")
con.Open "D:\Documents and Settings\Tool.mdb"
'rs.Open "select * from Table1 where date between # " & dta & " # and # " & dtb & " #", con
rs.Open "select DISTINCT [Region] from Main", con
'Sheet1.Range("a2").CopyFromRecordset (rs)
rs.MoveFirst
With Me.ComboBox1
.Clear
Do
.AddItem rs![Region]
rs.MoveNext
Loop Until rs.EOF
End With
UserForm_Initialize_exit:
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Exit Sub
End Sub
Last edited: