VB连接ACCESS例子及全部代码
VB连接ACCESS例子及全部代码
2007-03-19 13:40:32| 分类: sql相关|字号 订阅
1.首先创建一个新工程,取名为OpenSql,即Open Sql的缩写,意思是在Open方法中采用Sql查询。
2.打开“引用”对话框,选取 Microsoft ActiveX Data Object 2.1 Library
3.打开vb listview控件“部件”对话框,选取 Microsoft Windows Common Control 6.0,工具箱中便添加了若干控件,其中一个名为ListView的控件.
4.将控件ListView控件加载到窗体中,取名为ListView1.窗体中中有这样一个控件,别无其他,所以是一个特别简单的界面。
5.在代码窗口中的全部代码如下。
Dim WithEvents myConnection As ADODB.Connection
Dim myRecordset As New ADODB.Recordset
Dim myRecordset As New ADODB.Recordset
Private Sub Form_Load()
Dim myPath As String
Dim myPath As String
Dim srtConec As String
Set myConec = New ADODB.Connection
myPath = App.Path & "\TD.mdb;"
strconec = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & myPath
myConec.Open strconec
'If strconec <= 0 Then
' MsgBox "Connection Failed!"
'Else
' MsgBox "Congratulations,Established!"
'End If
Dim strSql As String
ListView1.Top = 0
ListView1.Left = 0
myRecordset.CursorType = adOpenKeyset
myRecordset.LockType = adLockReadOnly
Set myConec = New ADODB.Connection
myPath = App.Path & "\TD.mdb;"
strconec = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & myPath
myConec.Open strconec
'If strconec <= 0 Then
' MsgBox "Connection Failed!"
'Else
' MsgBox "Congratulations,Established!"
'End If
Dim strSql As String
ListView1.Top = 0
ListView1.Left = 0
myRecordset.CursorType = adOpenKeyset
myRecordset.LockType = adLockReadOnly
strSql = "select * from TBillInfo"
myRecordset.Open strSql, myConec, , , adCmdText
ShowListView
myRecordset.Close
myConec.Close
End Sub
Public Sub ShowListView()
Dim clmHead As ColumnHeader
Dim ListItm As ListItem
Dim i As Integer
ListView1.ColumnHeaders.Clear
ListView1.ListItems.Clear
ListView1.FullRowSelect = True
ListView1.View = lvwReport
For i = 0 To myRecordset.Fields.Count - 1
myRecordset.Open strSql, myConec, , , adCmdText
ShowListView
myRecordset.Close
myConec.Close
End Sub
Public Sub ShowListView()
Dim clmHead As ColumnHeader
Dim ListItm As ListItem
Dim i As Integer
ListView1.ColumnHeaders.Clear
ListView1.ListItems.Clear
ListView1.FullRowSelect = True
ListView1.View = lvwReport
For i = 0 To myRecordset.Fields.Count - 1
Set clmHead = ListView1.ColumnHeaders.Add()
clmHead.Text = myRecordset.Fields(i).Name
Next
myRecordset.MoveFirst
Do While Not myRecordset.EOF
Set ListItm = ListView1.ListItems.Add()
If IsNull(myRecordset.Fields(0).Value) Then
ListItm.Text = "NULL"
Else
ListItm.Text = myRecordset.Fields(0).Value
End If
For i = 1 To myRecordset.Fields.Count - 1
If IsNull(myRecordset.Fields(i).Value) Then
ListItm.SubItems(i) = "NULL"
Else
clmHead.Text = myRecordset.Fields(i).Name
Next
myRecordset.MoveFirst
Do While Not myRecordset.EOF
Set ListItm = ListView1.ListItems.Add()
If IsNull(myRecordset.Fields(0).Value) Then
ListItm.Text = "NULL"
Else
ListItm.Text = myRecordset.Fields(0).Value
End If
For i = 1 To myRecordset.Fields.Count - 1
If IsNull(myRecordset.Fields(i).Value) Then
ListItm.SubItems(i) = "NULL"
Else
ListItm.SubItems(i) = myRecordset.Fields(i).Value
End If
Next
myRecordset.MoveNext
Loop
ListView1.View = lvwReport
End Sub
Private Sub Form_Resize()
ListView1.Width = Width - 200
ListView1.Height = Height - 400
End Sub
End If
Next
myRecordset.MoveNext
Loop
ListView1.View = lvwReport
End Sub
Private Sub Form_Resize()
ListView1.Width = Width - 200
ListView1.Height = Height - 400
End Sub
Private Sub Form_Terminate()
Set myRecordset = Nothing
Set myConnection = Nothing
End Sub
Set myRecordset = Nothing
Set myConnection = Nothing
End Sub
Private Sub ListView1_BeforeLabelEdit(Cancel As Integer)
End Sub
---------------------------
---------------------------
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论