My dear Forum
Access 2007 / XP
I am becoming more confident communicating with an Access Db from Excel. I have a process that works but I want to understand the differences between the different methods of connection.
I have four modules that all seem to connect in different ways. What's the difference and why might one be better/more appropriate than another?
I have five questions, all of which are at the bottom.
1. "OpenDatabase" - with Workspaces(0)
2. OpenDatabase - without Workspaces(0)
3. New Access.Application.OpenCurrentDatabase
4. ADODB connection/Microsoft Jet
Questions
A. What do we more officially call the different kind of connections I used in each example?
B. What does Workspaces(0) do? Why is it apparently optional (difference between method 1 & 2).
C. Are all of these opening and closing cleanly (I haven't omitted any closes or 'nothings' have I?)
D. Are any of these better at doing a particular type of task than others?
E. If I picked only one methodology to be consistent which should I use and why.
As ever, dear Forum, I have the honour to remain your most humble servant and thank you for your kind consideration!
Tom
Access 2007 / XP
I am becoming more confident communicating with an Access Db from Excel. I have a process that works but I want to understand the differences between the different methods of connection.
I have four modules that all seem to connect in different ways. What's the difference and why might one be better/more appropriate than another?
I have five questions, all of which are at the bottom.
1. "OpenDatabase" - with Workspaces(0)
Code:
Sub ExecuteSQL(strSQL As String, StatusMsg As String)
Dim Db As Database
Dim strAccessDBPath As String
With Application
strAccessDBPath = .[MasterPath].Value & .[DbPath].Value & .[DBFilename].Value
Set Db = Workspaces(0).OpenDatabase(strAccessDBPath, ReadOnly:=True)
Db.Execute (strSQL)
Db.Close
.StatusBar = False
End With
End Sub
2. OpenDatabase - without Workspaces(0)
Code:
Sub ExecuteQuery(strQry As String, StatusMsg As String)
Dim dba As Database
With Application
.StatusBar = StatusMsg
Set dba = OpenDatabase(.[MasterPath].Value & .[DbPath].Value & .[DBFilename].Value)
dba.Execute strQry
dba.Close
.StatusBar = False
End With
End Sub
3. New Access.Application.OpenCurrentDatabase
Code:
Sub InsertInAccess(strType As String, strFilePath As String, strTableName As String, strSpec As String, PnlDate As Date)
Dim strAccessDBPath As String
Dim acApp As Access.Application
Dim MasterPath As String
With Application
MasterPath = .[MasterPath].Value
strAccessDBPath = MasterPath & .[DbPath].Value & .[DBFilename].Value
Set acApp = New Access.Application
With acApp
acApp.OpenCurrentDatabase (strAccessDBPath)
If strType = "csv" Then
acApp.DoCmd.TransferText acImportDelim, strSpec, strTableName, strFilePath, False
Else
acApp.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, strTableName, strFilePath, True
End If
End With
End With
End Sub
4. ADODB connection/Microsoft Jet
Code:
Sub GetSQLResult(strSQL As String, sht As Worksheet, rng As Range)
Dim i As Integer, strAccessDBPath As String, dealRS As ADODB.Recordset, dealConn As ADODB.Connection
Set dealRS = New ADODB.Recordset
Set dealConn = New ADODB.Connection
With Application
strAccessDBPath = .[MasterPath].Value & .[DbPath].Value & .[DBFilename].Value
End With
With dealConn
.CursorLocation = adUseClient
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAccessDBPath & ";User Id=admin;Password=;"
.Open
End With
'Get the field headings out of the Db
dealRS.Open strSQL, dealConn, adOpenForwardOnly, adLockReadOnly
For i = 0 To dealRS.Fields.Count - 1
sht.rng.Offset(0, i).Value = dealRS.Fields(i).Name
Next
'Get the data recordset from the Db
If Not dealRS.EOF Then
sht.rng.Offset(1, 0).CopyFromRecordset dealRS
End If
Set dealRS = Nothing
If dealConn.State = adStateOpen Then
dealConn.Close
End If
Set dealConn = Nothing
End Sub
Questions
A. What do we more officially call the different kind of connections I used in each example?
B. What does Workspaces(0) do? Why is it apparently optional (difference between method 1 & 2).
C. Are all of these opening and closing cleanly (I haven't omitted any closes or 'nothings' have I?)
D. Are any of these better at doing a particular type of task than others?
E. If I picked only one methodology to be consistent which should I use and why.
As ever, dear Forum, I have the honour to remain your most humble servant and thank you for your kind consideration!
Tom