设置文本框 VBA 的控件源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13670997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:40:24  来源:igfitidea点击:

Setting the Control Source of a textbox VBA

vbams-accesstextboxms-access-2007datasourcecontrol

提问by Paolo Bernasconi

In my Access 2007 Form, I was previously successful in setting the controlSource of a textbox, directly in the properties window, using this code

在我的 Access 2007 表单中,我之前成功设置了一个文本框的 controlSource,直接在属性窗口中,使用此代码

=UCase(Left([txtLName],6) & "_" & Left([TxtFName],1))

However, in an attempt to hard-code this into the form, I'm trying to use VBA to set the controlSource property using this code:

但是,为了将其硬编码到表单中,我尝试使用 VBA 使用以下代码设置 controlSource 属性:

Me.txtCodePersonal.ControlSource = "=UCase(Left([txtLName],6) & "_" & Left([TxtFName],1))"

In my debug, it address my problem to the "_"section of this line.

在我的调试中,它将我的问题解决到"_"这一行的部分。

I don't know how the controlSource property work in VBA, so I don't know how to correct this. Thank for all your help in advance.

我不知道 controlSource 属性在 VBA 中是如何工作的,所以我不知道如何纠正这个问题。提前感谢您的所有帮助。

采纳答案by HansUp

You're attempting to assign a string value to the .ControlSourceproperty. However, that string includes quotes within it. Similarly, in the Immediate window, this will throw an error:

您正在尝试为该.ControlSource属性分配一个字符串值。但是,该字符串中包含引号。同样,在立即窗口中,这将引发错误:

Debug.Print "=UCase(Left([txtLName],6) & "_" & Left([TxtFName],1))"

Double up the quotes inside the string to avoid that problem.

将字符串内的引号加倍以避免该问题。

Debug.Print "=UCase(Left([txtLName],6) & ""_"" & Left([TxtFName],1))"
=UCase(Left([txtLName],6) & "_" & Left([TxtFName],1))