在过去的几天里,我一直在尝试将过滤器应用于Access-2010表单。表单的记录源是一个表。过滤器是从表单上的一系列按钮创建的。以下是构建过滤器的按钮
列表按钮的事件过程:
Private Sub btnListInterfaceLog_Click()
Dim List As String
List = StartList()
End Sub
StartList()函数代码
Private Function StartList()
Dim strWhereClause As String
strWhereClause = MakeWhere()
MsgBox strWhereClause
Me.FilterOn = True
Me.Filter = strWhereClause
Me.Requery
End function
MakeWhere代码()
Private Function MakeWhere() As String
Dim whereClause As String
whereClause = ""
If Me![cboinstrument].Enabled = True And Me![cboinstrument] <> "" Then
whereClause = RvAppendCondition(whereClause, "InstrumentName", "String", "=", Me![cboinstrument])
End If
If Me![cboFolder].Enabled = True And Me![cboFolder] <> "" Then
whereClause = RvAppendCondition(whereClause, "folder", "String", "=", Me![cboFolder])
End If
If Me![BatchFrom].Enabled = True And Me![BatchFrom] > 0 And Me![BatchTo].Enabled = True And Me![BatchTo] > 0 Then
whereClause = RvAppendCondition(whereClause, "BatchID", "Number", "=", Me![BatchFrom], Me![BatchTo])
End If
If Me![AnalyzedFrom].Enabled = True And Me![AnalyzedFrom] > 0 And Me![AnalyzedTo].Enabled = True And Me![AnalyzedTo] > 0 Then
whereClause = RvAppendCondition(whereClause, "Dateofbatch", "Date", "=", Me![AnalyzedFrom], Me![AnalyzedTo])
End If
MakeWhere = whereClause
End Function
RvAppendCondition函数接受给定的字符串,并向where语句添加一个新参数。它已经过多次测试和工作,所以你不必太关注它。如果被问到我可以提供代码供参考。
例如:如果我所做的只是选择乐器名称(来自:组合框)并将其余部分留空,则按列表,strWhereClause将msgbox“[InstrumentName] ='ICPMS'”并且当我没有时我没有错误转到主窗体的属性过滤器下面没有列出任何内容。当我按下列表时,如何使它成为过滤器属性中的值。 (旁注:所有函数都有错误陈述,我把它们用来缩短它们)
答案 0 :(得分:0)
这是您的CODE应该是什么。
Private Sub btnListInterfaceLog_Click()
Dim strWhereClause As String
strWhereClause = MakeWhere()
MsgBox strWhereClause
Me.Filter = strWhereClause
Me.FilterOn = True
End Sub
Private Function MakeWhere() As String
Dim whereClause As String
If Me![cboinstrument].Enabled = True And Me![cboinstrument] <> "" Then
whereClause = whereClause & "(InstrumentName = '" & Me![cboinstrument] & "') AND "
End If
If Me![cboFolder].Enabled = True And Me![cboFolder] <> "" Then
whereClause = whereClause & "(folder = '" & Me![cboFolder] & "') AND "
End If
If Me![BatchFrom].Enabled = True And Me![BatchFrom] > 0 And Me![BatchTo].Enabled = True And Me![BatchTo] > 0 Then
whereClause = whereClause & "(BatchID BETWEEN " & Me![BatchFrom] & " AND " & Me![BatchTo] & ") AND "
End If
If Me![AnalyzedFrom].Enabled = True And Me![AnalyzedFrom] > 0 And Me![AnalyzedTo].Enabled = True And Me![AnalyzedTo] > 0 Then
whereClause = whereClause & "(Dateofbatch BETWEEN " & Format(Me![AnalyzedFrom], "\#mm\/dd\/yyyy\#") & _
" AND " & Format(Me![AnalyzedTo], "\#mm\/dd\/yyyy\#") & ") AND "
End If
MakeWhere = Left(whereClause, Len(whereClause) - 5)
End Function