I am trying to import Excel file into an Access table. each time i do an import, the columns from the Excel file may vary in the number of columns as well as their names. It was suggested that i use the TransferSpreadsheet function in Access using a Temporary table.
If you can suggest a solution to the original problem of importing an Excel file where the columns vary, please do so.
Access will automatically create a table for you. All tables are temporary. One day they'll go.
Are you asking "How do I automatically delete tables I have finished with?". I don't think Access has any specific feature to do this. You have to delete tables yourself or write code/macros to do it for you.
However i am trying to import the Excel file through VBA, to make a more user friendly interface; and not through the Access menu of File, Get External Data, Import ....
In VBA code
1. make an ADO connection to the excel spreadsheet.
2. send an SQL statement from the recordset method.
i.e. "Select * from sheeet1"
3. read the meta data returned in the recordset fields collection. Take each field name in the collection and build an SQL table create statement.
4. run the table create statement.
5. create a query to do Insert Into the access table from the excel spreadsheet table or insert record by record whichever you prefer.
i.e "Insert Into AccessTable Select * from sheet1
Here is an example of putting the excel data in an ADO recordset.
Public Function ReadExcel()
On Error GoTo ErrHandler
Dim cn As New ADODB.Connection, cn2 As New ADODB.Connection
Dim rs As New ADODB.Recordset, rs2 As New ADODB.Recordset
Dim connString As String, connString2 As String
Dim sql1 As String, sql2 As String
connString = "provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\ATestDir\myTest.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
cn.ConnectionString = connString
cn.Open connString
''cn2.Open connString
Set rs.ActiveConnection = cn
'-- sheet name = newcustomers
sql1 = "select * from newcustomers"
rs.Open sql1, cn, adOpenForwardOnly, adLockReadOnly
If Not (rs.EOF = True) Then
Debug.Print "field value = "; rs.Fields(0).Value
End If
Exit Function
ErrHandler:
Dim er As ADODB.Error
Debug.Print " In Error Handler "; Err.Description
For Each er In cn.Errors
Debug.Print "err num = "; Err.Number
Debug.Print "err desc = "; Err.Description
Debug.Print "err source = "; Err.Source
Next
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.