D
Dwaine Horton
I have two database files. One contains a form with my code and I am using
DAO to connect to my database with my tables. What I want to do is have a
user select an Excel file and then I want to import the data from that file
into a table that I created on my database with the tables. I have tried
using transferspreadsheet and it transfer's the data but it puts it in the
wrong database. It is transferring to my database with the form and coding
and not the one with my tables.
How do I get this to transfer the data to my table database?
Here is my code:
Dim wsAccess As Workspace
Dim dbAccess As DAO.Database
Dim tdf As DAO.TableDef
Dim fldAccountName As DAO.Field
Dim fldPhone As DAO.Field
Dim fldAddr1 As DAO.Field
Dim fldAddr2 As DAO.Field
Dim fldAddr3 As DAO.Field
Dim fldAddr4 As DAO.Field
Dim fldCity As DAO.Field
Dim fldState As DAO.Field
Dim fldZip As DAO.Field
Dim blnHasFieldNames As Boolean
Dim strTable As String
'Create the Access workspace
Set wsAccess = DBEngine(0)
'Open a Microsoft Access database
Set dbAccess = wsAccess.OpenDatabase(AccessDataName, False, False)
'If the Account table exists delete it
If IsObject(dbAccess.TableDefs("tblAccount")) Then
dbAccess.TableDefs.Delete "tblAccount"
End If
'Create the table definition in memory
Set tdf = dbAccess.CreateTableDef("tblAccount")
'Create the field definitions in memory
Set fldAccountName = tdf.CreateField("Account_Name", dbText, 50)
Set fldPhone = tdf.CreateField("Site_Phone", dbText, 50)
Set fldAddr1 = tdf.CreateField("Address1", dbText, 50)
Set fldAddr2 = tdf.CreateField("Address2", dbText, 50)
Set fldAddr3 = tdf.CreateField("Address3", dbText, 50)
Set fldAddr4 = tdf.CreateField("Address4", dbText, 50)
Set fldCity = tdf.CreateField("City", dbText, 50)
Set fldState = tdf.CreateField("State", dbText, 2)
Set fldZip = tdf.CreateField("Zip", dbText, 6)
'Append the fields to the TableDef's Field collection
tdf.Fields.Append fldAccountName
tdf.Fields.Append fldPhone
tdf.Fields.Append fldAddr1
tdf.Fields.Append fldAddr2
tdf.Fields.Append fldAddr3
tdf.Fields.Append fldAddr4
tdf.Fields.Append fldCity
tdf.Fields.Append fldState
tdf.Fields.Append fldZip
'Append the TableDef to the Database's Tabledefs collection
dbAccess.TableDefs.Append tdf
'Refresh the Tabledefs collection
dbAccess.TableDefs.Refresh
Application.RefreshDatabaseWindow
Set fldAccountName = Nothing
Set fldPhone = Nothing
Set fldAddr1 = Nothing
Set fldAddr2 = Nothing
Set fldAddr3 = Nothing
Set fldAddr4 = Nothing
Set fldCity = Nothing
Set fldState = Nothing
Set fldZip = Nothing
'Does the Excel file have Field Names
'Set to True if there are field names
'Set to False if there are no field names
blnHasFieldNames = True
'strTable = "tblAccount"
strTable = tdf.Name
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, AccountDataName, blnHasFieldNames
DAO to connect to my database with my tables. What I want to do is have a
user select an Excel file and then I want to import the data from that file
into a table that I created on my database with the tables. I have tried
using transferspreadsheet and it transfer's the data but it puts it in the
wrong database. It is transferring to my database with the form and coding
and not the one with my tables.
How do I get this to transfer the data to my table database?
Here is my code:
Dim wsAccess As Workspace
Dim dbAccess As DAO.Database
Dim tdf As DAO.TableDef
Dim fldAccountName As DAO.Field
Dim fldPhone As DAO.Field
Dim fldAddr1 As DAO.Field
Dim fldAddr2 As DAO.Field
Dim fldAddr3 As DAO.Field
Dim fldAddr4 As DAO.Field
Dim fldCity As DAO.Field
Dim fldState As DAO.Field
Dim fldZip As DAO.Field
Dim blnHasFieldNames As Boolean
Dim strTable As String
'Create the Access workspace
Set wsAccess = DBEngine(0)
'Open a Microsoft Access database
Set dbAccess = wsAccess.OpenDatabase(AccessDataName, False, False)
'If the Account table exists delete it
If IsObject(dbAccess.TableDefs("tblAccount")) Then
dbAccess.TableDefs.Delete "tblAccount"
End If
'Create the table definition in memory
Set tdf = dbAccess.CreateTableDef("tblAccount")
'Create the field definitions in memory
Set fldAccountName = tdf.CreateField("Account_Name", dbText, 50)
Set fldPhone = tdf.CreateField("Site_Phone", dbText, 50)
Set fldAddr1 = tdf.CreateField("Address1", dbText, 50)
Set fldAddr2 = tdf.CreateField("Address2", dbText, 50)
Set fldAddr3 = tdf.CreateField("Address3", dbText, 50)
Set fldAddr4 = tdf.CreateField("Address4", dbText, 50)
Set fldCity = tdf.CreateField("City", dbText, 50)
Set fldState = tdf.CreateField("State", dbText, 2)
Set fldZip = tdf.CreateField("Zip", dbText, 6)
'Append the fields to the TableDef's Field collection
tdf.Fields.Append fldAccountName
tdf.Fields.Append fldPhone
tdf.Fields.Append fldAddr1
tdf.Fields.Append fldAddr2
tdf.Fields.Append fldAddr3
tdf.Fields.Append fldAddr4
tdf.Fields.Append fldCity
tdf.Fields.Append fldState
tdf.Fields.Append fldZip
'Append the TableDef to the Database's Tabledefs collection
dbAccess.TableDefs.Append tdf
'Refresh the Tabledefs collection
dbAccess.TableDefs.Refresh
Application.RefreshDatabaseWindow
Set fldAccountName = Nothing
Set fldPhone = Nothing
Set fldAddr1 = Nothing
Set fldAddr2 = Nothing
Set fldAddr3 = Nothing
Set fldAddr4 = Nothing
Set fldCity = Nothing
Set fldState = Nothing
Set fldZip = Nothing
'Does the Excel file have Field Names
'Set to True if there are field names
'Set to False if there are no field names
blnHasFieldNames = True
'strTable = "tblAccount"
strTable = tdf.Name
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, AccountDataName, blnHasFieldNames