How can I convert a string to an actual form label caption in Microsoft Access VBA?

随声附和 提交于 2021-02-10 19:54:23

问题


I have attempted to use array values to loop through different scenarios to dynamically change the caption value of labels in a form. I'm inserting a screenshot of a snippet where I am successful in printing a concatenated value to the Immediate Window. What's in that looks exactly like what I need in my code. However, while i can debug.print these concatenated string values, once I remove the debug.print, everything in my code goes sideways. I'm missing something here, but I've already spent the better part of an entire morning trying to think of what it is with no results.

I'm just concatenating "Me.lbl" + element of myProdArr + "Sales.Caption" + " = " + "element of sales/cost/payable array"

I'm doing this in an attempt to substitute the need to type every last one out so I can DRY up the code. Is this even possible or am I dreaming?

Dim SalesCap, CostCap, PayableCap As String
Dim SalesObj, CostObj, PayableObj As Object

For i = LBound(myProdArr) To UBound(myProdArr)
    SalesCap = "Me.lbl" & myProdArr(i) & "Sales.Caption"
    CostCap = "Me.lbl" & myProdArr(i) & "Cost.Caption"
    PayableCap = "Me.lbl" & myProdArr(i) & "Payable.Caption"
    If Me.DealType = "Used" Then
        Debug.Print SalesCap & " = " & """" & myUsedSalesArr(i) & """"
        Debug.Print CostCap & " = " & """" & myUsedCostArr(i) & """"
        Debug.Print PayableCap & " = " & """" & myUsedPayArr(i) & """"
    Else
        Debug.Print SalesCap & " = " & """" & myNewSalesArr(i) & """"
        Debug.Print CostCap & " = " & """" & myNewCostArr(i) & """"
        Debug.Print PayableCap & " = " & """" & myNewPayArr(i) & """"
    End If
Next i

Any ideas?


回答1:


You can't concat properties that references a control (OK CallByName can), but you can concat strings in the Controls collection.

SalesCap = Me.Controls("lbl" & myProdArr(i) & "Sales").Caption
' result: SalesCap = Me.Controls("lblExtendedServiceSales").Caption


来源:https://stackoverflow.com/questions/60103081/how-can-i-convert-a-string-to-an-actual-form-label-caption-in-microsoft-access-v

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