我有一个名为FrmAgent的论坛。这是从名为“Agent”的表中获取数据。在此表中有一个名为“代理名称”的列。在这个cloumn下有5个名字“Chris”“Nikita”“Noe”“Jennifer”和“Diane”。在论坛上,我有一个复选框旁边带有这些名称的复选框。当然,所有不同的复选框。我需要做的是能够运行我的查询,从查询每个复选框的论坛中的“代理”表中获取数据。
示例如果我有标记为Chris和Diane的复选框,我需要从“代理”表中获取所有数据。如果只标记其中一个,那就抓住那些人数据。
这有意义吗?你能帮助我吗?我对vba和访问非常新。示例和编码将有很大帮助。先谢谢你。
答案 0 :(得分:0)
假设您有“提交”或“过滤器”命令按钮,您可以使用以下代码:
Dim strFilter As String
Dim i As Integer
strFilter = ""
If Me.chkbox1.Value = true Then
strFilter = "([Agent Name] = '" & Me.Name1Value & "' AND "
End IF
If Me.chkbox2.Value = true Then
strFilter = strFilter & "([Agent Name] = '" & Me.Name2Value & "' AND "
End IF
<<<<REPEAT AS MANY TIMES AS NEEDED>>>
If right(strFilter, 4) = "AND " Then
i = len(strFilter)
Strfilter = left(strFilter, i - 4)
Me.Filter = strFilter
Me.FilterOn = True
Me.Requery
i = Me.Recordset.RecordCount
If i = 0 Then
MsgBox "There are no records selected", vbOKOnly, "No Records”
‘You could delete the filter & Requery if you want.
End If
End If
答案 1 :(得分:0)
这是我使用的代码。我制作了一个模块和一个宏来运行。我在表单上附加了一个按钮,将两者连接在一起。我在第一个声明中得到错误。没有子表单。再次感谢您的帮助。 选项比较数据库
功能YoMomma()
Dim strFilter As String
Dim i As Integer
strFilter = ""
If Me.Chris.Value = True Then
strFilter = "([Agent Name] = '" & Me.Chris & "' AND "
End If
If Me.Diane.Value = True Then
strFilter = strFilter & "([Agent Name] = '" & Me.Diane & "' AND "
End If
If Me.Noe.Value = True Then
strFilter = "([Agent Name] = '" & Me.Noe & "' AND "
End If
If Me.Jennifer.Value = True Then
strFilter = strFilter & "([Agent Name] = '" & Me.Jennifer & "' AND "
End If
If Me.Nikita.Value = True Then
strFilter = "([Agent Name] = '" & Me.Nikita & "' AND "
End If
'REPEAT AS MANY TIMES AS NEEDED
If Right(strFilter, 4) = "AND " Then
i = Len(strFilter)
strFilter = Left(strFilter, i - 4)
Me.Filter = strFilter
Me.FilterOn = True
Me.Requery
i = Me.Recordset.RecordCount
If i = 0 Then
MsgBox "There are no records selected", vbOKOnly, "No Records”"
'You could delete the filter & Requery if you want.
End If
结束如果 结束功能