How to Autofill Textboxes on a Form using a Loop?

混江龙づ霸主 提交于 2019-12-19 10:52:46

问题


So I have a table that has a list of totals im trying to display on a form, I have 10 totals I need to get from the totals table and display in 10 textboxes on the form.

The 10 textboxes are "A1, A2, A3..." and its using DLookup to find the ID field number.

It seems like its a syntax issue with Me.TEXTX & X1.Value though I'm not sure how else I can type it.

Hope this makes sense. Thanks!

Private Sub UPDATETOTALS()
    Dim FORMX As String
    FORMX = "GRID"

    Dim TEXTX As String
    TEXTX = "A"

    Dim TABLENAMEx As String, FINDFIELDx As String, GETFIELDx As String
    TABLENAMEx = "GRID_TOTALS"
    FINDFIELDx = "[ID]="
    GETFIELDx = "TODAY"

    Dim X1 As Integer
    For X1 = 1 To 10
        Me.TEXTX & X1.Value = DLookup(GETFIELDx, TABLENAMEx, FINDFIELDx & X1)
    Next X1
End Sub

回答1:


You cannot access an object reference directly using a concatenated string, as such reference is not of string data type.

Instead, you will need to access the object from the relevant collection (in this case, the Controls collection), by supplying the name of the object (as a string) to the Item method of that collection.

Since the Item method is the default method for a collection, the item name can immediately follow the collection as an argument.

For example:

For X1 = 1 To 10
    Me.Controls(TEXTX & X1).Value = DLookup(GETFIELDx, TABLENAMEx, FINDFIELDx & X1)
Next X1


来源:https://stackoverflow.com/questions/54498400/how-to-autofill-textboxes-on-a-form-using-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!