Ryobi
Registered User.
- Local time
- Today, 14:12
- Joined
- Mar 5, 2012
- Messages
- 49
Hello,
I am trying to assign query to combo boxes via VBA. I have about 10 combo boxes which data is a table name supply. All the Combo boxes are name Cbobox1. CboBox2... etc. I want to create procedure that let to assign a query to each combo box. The procedure be call would be something like this Cbox "Suppy", '10", "Select * from Supplies". I have already written the procedure which is shown below. The procedure works except that I can only assign a single value to the Combo boxes. I need something like ctl.value = Strqry which does not work. I have also tried using [Form]![MyForm].[CboBoxName] = Stqry, but I can not get to the pass the name of the combo box even if I use a Gobal variable. I am using Msaccess 2007. Any I on how to resolve this issue ?
Sub CboBox(ByVal CboName As String, ByVal CboNo As Integer, StrQry as string)
Dim ctl As Access.Control
Dim BoxNo As Integer
BoxNo = 1
For Each ctl In Forms!MyForm.Controls
CtlName = CboName & LTrim(Str(BoxNo))
If ctl.Name = CtlName Then
ctl.Value = "Sample" ' Here is where I need to assign the query
BoxNo = BoxNo + 1 ' Used to count the number of Combo Box
If BoxNo > CboNo Then
Exit Sub
End If
End If
Next ctl
I am trying to assign query to combo boxes via VBA. I have about 10 combo boxes which data is a table name supply. All the Combo boxes are name Cbobox1. CboBox2... etc. I want to create procedure that let to assign a query to each combo box. The procedure be call would be something like this Cbox "Suppy", '10", "Select * from Supplies". I have already written the procedure which is shown below. The procedure works except that I can only assign a single value to the Combo boxes. I need something like ctl.value = Strqry which does not work. I have also tried using [Form]![MyForm].[CboBoxName] = Stqry, but I can not get to the pass the name of the combo box even if I use a Gobal variable. I am using Msaccess 2007. Any I on how to resolve this issue ?
Sub CboBox(ByVal CboName As String, ByVal CboNo As Integer, StrQry as string)
Dim ctl As Access.Control
Dim BoxNo As Integer
BoxNo = 1
For Each ctl In Forms!MyForm.Controls
CtlName = CboName & LTrim(Str(BoxNo))
If ctl.Name = CtlName Then
ctl.Value = "Sample" ' Here is where I need to assign the query
BoxNo = BoxNo + 1 ' Used to count the number of Combo Box
If BoxNo > CboNo Then
Exit Sub
End If
End If
Next ctl