MS Access 2007 使用 VBA 打开特定记录的单独表单

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

MS Access 2007 Open Separate Form to Specific Record Using VBA

vbams-accessaccess-vbams-access-2007

提问by smk081

I have two forms, one which is a data entry form, the other is a summary form which lists all the records in the database. On the summary form there is a listbox which lists all the records. I want the user to be able to select a record from the listbox and using a command button, open the second form to the specific record. I've gotten this work with using a specific field, in one case "Name", using the code below:

我有两种表格,一种是数据输入表格,另一种是列出数据库中所有记录的汇总表格。在汇总表上有一个列出所有记录的列表框。我希望用户能够从列表框中选择一条记录并使用命令按钮打开第二个表单到特定记录。我通过使用特定字段(在一种情况下为“名称”)使用以下代码完成了这项工作:

DoCmd.OpenForm "frmEditAddPerson", acNormal, ,"[PersonName]='" & Me.listPeople.Value & "'"

but when I realized two people could have the same name, I decided it made sense to use the PersonID which is the primary key and its datatype is "AutoNumber". I can't seem to get this to work:

但是当我意识到两个人可以有相同的名字时,我决定使用作为主键且其数据类型为“AutoNumber”的 PersonID 是有意义的。我似乎无法让这个工作:

DoCmd.OpenForm "frmEditAddPerson", acNormal, "[PersonName] = " & SelectPersonID

Note, I am getting SelectedPersonID by pulling it from ListBox from a hidden column. I confirmed that I am infact getting correct the number value for the AutoNumber field by displaying it in a MessageBox while trying to debug.

请注意,我通过从隐藏列的 ListBox 中拉取 SelectedPersonID 来获取它。我确认我实际上通过在尝试调试时在 MessageBox 中显示它来正确获取 AutoNumber 字段的数值。

For the WHERE argument of this method/command, I know you are supposed to contain String values in quotes, Integers without, and dates with "#" like in a SQL statement. I've tried delcaring SelectedPersonID as a string and as an integer and I still cannot get the above to work. I've even tried the below just to be sure:

对于此方法/命令的 WHERE 参数,我知道您应该像 SQL 语句中一样在引号中包含字符串值、不包含整数和带有“#”的日期。我试过将 SelectedPersonID 声明为字符串和整数,但仍然无法使上述内容正常工作。我什至试过下面的只是为了确定:

DoCmd.OpenForm "frmEditAddPerson", acNormal, "[PersonName] = " & CInt(SelectPersonID)

Each time I get "Type mismatch". Is the AutoNumber field special in the sense that it cannot be used for something like this or does it need to be handled in a special way?

每次我得到“类型不匹配”。AutoNumber 字段是否特殊,因为它不能用于这样的事情还是需要以特殊方式处理?

回答by HansUp

If PersonIDis the autonumber primary key, reference it in the fourth OpenFormargument (WhereCondition). Your OpenFormexamples still include PersonNameinstead of PersonID.

如果PersonID是自动编号主键,则在第四个OpenForm参数 ( WhereCondition) 中引用它。您的OpenForm示例仍然包括PersonName而不是PersonID.

Also, in your last two examples, PersonID was referenced in the third OpenFormargument (FilterName).

此外,在您的最后两个示例中,第三个OpenForm参数 ( FilterName) 中引用了 PersonID 。

DoCmd.OpenForm "frmEditAddPerson", acNormal, , "[PersonID] = " & SelectPersonID

It can be easier to keep track of which option is which by including the option names.

通过包含选项名称,可以更轻松地跟踪哪个选项是哪个。

DoCmd.OpenForm FormName:="frmEditAddPerson", View:=acNormal, _
    WhereCondition:="[PersonID] = " & SelectPersonID