|
给网友做的示例,主要实现多条件组合查询,代码使用了SQL IN关键字包含功能.
下载源代码文件
Function myQuery()
Dim strSQL As String
Dim strXB As String
Dim strHY As String
strSQL = "SELECT * from 患者资料"
strSQL = strSQL & " WHERE 1=1" '设一个永远存在的条件,目的是让WHERE先存在,下面的条件直接就可以使用AND联了
If Me.Check0 Then strXB = strXB & "'男'" & ","
If Me.Check1 Then strXB = strXB & "'女'" & ","
If Me.Check2 Then strHY = strHY & "'未婚'" & ","
If Me.Check3 Then strHY = strHY & "'已婚'" & ","
If Me.Check4 Then strHY = strHY & "'离异'" & ","
If Me.Check5 Then strHY = strHY & "'丧偶'" & ","
If Len(strXB) Then strXB = Left(strXB, Len(strXB) - 1)
If Len(strHY) Then strHY = Left(strHY, Len(strHY) - 1)
If Len(strXB) Then strSQL = strSQL & " AND 性别 IN(" & strXB & ")"
If Len(strHY) Then strSQL = strSQL & " AND 婚姻 IN(" & strHY & ")"
Debug.Print strSQL
Me.患者资料查询子窗体.Form.RecordSource = strSQL
End Function |
|