简体   繁体   English

MS Access:引用 VBA 查询定义中的子表单文本框控件元素

[英]MS Access: referring to a subform textbox control element in VBA query definition

I have a subform with user input (text box), that can reach more than 255 chars and can have special characters.我有一个带有用户输入(文本框)的子表单,它可以超过 255 个字符并且可以包含特殊字符。 As I cannot assign it as a parameter to a vba query definition (only 255 characters will work), I thought about directly referencing the text box:由于我无法将其作为参数分配给 vba 查询定义(只能​​使用 255 个字符),因此我考虑直接引用文本框:

Insert into ...
Values (Forms!MainFormName!txt_MyTextField, ... )

does work in a query in the Access GUI, but not in a vba query definition specified with the following code:在 Access GUI 中的查询中确实有效,但在使用以下代码指定的 vba 查询定义中无效:

Set query_definition = CurrentDb.CreateQueryDef("", SQLQuery)) 

"Forms!MainFormName!NameOfSubformNavigationElement!txt_MyTextField" does not work either as a reference, just in case you are wondering. "Forms!MainFormName!NameOfSubformNavigationElement!txt_MyTextField" 也不能作为参考,以防万一。 Neither does "Forms!MainFormName!NameOfSubformNavigationElement.Form.txt_MyTextField" “Forms!MainFormName!NameOfSubformNavigationElement.Form.txt_MyTextField”也不行

How can I get what I want?我怎样才能得到我想要的? The string can have any kind of special chars like ' and ". Is there no alternative than appending the user input into the sql query? Is there some built-in function to escape these values?字符串可以有任何类型的特殊字符,如 ' 和 "。除了将用户输入附加到 sql 查询之外别无选择吗?是否有一些内置函数来转义这些值?

UPDATE: Added another example now that the environment is known.更新:现在环境已知,添加了另一个示例。

To use the form field in an action query, create a public function that will return the desired field, then call that function from your query.要在操作查询中使用表单字段,请创建一个将返回所需字段的公共函数,然后从您的查询中调用该函数。

Public Function GetFormField() As String
    GetFormField = Forms![MyMainForm]!MySub.Form.MyField
End Function

In your query, use something like:在您的查询中,使用以下内容:

WHERE ((([106thZip_code_database].zipcode)=GetFormField()));

The following is an example you could use in the Main form to reference a field in a subform.以下是您可以在主窗体中用于引用子窗体中的字段的示例。 'MySub' is the name of the control on the Main form - not the subform's object name. 'MySub' 是主窗体上控件的名称 - 不是子窗体的对象名称。

Debug.Print Forms![MyMainForm]!MySub.Form.MyField

A good reference on this subject can be found at: http://access.mvps.org/access/forms/frm0031.htm关于这个主题的一个很好的参考可以在: http : //access.mvps.org/access/forms/frm0031.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM