VB连接ACCESS例子及全部代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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.打开“部件”对话框,选取Microsoft Windows Common Control 6.0,工具箱中便添加了若干控件,其中一个名为ListView的控件.
4.将控件ListView控件加载到窗体中,取名为ListView1.窗体中中有这样一个控件,别无其他,所以是一个特别简单的界面。

5.在代码窗口中的全部代码如下。

Dim WithEvents myConnection As ADODB.Connection
Dim myRecordset As New ADODB.Recordset
Private Sub Form_Load()
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
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
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
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
Private Sub Form_Terminate()
Set myRecordset = Nothing
Set myConnection = Nothing
End Sub
Private Sub ListView1_BeforeLabelEdit(Cancel As Integer) End Sub
---------------------------。

相关文档
最新文档