如何在 Microsoft Access VBA 中将字符串转换为实际的表单标签标题? [英] How can I convert a string to an actual form label caption in Microsoft Access VBA?

查看:28
本文介绍了如何在 Microsoft Access VBA 中将字符串转换为实际的表单标签标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用数组值在不同的场景中循环,以动态更改表单中标签的标题值.我正在插入一个片段的屏幕截图,其中我成功地将连接值打印到立即窗口.里面的内容与我在代码中需要的完全一样.但是,虽然我可以 debug.print 这些连接的字符串值,但一旦我删除了 debug.print,我的代码中的所有内容都会侧身.我在这里遗漏了一些东西,但我已经花了一整个上午的大部分时间来思考它是什么,但没有结果.

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.

我只是连接Me.lbl"+myProdArr的元素+Sales.Caption"+="+销售/成本/应付数组的元素"

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

有什么想法吗?

推荐答案

您不能连接引用控件的属性 (OK CallByName 可以),但您可以在 Controls 集合中连接字符串.

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

这篇关于如何在 Microsoft Access VBA 中将字符串转换为实际的表单标签标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆