I have a program written within Excel VBA which at some point need to take a table from a worksheet, and use the data to create a new table in an Access database I have connected to prevoiusly.
Everything seems to be running OK, but when I look at the database afterwards, it is clear that the new table is in fact a link to the Excel sheet, rather than a table in it's own right (which is what I want).
Here is the code I am using:
Const DATABASE_PATH As String = "C:\workarea\trial.mdb"
Global strWorkbookPath As String
Sub Transfer_To_Access()
Dim ws As Worksheet
Dim dbTrial As Database
Dim rsInstrument As Recordset
strWorkbookPath = ThisWorkbook.Path
Set dbTrial = DBEngine.OpenDatabase(DATA
BASE_PATH)
For Each ws In ThisWorkbook.Worksheets
If Left(ws.Name, 6) = "(Data)" Then
If CheckTable(dbLimits, ws.Name) = True Then
Debug.Print ws.Name
Else
addTable dbTrial, ws.Name
End If
End If
Next ws
dbTrial.Close
Set ws = Nothing
Set dbTrial = Nothing
Set rsInstrument = Nothing
End Sub
Sub addTable(dbTemp As Database, strTableName As String)
Dim tblNewInstrument As TableDef
Dim dataSource As String
dataSource = "Excel 8.0;DATABASE=" & strWorkbookPath & "\" & _
ThisWorkbook.Name
strTableName = "TrialTable"
Set tblNewInstrument = dbTemp.CreateTableDef(Name
:=strTable
Name)
With tblNewInstrument
.Connect = dataSource
.SourceTableName = strTableName
End With
dbTemp.TableDefs.Append tblNewInstrument
Set tblNewInstrument = Nothing
End Sub
Public Function CheckTable(dbTemp As Database, strTableName As String) As Boolean
Dim rsTemp As Recordset
Dim strSQL As String
Dim result As Boolean
result = True
strSQL = "SELECT * FROM [" & strTableName & "];"
On Error GoTo noTable
Set rsTemp = dbTemp.OpenRecordset(strSQ
L, dbOpenSnapshot)
CheckTable = result
Exit Function
noTable:
result = False
Resume Next
End Function
If anyone can help me to get a proper table in Access rather than just a link to an Excel sheet you would stop me from driving myself insane!
Thanks
#D