VB查询ACCESS数据库内容

来源:互联网 发布:sopcast这款网络电视 编辑:程序博客网 时间:2024/05/29 02:18
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command

Private Sub Command2_Click()
On Error Resume Next
'Adodc2.RecordSource = "select * from A2 where 人口=" & Trim(Text5.Text)
Adodc2.RecordSource = "select * from A2 where 人口>=" & Text5.Text & " And 人口 <= " & Text6.Text & ""


Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
End Sub

Private Sub Command3_Click()
Adodc2.RecordSource = "select * from A2 where 省份='" & Text7.Text & "'"
Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
End Sub


Private Sub Command4_Click()

'Adodc2.RecordSource = "select * from A2 where 时间 = #2002/7/5#"
'Adodc2.RecordSource = "select * from A2 where 时间 > #2000/1/1# and 时间< #2001/1/2#"
'Adodc2.RecordSource = "select * from A2 where 时间 between #2000/1/1# and #2000/2/2#"
'Adodc2.RecordSource = "select * from A2 where 时间 =#" & Format(Text8.Text, "yyyy/mm/dd") & "#"
Adodc2.RecordSource = "select * from A2 where 
时间 between #" & Format(Text8.Text, "yyyy/mm/dd") & "# and #" & Format(Text9.Text, "yyyy/mm/dd") & "# "



Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
End Sub

Private Sub Command5_Click()
'Adodc2.RecordSource = "select * from A2 where 时间 =#" & Format(DTPicker1.Value, "yyyy/mm/dd") & "#"
'Adodc2.RecordSource = "select * from A2 where 时间 >#" & Format(DTPicker1.Value, "yyyy/mm/dd") & "# and 时间 <#" & Format(DTPicker2.Value, "yyyy/mm/dd") & "# "
Adodc2.RecordSource = "select * from A2 where 时间 between #" & Format(DTPicker1.Value, "yyyy/mm/dd") & "# and #" & Format(DTPicker2.Value, "yyyy/mm/dd") & "# "


Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
End Sub

Private Sub Form_Load()
cnn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & App.Path & "\db_k1.mdb" '连接数据库
Set cmd.ActiveConnection = cnn
Me.WindowState = 2
Adodc1.CommandType = adCmdText
Adodc2.CommandType = adCmdText
End Sub

Private Sub Command1_Click()        '单击"查询"按钮
On Error GoTo err_1         '如果发生错误,则去执行err_1后面的语句
cmd.CommandText = "select * from A2 where 城市='" & Text1.Text & "'"  '查询文本框1中的内容

'cmd.CommandType = adCmdText
cmd.CommandTimeout = 15
Set rs = cmd.Execute

Text2.Text = rs.Fields("省份")
Text3.Text = rs.Fields("城市")
Text4.Text = rs.Fields("人口")

Adodc1.Recordset.MoveFirst
'Adodc1.Recordset.Find "省份=" & Text2.Text
'Adodc1.Recordset.Find "城市 = '苏州'"
Adodc1.Recordset.Find "城市= '" & Text3.Text & "'"

Adodc2.RecordSource = "select * from A2 where 城市='" & Text3.Text & "'"
Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
Exit Sub

err_1:
MsgBox "未找到你所查询的结果"
End Sub

0 0
原创粉丝点击