我可以在 Microsoft Access VBA 中使用变量作为控件的名称吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8267453/
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
Can I use a variable as the name of a control in Microsoft Access VBA
提问by JMK
I have a Microsoft Access Popup Form which I use to lookup addresses. Once the user has found the postcode, the address is then put into various text boxes on the form it was launched from. The problem is, this popup form is launched from various forms throughout the database and so the text boxes it puts the result into are in different locations.
我有一个用于查找地址的 Microsoft Access 弹出窗体。一旦用户找到邮政编码,地址就会被放入它启动的表单上的各种文本框中。问题是,这个弹出表单是从整个数据库中的各种表单启动的,因此它把结果放入的文本框位于不同的位置。
I tried to work around this in the following way. I have a switchboard which is open at all times so I have a hidden Textbox on there which I programmatically put the name of the form I am launching the popup form from. I then declare a string variable which is set to the current value of this hidden textbox like so:
我试图通过以下方式解决这个问题。我有一个始终打开的总机,所以我在那里有一个隐藏的文本框,我以编程方式将要从中启动弹出窗体的窗体的名称。然后我声明一个字符串变量,它被设置为这个隐藏文本框的当前值,如下所示:
Dim currentForm As String
currentForm = [Forms]![foo]![bar]
I then tried to put my address details into the relevant textboxes like so:
然后我尝试将我的地址详细信息放入相关的文本框中,如下所示:
Forms!currentForm![txtCurrentAdd1] = rst![Line1]
However this isn't working as planned, what am I doing wrong?
然而,这并没有按计划工作,我做错了什么?
Thanks
谢谢
回答by Fionnuala
Either:
任何一个:
Dim currentForm As String
''Not sure where the two parts are coming from
''but you cannot have them like that
currentForm = "foobar"
Forms(currentForm).[txtCurrentAdd1] = rst![Line1]
Or
或者
Dim currentForm As Form
Set currentForm = Forms![foobar]
currentForm![txtCurrentAdd1] = rst![Line1]
You might like to read up on bang vs dot.
您可能想阅读有关 bang 与 dot 的内容。
Mind you, the whole thing looks a little like you are swimming upstream.
请注意,整个事情看起来有点像你在逆流而上。
回答by Christian Specht
You can access controls on other forms like this:
您可以访问其他表单上的控件,如下所示:
Dim FormName As String
Dim ControlName As String
FormName = "YourForm"
ControlName = "YourTextbox"
Forms(FormName).Controls(ControlName) = "New Value"
回答by Patrick Honorez
You could also use the OpenArgs property. To open your reusable popup, use:
您还可以使用 OpenArgs 属性。要打开可重复使用的弹出窗口,请使用:
DoCmd.OpenForm FormName:="frmPopup", OpenArgs:=Me.Name
in order to pass the caller's name.
为了传递调用者的名字。
In the frmPopup, you can refer to Me.OpenArgs
to get that information.
在 frmPopup 中,您可以参考Me.OpenArgs
以获取该信息。
回答by Jamie Garroch
I know this is an old post but this might be useful for new readers. The simple way I get round the problem of passing variables between modules and forms is to use the VBA registry area. It's a very simple and easy to use technique and keeps projects nice and clean.
我知道这是一篇旧文章,但这可能对新读者有用。我解决在模块和表单之间传递变量的问题的简单方法是使用 VBA 注册表区域。这是一种非常简单且易于使用的技术,可以使项目保持整洁。
To save a value:
要保存值:
SaveSetting AppName, Section, Key, Setting
To get the value:
获取值:
GetSetting AppName, Section, Key, [Default]
This avoids loosing track of variables and creating hidden controls on forms. It also works cross platform without change on a PC and a Mac.
这可以避免丢失对变量的跟踪和在表单上创建隐藏控件。它还可以跨平台工作,无需在 PC 和 Mac 上进行更改。
I then usually set a load of constants in my projects to define the first three parameters:
然后我通常会在我的项目中设置一些常量来定义前三个参数:
Public Const REG_PROD = "My product name"
Public Const REG_CONFIG = "Configuration"
Public Const REG_SETTING1 = "My Setting 1"
So the above calls would become:
所以上面的调用将变成:
To save a value:
要保存值:
SaveSetting REG_PROD, REG_CONFIG, REG_SETTING1, "My Value"
To get the value:
获取值:
GetSetting REG_PROD, REG_CONFIG, REG_SETTING1, "Value not set"
I used to use the hidden control method but it adds a lot of management and you can't always the variable values whereas you can keep an instance of RegEdit open for debugging purposes.
我曾经使用隐藏控件方法,但它增加了很多管理,您不能总是保留变量值,而您可以保持打开 RegEdit 的实例以进行调试。
回答by Huntnewaygo
I think
我认为
Me.Controls(variablelabelName).Caption
you can write code to make the variable change and use as the label
's name to create the unique caption for each label.
您可以编写代码来更改变量并用作label
的名称来为每个标签创建唯一的标题。