Dynamic inserting into combo box

ismailr

Registered User.
Local time
Today, 02:40
Joined
Oct 16, 2015
Messages
31
Hi,

On a form there are 4 combo boxes. Each combo box will have the multiple list of values. These list of values are uploaded while opening the form from a table. I am using the nested loop to assign the list of values. Like;

Code:
 [SIZE=3][FONT=Calibri]Private Sub Form_Open(Cancel As Integer)[/FONT][/SIZE]
 [SIZE=3][FONT=Calibri]Dim sqltxt As String[/FONT][/SIZE]
 [SIZE=3][FONT=Calibri]Dim rst As Recordset[/FONT][/SIZE]
 [SIZE=3][FONT=Calibri]Dim vQno As Integer[/FONT][/SIZE]
 [SIZE=3][FONT=Calibri]Me.cbxQuestion1.RowSource = Clear[/FONT][/SIZE]
 [SIZE=3][FONT=Calibri]Me.cbxquestion2.RowSource = Clear[/FONT][/SIZE]
 [SIZE=3][FONT=Calibri]Me.cbxquestion3.RowSource = Clear[/FONT][/SIZE]
 [SIZE=3][FONT=Calibri]Me.cbxquestion4.RowSource = Clear[/FONT][/SIZE]
 
 [SIZE=3][FONT=Calibri]For i = 1 To 4[/FONT][/SIZE]
 [FONT=Calibri][SIZE=3]sqltxt = "Select Question from tbQues where QuesNo=" & i[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Set rst = CurrentDb.OpenRecordset(sqltxt)[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]d = rst.RecordCount[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]If rst.EOF Then[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]vNoq = 0[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Else[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]rst.MoveLast[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]vNoq = rst.RecordCount[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]End If[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]For j = 1 To vNoq[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]If i = 1 Then[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Me.cbxQuestion1.AddItem rst!Question[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]ElseIf i = 2 Then[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Me.cbxquestion2.AddItem rst!Question[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]ElseIf i = 3 Then[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Me.cbxquestion3.AddItem rst!Question[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]ElseIf i = 4 Then[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Me.cbxquestion4.AddItem rst!Question[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]End If[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Next j[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]If Not rst.EOF Then rst.MoveNext[/SIZE][/FONT]
 
 [SIZE=3][FONT=Calibri]Next i[/FONT][/SIZE]
 
 [SIZE=3][FONT=Calibri]End Sub[/FONT][/SIZE]
How I can write VBA code to dynamically refer the combo box name with the serial number example me.question&i.additem
Is there any way to concatenate the combo box name with serial number and get properties of combo box? Please guide.
Thanks
Ismail
 
thanks CJ. It worked.
 

Users who are viewing this thread

Back
Top Bottom