访问 VBA OpenForm 分组和排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17700366/
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
Access VBA OpenForm Grouping and Sorting
提问by Steven 'Catfish' Catlett
I have a form that is used for data entry. We have to go back through and add data to these records. Is there a way to pull up the form that groups the records by field "A" and sorts by field "B"? This would essentially order the forms A1-1, A1-2, etc, making adding data easier.
我有一个用于数据输入的表单。我们必须回过头来将数据添加到这些记录中。有没有办法拉出按字段“A”分组记录并按字段“B”排序的表单?这实质上是对表单 A1-1、A1-2 等进行排序,从而使添加数据更容易。
Right now I am using DoCmd.OpenForm to only display records with certain values in certain fields. Do I just need to modify this a bit?
现在我使用 DoCmd.OpenForm 只显示某些字段中具有某些值的记录。我只需要稍微修改一下吗?
Thanks for the help!
谢谢您的帮助!
[Edit]
[编辑]
I would like this to load the form on button click so I have
我希望在单击按钮时加载表单,所以我有
Private Sub btnDataEntry_Click()
DoCmd.OpenForm "Data Sheet", acNormal, , , acFormEdit, , OpenArgs:="MapNumber"
End Sub
Then as suggested
然后按照建议
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
Main.OrderBy = Me.OpenArgs
Main.OrderByOn = True
End If
End Sub
This is not working for me. If possible I would also like it to group all map numbers together and then have all Item numbers ascending. So there could be 10 entries with map number 1 and item numbers 1-10.
这对我不起作用。如果可能,我还希望将所有地图编号组合在一起,然后让所有项目编号升序。所以可能有 10 个条目,地图编号为 1,项目编号为 1-10。
采纳答案by HansUp
OpenForm
doesn't include an option to specify the sort order. However you could use its OpenArgsoption to pass in sort information, then apply that during form load.
OpenForm
不包括指定排序顺序的选项。但是,您可以使用其OpenArgs选项传递排序信息,然后在表单加载期间应用该信息。
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
Me.OrderBy = Me.OpenArgs
Me.OrderByOn = True
End If
End Sub
Then to open YourFormsorted by a field named idin ascending order ...
然后打开按名为id的字段按升序排序的YourForm...
DoCmd.OpenForm "YourForm", OpenArgs:="id"
Include DESC
for descending order ...
包括DESC
降序...
DoCmd.OpenForm "YourForm", OpenArgs:="id DESC"
Use this version of Form_Load
to troubleshoot why the form opens without the sorting you expect.
使用此版本Form_Load
来解决为什么表单打开时没有按您期望的排序。
Private Sub Form_Load()
MsgBox "Me.OpenArgs: " & Nz(Me.OpenArgs, "Null")
? ? If Not IsNull(Me.OpenArgs) Then
? ? ? ? Me.OrderBy = Me.OpenArgs
? ? ? ? Me.OrderByOn = True
? ? End If
MsgBox "Me.OrderBy : '" & Me.OrderBy & "'"
MsgBox "Me.OrderByOn: " & Me.OrderByOn
End Sub