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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章