vba 在子表单中设置过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21109977/
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
Setting Filter in Subform
提问by 7thGalaxy
I've a main form "Properties", which has two subforms, one of which displays the rooms in that property, the other one occupants in each room.
我有一个主窗体“属性”,它有两个子窗体,其中一个显示该属性中的房间,另一个显示每个房间中的居住者。
As you change the property, the rooms change in the first subform, which is continuous. As you scroll down the room subform, making a new room active, I want the occupants to change in the second subform.
当您更改属性时,房间在第一个子窗体中发生变化,这是连续的。当您向下滚动房间子表单,激活一个新房间时,我希望居住者在第二个子表单中进行更改。
So far I've written this in the Current event of the "properties" main form:
到目前为止,我已经在“属性”主窗体的当前事件中写了这个:
Dim dblRoomID As Double
dblRoomID = Forms.Properties.frmRoomsByPropertySubform.Form.room_id
Which successfully pulls out the Room_ID from the first Subform.
它成功地从第一个子表单中提取了 Room_ID。
Now I need to use that Room_ID to set a filter in the second subform, which currently displays all occupants of all properties, but has a Room_ID field.
现在我需要使用 Room_ID 在第二个子表单中设置过滤器,该表单当前显示所有属性的所有住户,但有一个 Room_ID 字段。
I cant get
我不能得到
Forms.Properies.frmStudentsRoomQuickview.Form.Filter = "[Room_ID]=" & dblRoomID
or docmd.applyfilter to work - I've been working on the assumption that this is because the active form needs to be the 'frmstudentRoomQuickview' for the later to work - but I can't understand why simply setting the .filter won't work.
或 docmd.applyfilter 工作 - 我一直在假设这是因为活动表单需要成为“frmstudentRoomQuickview”以便后者工作 - 但我不明白为什么简单地设置 .filter 不会工作。
Edit: I should add, I can't use a subform within the "rooms" form, as the rooms form needs to be continuous.
编辑:我应该补充一点,我不能在“房间”表单中使用子表单,因为房间表单需要是连续的。
Edit 2:
编辑2:
Private Sub Form_Current()
Dim dblRoomID As Double
If IsNull(Forms.Properties.frmRoomsByPropertySubform.Form.room_id) Then
Forms.Properties.frmRoomsByPropertySubform.Visible = False
Forms.Properties.frmStudentsRoomQuickview.Visible = False
Else
Forms.Properties.frmRoomsByPropertySubform.Visible = True
Forms.Properties.frmStudentsRoomQuickview.Visible = True
dblRoomID = Forms.Properties.frmRoomsByPropertySubform.Form.room_id
Call frmStudentsRoomQuickview_Enter(dblRoomID)
End If
End Sub
Private Sub frmStudentsRoomQuickview_Enter(dblRoomID)
Forms.Properties.frmStudentsRoomQuickview.Filter = "[room_id] = " & dblRoomID
Forms.Properties.frmStudentsRoomQuickview.FilterOn = True
Forms.Properties.frmStudentsRoomQuickview.Requery
Debug.Print Screen.ActiveForm.name
End Sub
I'm now getting "Procedure declaration does not match description of event or procedure having same name" errors
我现在收到“过程声明与具有相同名称的事件或过程的描述不匹配”错误
回答by Jamie Dunstan
Did you try turning the filter on?
你试过打开过滤器吗?
Forms.Properies.frmStudentsRoomQuickview.Form.Filter = "[Room_ID]=" & dblRoomID
Forms.Properies.frmStudentsRoomQuickview.Form.FilterOn = True
Edit:
编辑:
Having seen your update, I understand the issue. The problem is that you cannot pass a parameter to the _Enter event. You'd have to do something like this:
看到你的更新,我明白了这个问题。问题是您不能将参数传递给 _Enter 事件。你必须做这样的事情:
Dim dblRoomID As Double
Private Sub Form_Current()
dblRoomID = 0
If IsNull(Forms.Properties.frmRoomsByPropertySubform.Form.room_id) Then
Me.frmRoomsByPropertySubform.Visible = False
Me.frmStudentsRoomQuickview.Visible = False
Else
Me.frmRoomsByPropertySubform.Visible = True
Me.frmStudentsRoomQuickview.Visible = True
dblRoomID = Me.frmRoomsByPropertySubform.Form.room_id
DoStudentsRoomQuickViewFilter
End If
End Sub
Private Sub DoStudentsRoomQuickViewFilter()
If dblRoomID <> 0 Then
Me.frmStudentsRoomQuickview.Form.Filter = "[Room_ID] = " & dblRoomID
Me.frmStudentsRoomQuickview.Form.FilterOn = True
Me.frmStudentsRoomQuickview.Requery
Debug.Print Screen.ActiveForm.Name
End If
End Sub
Private Sub frmStudentsRoomQuickview_Enter()
DoStudentsRoomQuickViewFilter
End Sub
回答by DaveG
Check your spelling of "Properties"...
检查您对“属性”的拼写...
Forms.Properies.frmStudentsRoomQuickview.Form.Filter = "[Room_ID]=" & dblRoomID
Forms.Properies.frmStudentsRoomQuickview.Form.FilterOn = True