|
3#
楼主 |
发表于 2009-4-21 19:46:09
|
只看该作者
我就是做了这样子的一个通讯录,按下查询就会在下面“查询结果”里面显示查找到的记录。查询的事件响应代码如下:
Private Sub Command46_Click()
On Error GoTo Err_Command46_Click
Dim qry_tmp As QueryDef
Dim dbs_itsr As Database
Dim strwhere As String
Dim strsql As String
Dim length As Integer
Set dbs_itsr = CurrentDb()
strsql = "SELECT * FROM Contract"
strwhere = ""
If Not IsNull(Me.学号) And Not Me.学号 = "" Then
If Me.Check1.Value = False Then
strwhere = strwhere & "Contract.学号='" & Me.学号 & "' " & Me.LogicOp1 & " "
Else
strwhere = strwhere & "Contract.学号 LIKE '*" & Me.学号 & "*' " & Me.LogicOp1 & " "
End If
length = Len(Me.LogicOp1) + 2
End If
If Not IsNull(Me.姓名) And Not Me.姓名 = "" Then
If Me.Check2.Value = False Then
strwhere = strwhere & "Contract.姓名='" & Me.姓名 & "' " & Me.LogicOp2 & " "
Else
strwhere = strwhere & "Contract.姓名 LIKE '*" & Me.姓名 & "*' " & Me.LogicOp2 & " "
End If
length = Len(Me.LogicOp2) + 2
End If
If Not IsNull(Me.班级) And Not Me.班级 = "" Then
If Me.Check3.Value = False Then
strwhere = strwhere & "Contract.班级='" & Me.班级 & "' " & Me.LogicOp3 & " "
Else
strwhere = strwhere & "Contract.班级 LIKE '*" & Me.班级 & "*' " & Me.LogicOp3 & " "
End If
length = Len(Me.LogicOp3) + 2
End If
If Not IsNull(Me.专业) And Not Me.专业 = "" Then
If Me.Check4.Value = False Then
strwhere = strwhere & "Contract.专业='" & Me.专业 & "' " & Me.LogicOp4 & " "
Else
strwhere = strwhere & "Contract.专业 LIKE '*" & Me.专业 & "*' " & Me.LogicOp4 & " "
End If
length = Len(Me.LogicOp4) + 2
End If
If Not IsNull(Me.电话) And Not Me.电话 = "" Then
If Me.Check5.Value = False Then
strwhere = strwhere & "Contract.电话='" & Me.电话 & "' " & Me.LogicOp5 & " "
Else
strwhere = strwhere & "Contract.电话 LIKE '*" & Me.电话 & "*' " & Me.LogicOp5 & " "
End If
length = Len(Me.LogicOp5) + 2
End If
If Not IsNull(Me.邮件) And Not Me.邮件 = "" Then
If Me.Check6.Value = False Then
strwhere = strwhere & "Contract.邮箱地址='" & Me.邮件 & "' " & Me.LogicOp6 & " "
Else
strwhere = strwhere & "Contract.邮箱地址 LIKE '*" & Me.邮件 & "*' " & Me.LogicOp6 & " "
End If
length = Len(Me.LogicOp6) + 2
End If
If Not IsNull(Me.性别) And Not Me.性别 = "" And Not Me.性别 = "任意" Then
strwhere = strwhere & "Contract.性别='" & Me.性别 & "' " & Me.LogicOp7 & " "
length = Len(Me.LogicOp7) + 2
End If
If Not IsNull(Me.年龄1) And Not IsNull(Me.年龄2) And Not Me.年龄1 = "" And Not Me.年龄2 = "" And Not Me.年龄1 = "任意" And Not Me.年龄2 = "任意" Then
If Me.年龄1 = Me.年龄2 Then
strwhere = strwhere & "Contract.年龄=" & Me.年龄1
End If
If Me.年龄1 > Me.年龄2 Then
strwhere = strwhere & "Contract.年龄<" & Me.年龄1 & " AND Contract.年龄>" & Me.年龄2
End If
If Me.年龄1 < Me.年龄2 Then
strwhere = strwhere & "Contract.年龄<" & Me.年龄2 & " AND Contract.年龄>" & Me.年龄1
End If
length = 0
End If
If Len(strwhere) > 0 Then
strwhere = Left(strwhere, Len(strwhere) - length)
End If
If Len(strwhere) > 0 Then
strsql = strsql & " WHERE " & strwhere & ";"
End If
For Each qry_tmp In dbs_itsr.QueryDefs
If qry_tmp.Name = "ContractQuery" Then
dbs_itsr.QueryDefs.Delete qry_tmp.Name
End If
Next qry_tmp
Set qry_tmp = dbs_itsr.CreateQueryDef("ContractQuery", strsql)
On Error GoTo Err_Command46_Click
Me.查询结果.Form.RecordSource = strsql
Me.查询结果.Form.Requery
Me.Refresh
Exit_Command46_Click:
Exit Sub
Err_Command46_Click:
MsgBox Err.Description
Resume Exit_Command46_Click
End Sub |
|