white_flag
09-27-2013, 05:58 AM
Hello
I have an user form in excel file. I like to know how can I populate some textboxes based on a combobox selection.
The combobox is populated from a access database. (see attachment)
I an new to sql queries so a bit of help will be appreciated.
Thank you!
ps. I try to upload the files but it is not going.
I have an userform in excel with 5 textboxes + 1 combobox
my code here:
Option Explicit
Private Sub ComboBox1_Change()
PopulateTB Me.TextBox1, "materials", "name", Me.ComboBox1
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub UserForm_Initialize()
PopulateCB Me.ComboBox1, "materials", "name"
End Sub
Sub PopulateCB(ByRef ctl As Control, ByVal strTable As String, ByVal strField As String, Optional ByVal strCriteria As String)
Dim strSql As String
Dim rs As Recordset
Dim db As database
Set db = OpenDatabase(ThisWorkbook.Path & "\materials.mdb")
strTable = "[" & strTable & "]"
strSql = ""
strSql = strSql & " " & "SELECT DISTINCT" & "[" & strField & "]"
strSql = strSql & " " & "FROM " & strTable
strSql = strSql & " " & "WHERE " & "[" & strField & "]" & " IS NOT NULL"
strSql = strSql & " " & strCriteria
strSql = strSql & " " & "ORDER BY " & "[" & strField & "]"
Set rs = db.OpenRecordset(strSql)
ctl.Clear
Do While Not rs.EOF
ctl.AddItem rs.Fields(strField)
rs.MoveNext
Loop
rs.Close
End Sub
Sub PopulateTB(ByRef ctl As Control, ByVal strTable As String, ByVal strField As String, Optional ByVal strCriteria As String)
Dim strSql As String
Dim rs As Recordset
Dim db As database
Set db = OpenDatabase(ThisWorkbook.Path & "\materials.mdb")
strTable = "[" & strTable & "]"
strSql = ""
strSql = strSql & " " & "SELECT DISTINCT" & "[" & strField & "]"
strSql = strSql & " " & "FROM " & strTable
strSql = strSql & " " & "WHERE " & "[" & strField & "]" & " ="
strSql = strSql & " " & strCriteria
strSql = strSql & " " & "ORDER BY " & "[" & strField & "]"
Set rs = db.OpenRecordset(strSql)
ctl.Clear
Do While Not rs.EOF
ctl.AddItem rs.Fields(strField)
rs.MoveNext
Loop
rs.Close
End Sub
The combobox is populated via PopulateCB
I have error in PopulateTB, error 3061 - Too few parameters. Expected 1
I have an user form in excel file. I like to know how can I populate some textboxes based on a combobox selection.
The combobox is populated from a access database. (see attachment)
I an new to sql queries so a bit of help will be appreciated.
Thank you!
ps. I try to upload the files but it is not going.
I have an userform in excel with 5 textboxes + 1 combobox
my code here:
Option Explicit
Private Sub ComboBox1_Change()
PopulateTB Me.TextBox1, "materials", "name", Me.ComboBox1
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub UserForm_Initialize()
PopulateCB Me.ComboBox1, "materials", "name"
End Sub
Sub PopulateCB(ByRef ctl As Control, ByVal strTable As String, ByVal strField As String, Optional ByVal strCriteria As String)
Dim strSql As String
Dim rs As Recordset
Dim db As database
Set db = OpenDatabase(ThisWorkbook.Path & "\materials.mdb")
strTable = "[" & strTable & "]"
strSql = ""
strSql = strSql & " " & "SELECT DISTINCT" & "[" & strField & "]"
strSql = strSql & " " & "FROM " & strTable
strSql = strSql & " " & "WHERE " & "[" & strField & "]" & " IS NOT NULL"
strSql = strSql & " " & strCriteria
strSql = strSql & " " & "ORDER BY " & "[" & strField & "]"
Set rs = db.OpenRecordset(strSql)
ctl.Clear
Do While Not rs.EOF
ctl.AddItem rs.Fields(strField)
rs.MoveNext
Loop
rs.Close
End Sub
Sub PopulateTB(ByRef ctl As Control, ByVal strTable As String, ByVal strField As String, Optional ByVal strCriteria As String)
Dim strSql As String
Dim rs As Recordset
Dim db As database
Set db = OpenDatabase(ThisWorkbook.Path & "\materials.mdb")
strTable = "[" & strTable & "]"
strSql = ""
strSql = strSql & " " & "SELECT DISTINCT" & "[" & strField & "]"
strSql = strSql & " " & "FROM " & strTable
strSql = strSql & " " & "WHERE " & "[" & strField & "]" & " ="
strSql = strSql & " " & strCriteria
strSql = strSql & " " & "ORDER BY " & "[" & strField & "]"
Set rs = db.OpenRecordset(strSql)
ctl.Clear
Do While Not rs.EOF
ctl.AddItem rs.Fields(strField)
rs.MoveNext
Loop
rs.Close
End Sub
The combobox is populated via PopulateCB
I have error in PopulateTB, error 3061 - Too few parameters. Expected 1