title: Ms Access - VBA - Create Labels Programatically with Size
body: <p>I am trying to create <code>Labels &amp; Textboxes</code> and assign some values to it dynamically, depending on the variable count <code>NoOfRecords</code>(The problem is that I don't know in advance how many controls I'll need. The number will be driven by how many records there are in a particular table).
  My Form name is <code>frmDashboard</code></p>
  
  <p><strong>What I tried is</strong></p>
  
  <pre><code>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_" &amp; x
         Controls("lblDynamic_control_" &amp; x).Caption = x
          Set ctrl1 = CreateControl("frmDashboard", acTextBox, acDetail, , "", 0 + (x * 300), 0, 300, 240)
          ctrl1.ControlName = "txtDynamic_control_" &amp; x  
         Controls("txtDynamic_control_" &amp; x).Value= x
      Next x
  </code></pre>
  
  <p><strong>There are 2 issues i am facing here</strong><br><br>
  <strong>1) How to show label and textboxes one after another like below 
  (Next Label and Textbox should be exactly below the top.)</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Z8LYo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z8LYo.png" alt="enter image description here"></a></p>
  
  <p><strong>2) The above code throws the following error</strong></p>
  
  <p><a href="https://i.stack.imgur.com/qA0Mm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qA0Mm.png" alt="enter image description here"></a></p>