I have this snippet that will import an access table into excel. the MDB path is in range C2 and the table name is is C4
Is there a way I can import the table's properties/design and write it into a new place using excel vba? This will be used for many people of varying skill levels with varying table structures to import. The data will eventually have to go back into access but I am getting tripped up on how to make sure the field properties are correct within access.
Sub GetData()
DeleteConnections 'remove existing connections in case they persist
Sheet4.Cells.Clear 'clear the old table
Sheets("Import").Activate
DatabaseName = Sheets("Setup").Range("C2").Value
TableName = Sheets("Setup").Range("C4").Value
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=" & DatabaseName & "" _
, _
";Mode=Share" _
, _
" Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OL" _
, _
"EDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet" _
, _
" OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Loc" _
, _
"ale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet" _
, " OLEDB:Bypass UserInfo Validation=False"), Destination:=Range("$A$1")). _
QueryTable
.CommandType = xlCmdTable
.CommandText = Array("" & TableName & "")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = _
"" & DatabaseName & ""
.ListObject.DisplayName = "" & TableName & ""
.Refresh BackgroundQuery:=False
End With
DeleteConnections 'remove the new connection
End Sub
TableDefs
object for the specific database. This will give you some helpful pointers to start. – Scott Holtzman