database MS Access - 打开一个表单,获取先前表单中的字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/723468/
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
MS Access - open a form taking a field value from a previous form
提问by Fionnuala
I have a form in an MS Access database which lists all the landowners consulted with for a new electricity line. At the end of each row is a button which opens another form, showing the details of all consultation, offers made etc.
我在 MS Access 数据库中有一个表格,其中列出了为新电线咨询过的所有土地所有者。在每一行的末尾是一个按钮,可以打开另一个表格,显示所有咨询、报价等的详细信息。
I am trying to use vb in MS Access to take the contactID and automatically put it in a field in the details form, so that landowner's consultation details will pop up automatically. I am not a vb programmer at all (I have a comp sci degree mostly in Java and I'm currently working as a GIS analyst but it's a small company so I've been asked to get an Access database working).
我正在尝试在 MS Access 中使用 vb 获取 contactID 并自动将其放入详细信息表单的字段中,以便自动弹出土地所有者的咨询详细信息。我根本不是一个 vb 程序员(我的计算机科学学位主要是 Java,我目前是一名 GIS 分析师,但它是一家小公司,所以我被要求让 Access 数据库工作)。
I want to say [detailsForm]![contactID] = [landownerlist]![ID] in a way that vb and access will be happy with. Then I can see if I'm on the right track and if it will actually work! What I have above does not actually work. It won't compile.
我想以 vb 和 access 满意的方式说 [detailsForm]![contactID] = [landownerlist]![ID]。然后我可以看看我是否在正确的轨道上,它是否真的有效!我上面的内容实际上不起作用。它不会编译。
From Kaliana
从卡利亚纳
回答by Fionnuala
If you wish to open a form to a new record and to set the ID there, you can use Openargs, an argument of Openform:
如果你想打开一个新记录的表单并在那里设置 ID,你可以使用 Openargs,一个 Openform 的参数:
DoCmd.OpenForm "FormName",,,,acFormAdd,,Me.ID
The opened form would also need some code:
打开的表单还需要一些代码:
If Me.Openargs<>vbNullstring Then
Me.Id = Me.Openargs
End If
It is also possible to find:
还可以找到:
Forms!LandownersList.Recordset.FindFirst "ID=" & Me.ID
or fill in a value:
或填写一个值:
Forms!LandownersList!Id = Me.ID
on the form being opened from the calling form.
在从调用窗体打开的窗体上。
回答by JeffO
You may want to look into the code that is behind these buttons. If you are using a docmd.openform you can set the 4th Setting to a where clause on openning the next form.
您可能需要查看这些按钮背后的代码。如果您使用的是 docmd.openform,您可以将第四个设置设置为打开下一个表单的 where 子句。
DoCmd.OpenForm "OpenFormName", acNormal, , "[contactID] = " _
& [detailsForm]![contactID] , acFormEdit, acWindowNormal
This assumes contact ID is numeric and doesn't require any quotes.
这假设联系人 ID 是数字并且不需要任何引号。
回答by Oorang
Using open args is the generally accepted solution, as alluded to by others. This just falls under the category of "For you edification":) One of the problems with using open args is that unless you are careful with your comments it's easy to forget what they were supposed to mean. Were you passing more than one? Which is which? How did I do it here? How did I do it there etc. For my own money, I standardized to this (below) so I can always pass more than one argument without fear, and when I review my code a year from now, I can still see what's what without a huge hassle:
正如其他人所暗示的那样,使用 open args 是普遍接受的解决方案。这只是属于“为你启蒙”的范畴:) 使用开放参数的问题之一是,除非你对你的评论很小心,否则很容易忘记它们应该是什么意思。你通过了不止一个?哪个是哪个?我在这里是怎么做的?我是怎么在那里做的等等。为了我自己的钱,我标准化了这个(下面)所以我总是可以毫无畏惧地传递多个论点,当我从现在起一年后我的代码时,我仍然可以看到没有什么一个巨大的麻烦:
Option Explicit
'Example use: DoCmd.OpenForm "Example", OpenArgs:="Some Filter|True"
Public Enum eForm1Args
eFilter = 0
eIsSpecial = 1
End Enum
Private m_strArgs() As String
Public Property Get Args(ByVal eForm1Args As eForm1Args) As String
Args = m_strArgs(eForm1Args)
End Property
Private Sub Form_Open(Cancel As Integer)
m_strArgs = Split(Nz(Me.OpenArgs, vbNullString), "|")
If LenB(Me.Args(eFilter)) Then Me.Filter = Me.Args(eFilter)
End Sub
Private Sub Command1_Click()
If LCase$(Me.Args(eIsSpecial)) = "true" Then
'Do something special
End If
End Sub
回答by Simon
As previously posted OpenArgs is great for this. One trick I have learned is that it is easy to pass in multiple parameters if required as a delimited string (comma for example), the target form can then access these values using the Split() function thus:
正如之前发布的那样,OpenArgs 对此非常有用。我学到的一个技巧是,如果需要作为分隔字符串(例如逗号)传递多个参数很容易,然后目标表单可以使用 Split() 函数访问这些值,因此:
StringArrayVariable()= Split(me.OpenArgs,",")
Me.textbox= StringArrayVariable(0)
Me.textbox1= StringArrayVariable(1)
etc.
等等。
This is air code so check out the helpfile for Split().
这是空气代码,因此请查看 Split() 的帮助文件。
It is also possible to pass objects in OpenArgs as well, it requires some manual memory pointer manipulation and I don't have the code to hand but I'm sure a Google search will find some examples. This technique can cause some random crashes though. Be Warned!
也可以在 OpenArgs 中传递对象,它需要一些手动内存指针操作,我手头没有代码,但我相信谷歌搜索会找到一些例子。不过,这种技术可能会导致一些随机崩溃。被警告!