vba - Ms Access - VBA - 以编程方式创建具有大小的标签

标签 vba ms-access ms-access-2010

我正在尝试创建 Labels & Textboxes并根据变量计数 NoOfRecords 动态为其分配一些值(问题是我事先不知道我需要多少个控件。这个数字将由特定表中有多少记录决定)。
我的表单名称是 frmDashboard
我试过的是

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

我在这里面临两个问题
1)如何一个接一个地显示标签和文本框,如下所示
(下一个标签和文本框应该正好在顶部下方。)


enter image description here

2)以上代码抛出以下错误

enter image description here

最佳答案

您看到的错误是正确的,当您最终将数据库作为编译的拆分数据库分发时无法克服。

这里的诀窍是提前创建您可能需要的所有控件。然后您需要标记并在表格上按顺序排列它们。所以喜欢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

关于vba - Ms Access - VBA - 以编程方式创建具有大小的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34363294/

相关文章:

ms-access - 为什么我看到的是缓存的数据而不是更新的数据?

ms-access - 运行时错误 '3001' : Invalid Argument when creating a table in Access VBA

VBA Excel : How to loop through Column B for cells containing alphanumeric or just numeric content?

sql - 在Microsoft Access中使用查询来比较两个字段并查找多个匹配值

xml - 用于 xls 转换的 Excel VBA 编码以及参数

excel - 使用VBA在word中复制表格前面的文本行

ms-access - MS Access 的多对多关系问题

database - Visual Basic 测验 1 题无论如何总是错的

.net - 从 Access 数据库的附件字段中提取文件

vba - 使用路径从外部源将图像添加到表单中