vba 在 MS Access 中的表单之间传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3474568/
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-08 10:42:17  来源:igfitidea点击:

Passing parameters between forms in MS Access

formsms-accessvbaparameters

提问by user419263

I have created a login form named login where the username is typed into the txtEmployee textbox, and I need to display the same in the second page in another form in MS Access.

我创建了一个名为 login 的登录表单,其中用户名被输入到 txtEmployee 文本框中,我需要在 MS Access 的另一个表单的第二页中显示相同的内容。

回答by Heinzi

DoCmd.OpenFormallows you to pass an arbitrary value as the last parameter. This value can be accessed in the new form as Me.OpenArgs:

DoCmd.OpenForm允许您将任意值作为最后一个参数传递。可以在新形式中访问此值,如下所示Me.OpenArgs

' Invoked by some Button on the first form '
Sub GoToSecondPage()
    DoCmd.OpenForm "MySecondPage", acNormal, , , , , txtEmployee.Value
End Sub

' Second form '
Sub Form_Open(Cancel As Integer)
    If Not IsNull(Me.OpenArgs) Then
        lblShowEmployeeName.Value = Me.OpenArgs
    End If
End Sub

(Code example untested.)

(代码示例未经测试。)

回答by Lance Roberts

You can pass a delimited string as the OpenArgs parameter:

您可以将分隔字符串作为 OpenArgs 参数传递:

DoCmd.OpenForm FormName:="miscForm", OpenArgs:=paramstring

Here's a routine for processing a pipe-delimited string passed as the parameter to DoCmd.OpenForm:

下面是处理作为参数传递给 DoCmd.OpenForm 的管道分隔字符串的例程:

Dim Pstring As Variant

If Len(Me.OpenArgs) > 0 Then
   Pstring = Split(Me.OpenArgs, "|")
   var1 = Pstring(0)
   <etc..>
End If

回答by Kevin Ross

Personally I would pass them through the open arguments when opening the form. For example from form A your would write

我个人会在打开表单时通过公开参数传递它们。例如从表格 A 你会写

DoCmd.OpenForm "frmB", , , , , acDialog,”Badger”

And then in the OnOpen event of form B you can capture what you have sent like this

然后在表单 B 的 OnOpen 事件中,您可以像这样捕获您发送的内容

Me.txtSomething=Me.OpenArgs

You can only pass one thing however What I do a lot is pass a pipe delimited string in the open arguments and then split that out.

您只能传递一件事,但是我经常做的是在开放参数中传递一个管道分隔的字符串,然后将其拆分。

回答by That doesn't look right

A couple ideas...

几个想法...

Use the AfterUpdate event on the login username field to write the name to a global variable, then populate the field on the second page with the OnLoad event.

使用登录用户名字段上的 AfterUpdate 事件将名称写入全局变量,然后使用 OnLoad 事件填充第二页上的字段。

Or, if you plan on leaving the log in form open at all times you could set the default value of the field directly to =[Forms]![LogInForm]![UserName]

或者,如果您打算让登录表单始终保持打开状态,您可以将该字段的默认值直接设置为 =[Forms]![LogInForm]![UserName]

回答by Jim Parker

Why not create a public function which stores &/or retrieves a global variable that you store after the first form is executed? This would allow you to not only use it on one or more forms, but also as criteria within a query (for example, to return records which match the user name you've stored).

为什么不创建一个公共函数来存储和/或检索您在第一个表单执行后存储的全局变量?这将使您不仅可以在一个或多个表单上使用它,还可以将其用作查询中的条件(例如,返回与您存储的用户名匹配的记录)。

I agree this is "fake" security, but there also times when it's sufficient for your needs. I've implemented this before, where I match the environment string username to an entry in a user table.

我同意这是“假”安全性,但有时也足以满足您的需求。我之前已经实现过,我将环境字符串 username 与用户表中的条目进行匹配。

BTW, I refer to this feature as personalization, not security.

顺便说一句,我将此功能称为个性化,而不是安全性。

Just a thought.

只是一个想法。