VB查询ACCESS数据库内容

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值