Option Explicit
Dim con As New ADODB.Connection
Dim res As New ADODB.Recordset
Dim moveX As Long '动态添加控件时控件移到X点
Dim moveY As Long '动态添加控件时控件移到Y点
Const mWidth = 1500
Const ConCount = 4 '表中查询条件的数量
Const WM_DELETE = &H400 '自定义消息
Const GWL_WNDPROC = -4
Dim LVW_COUNT As Integer 'listview控件中行数
Dim WithEvents cBoxName As VB.ComboBox
Dim WithEvents cBoxAge As VB.ComboBox
Dim WithEvents cBoxTime As VB.ComboBox
Dim isLoadAge(2) As Boolean
Dim tBoxName As VB.TextBox
Dim tBoxAge(1) As Control
Dim tBoxTime() As VB.TextBox
Dim lbl(1) As VB.Label
Private Sub Age_Click() '选中年龄
Dim itemA As ListItem
Dim i,x As Integer
res.MoveFirst
Do While Not res.EOF
If (res.Fields("Condition")) = "年龄" Then
For x = 1 To ListViewSearch.ListItems.Count
If ListViewSearch.ListItems(x).Text = "年龄" Then
MsgBox "该配置项中已经存在!"
Exit Sub
End If
Next x
Set itemA = ListViewSearch.ListItems.Add(,res.Fields("Condition"))
Set cBoxAge = Controls.Add("VB.ComboBox","cBoxAge")
LVW_COUNT = ListViewSearch.ListItems.Count
moveX = ListViewSearch.Width + 60
moveY = ListViewSearch.Top + 260 * (LVW_COUNT)
cBoxAge.Visible = True
cBoxAge.Move moveX,moveY,mWidth
For i = 1 To ConCount
If res.Fields(i).Value <> "" Then
cBoxAge.AddItem res.Fields(i).Value
End If
Next i
End If
res.MoveNext
Set itemA = Nothing
Loop
End Sub
Private Sub cBoxAge_Click() '选择年龄查询条件,根据所选条件查询
If Not (tBoxAge(0) Is Nothing) Then
Dim wParam As Long
Dim lParam As Long
Dim lResult As Long
SendKeys "{Enter}"
Call SendMessage(Me.hwnd,WM_DELETE,wParam,lParam)
End If
If cBoxAge.List(cBoxAge.ListIndex) = "介于两者之间" Then
Set tBoxAge(0) = Controls.Add("VB.TextBox","tBoxAge0") '添加控件用于输入年龄进行查询
Set tBoxAge(1) = Controls.Add("VB.TextBox","tBoxAge1")
Set lbl(0) = Controls.Add("VB.Label","lbl")
isLoadAge(0) = True
isLoadAge(1) = True
lbl(0).Caption = "到"
lbl(0).Visible = True
tBoxAge(0).Visible = True
tBoxAge(1).Visible = True
moveX = ListViewSearch.Width + cBoxAge.Width + 50
moveY = cBoxAge.Top
tBoxAge(0).Move moveX,mWidth,cBoxAge.Height - 50
moveX = moveX + tBoxAge(0).Width + 50
lbl(0).Width = mWidth - 1200
lbl(0).Move moveX,moveY + 50,lbl(0).Width,tBoxAge(0).Height
tBoxAge(1).Move moveX + lbl(0).Width - 100,cBoxAge.Height
Else
Set tBoxAge(0) = Controls.Add("VB.TextBox","tBoxAge0")
isLoadAge(0) = True
tBoxAge(0).Visible = True
moveX = ListViewSearch.Width + cBoxAge.Width + 50
moveY = cBoxAge.Top
tBoxAge(0).Move moveX,cBoxAge.Height
End If
End Sub
Private Sub cBoxAge_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Controls.Remove tBoxAge(0)
Set tBoxAge(0) = Nothing
End If
KeyAscii = 0
End Sub
Private Sub Form_Load()
con.ConnectionString = "Provider=sqlOLEDB;Persist Security Info=False;User ID=sa;PWD=密码;Initial Catalog=数据库名;Data Source=服务器名" '连接数据库字符串
con.Open
con.CommandTimeout = 20
res.Open "CaseSerchConfg",con,adOpenDynamic,adLockPessimistic
ListViewSearch.View = lvwReport '报表显示
ListViewSearch.ColumnHeaders.Add 1,"Condition",2050
ListViewSearch.GridLines = True
Call WindowProc(Me.hwnd,0)
End Sub
Private Sub Name_Click() '选中姓名
Dim itemA As ListItem
Dim i,x As Integer
res.MoveFirst
Do While Not res.EOF
If (res.Fields("Condition")) = "姓名" Then
For x = 1 To ListViewSearch.ListItems.Count
If ListViewSearch.ListItems(x).Text = "姓名" Then
MsgBox "该配置项中已经存在!"
Exit Sub
End If
Next x
Set itemA = ListViewSearch.ListItems.Add(,res.Fields("Condition"))
Set cBoxName = Controls.Add("VB.ComboBox","cBoxName")
Set tBoxName = Controls.Add("VB.TextBox","tBoxName")
LVW_COUNT = ListViewSearch.ListItems.Count
moveX = ListViewSearch.Width + 60
moveY = ListViewSearch.Top + 260 * (LVW_COUNT)
cBoxName.Visible = True
cBoxName.Move moveX,mWidth
For i = 1 To ConCount
If res.Fields(i).Value <> "" Then
cBoxName.AddItem res.Fields(i).Value
End If
Next i
tBoxName.Visible = True
moveX = moveX + cBoxName.Width
tBoxName.Move moveX,cBoxName.Height
End If
res.MoveNext
Loop
Set itemA = Nothing
End Sub
Private Sub Time_Click() '选中日期 Dim itemA As ListItem Dim i,x As Integer res.MoveFirst Do While Not res.EOF If (res.Fields("Condition")) = "时间" Then For x = 1 To ListViewSearch.ListItems.Count If ListViewSearch.ListItems(x).Text = "时间" Then MsgBox "该配置项中已经存在!" Exit Sub End If Next x Set itemA = ListViewSearch.ListItems.Add(,res.Fields("Condition")) Set cBoxTime = Controls.Add("VB.ComboBox","cBoxTime") cBoxTime.Visible = True LVW_COUNT = ListViewSearch.ListItems.Count moveX = ListViewSearch.Width + 60 moveY = ListViewSearch.Top + 260 * (LVW_COUNT) cBoxTime.Move moveX,mWidth For i = 1 To ConCount If res.Fields(i).Value <> "" Then cBoxTime.AddItem res.Fields(i).Value End If Next i End If res.MoveNext Loop Set itemA = Nothing End Sub
原文链接:https://www.f2er.com/vb/258819.html