Ms Access - VBA - Create shortcuts programmatically with size

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.)

enter image description here

2) The above code produces the following error

enter image description here

+4
source share
2 answers

, , , , .

, , . . , Text1, Text2, Label, Label2. , / ( ).

enter image description here

Private Function ReBindControls()

    Dim rs As DAO.Recordset 'if you are using ADO then replace this with ADODB.Recordset 
    If IsNull(Combo99) Then
        Exit Function
    End If
    Set rs = CurrentDb.OpenRecordset(Combo99) ' If you are using ADO use the appropriate ADO method to open the recordset

    Dim fs As DAO.Fields 'if you are using ADO then replace this with ADODB.Fields
    Set fs = rs.Fields

    Dim f As Integer
    Dim aLabel As Label, aTextBox As TextBox

    Set Me.Recordset = rs

    For f = 0 To fs.Count - 1
        Set aLabel = Controls("Label" & f)
        aLabel.Caption = fs(f).Name
        aLabel.Visible = True

        Set aTextBox = Controls("text" & f)
        aTextBox.ControlSource = fs(f).Name
        aTextBox.Visible = True

        aLabel.Move 1 * 1440, f * 1440 / 2
        aTextBox.Move 2.5 * 1440, f * 1440 / 2
    Next f

End Function


Function clearBindings()
    Dim c As Integer
    Dim aLabel As Label, aTextBox As TextBox

    For c = 0 To maxIndexOfControls
        Set aTextBox = Controls("text" & c)
        aTextBox.ControlSource = ""
        Set aLabel = Controls("Label" & c)
        aLabel.Visible = False
        aTextBox.Visible = False
        aLabel.Move 0, 0
        aTextBox.Move 0, 0

    Next c
End Function

Private Sub Combo99_Change()
    clearBindings
    ReBindControls
End Sub

enter image description here enter image description here

+2

- , , , , VBA (.. set ctrl set ctrl1) . .

NoOfRecords, ALL ctrls ( ) , / .

, . , ctrl CreateControl "left" "top" ( left = 0+ (x * 300), top = 0). x--y, ctrl.

CreateControl: https://msdn.microsoft.com/en-us/library/office/aa221167%28v=office.11%29.aspx

, !

0

Source: https://habr.com/ru/post/1620813/


All Articles