簡體   English   中英

Access Access - VBA - 以編程方式創建標簽

[英]Ms Access - VBA - Create Labels Programatically with Size

我正在嘗試創建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)如何一個接一個地顯示標簽和文本框,如下所示(下一個標簽和文本框應該正好在頂部下方。)

在此輸入圖像描述

2)上面的代碼拋出以下錯誤

在此輸入圖像描述

您最終將數據庫作為已編譯的拆分數據庫進行分發時,您看到的錯誤是正確的,無法克服。

這里的訣竅是創建你可能需要的所有控件。 您需要標記然后在表單上按順序排列。 就像Text1Text2LabelLabel2 這樣,您可以通過索引遍歷每個文本框/標簽組合(這將是您的記錄集中字段的索引)。

在此輸入圖像描述

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

你可以得到這些結果

在此輸入圖像描述 在此輸入圖像描述

好的 - 免責聲明我在這里是一個完整的新手 - 但我調查了這一點,顯然VBA不會讓你在運行時創建控件(即'set ctrl'或'set ctrl1'命令)。 這些必須在設計時創建,並在運行時根據動態輸入顯示。

如果NoOfRecords有最大值,我可能建議在設計時預編碼所有可能的ctrl(使用循環),並根據運行時的動態輸入使數字變為可見/活動。

至於組織標簽和文本框的布局,這也將作為該設計循環的一部分來完成。 如下面的文檔中所述,每個ctrl的左上角的位置可以在'left'和'top'命令的CreateControl方法中指定(你現在將它保留為left = 0+(x * 300) ,top = 0)。 在迭代ctrl創建時,只需對x和y坐標添加必要的調整。

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

希望有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM