I am trying to create Labels & Textboxesand assign it some values dynamically, depending on the variable count NoOfRecords(The problem is that I do not know in advance how many controls I will need. Due to how many records there are in a particular table). My form namefrmDashboard
I tried
Set cNN = Nothing
Set rsfnum = Nothing
Dim strconnfnum As String
Set cNN = CurrentProject.Connection
Set rsfnum = New ADODB.Recordset
strconnfnum = "SELECT nz(employeename,'') as employeename from employees"
rsfnum.Open strconnfnum, cNN, adOpenKeyset, adLockOptimistic
'Number of Records in Employees tables
NoOfRecords = rsfnum.RecordCount
For x = 1 To NoOfRecords
Set ctrl = CreateControl("frmDashboard", acLabel, acDetail, , "", 0 + (x * 300), 0, 300, 240)
ctrl.ControlName = "lblDynamic_control_" & x
Controls("lblDynamic_control_" & x).Caption = x
Set ctrl1 = CreateControl("frmDashboard", acTextBox, acDetail, , "", 0 + (x * 300), 0, 300, 240)
ctrl1.ControlName = "txtDynamic_control_" & x
Controls("txtDynamic_control_" & x).Value= x
Next x
There are 2 problems that I encountered here
1) How to show shortcuts and text fields one by one, as shown below (The next label and text field should be exactly below the top.)

2) The above code produces the following error

source
share