參考實例如下:
Public cnn As adodb.Connection
Public rst As adodb.Recordset
Public Sub setdb()
Set cnn = New adodb.Connection
cnn.Provider = "Microsoft.Jet.OLEDB.4.O"
cnn.ConnectionString = "Data Source=" & App.Path & "\personal.mdb;"
cnn.CursorLocation = adUseClient
cnn.Open
End Sub
Private Sub Form_Load()
On Error Resume Next
Dim Strsql As String
setdb
Set DataGrid1.DataSource = Nothing
Strsql = "select * from 備忘錄 order by 編號"
Set rst = cnn.Execute(Strsql)
If Err Then
MsgBox "讀取數據失敗"
Exit Sub
End If
If rst.EOF Then
Set DataGrid1.DataSource = Nothing
Else
Set DataGrid1.DataSource = rst
End If
End Sub