Creating a linked table to an Excel worksheet
Hi Guys,
Not sure if this belongs in the VBA or MS Access forums, so I'll post it to both.
I'm writing an application in MS Access that needs to read an Excel worksheet as a table. It contains customers details and the Access application will be opening it to read it only. No updates, no additions.
The full path name to the Excel spread sheet is contained in the variable CliData and the name of the worksheet in the XL file is called CUSTTABLE. For simplicity, I will create the table name in Access as CUSTTABLE also.
So, my code looks like this:
Dim DataFile As TableDef
Set DataFile = CurrentDb.CreateTableDef("CUSTTABLE")
DataFile.Connect = "Excel 8.0;database=" & CliData
DataFile.SourceTableName = "CUSTTABLE"
CurrentDb.TableDefs.Append DataFile
When I execute the code, it steps to the last line, when it attempts to append it to the current DB and gives the error message:
The Microsoft Jet database engine could not find the object 'CUSTTABLE'. Make sure the object exists and that you spell its name and the path name correctly.
Well, I've checked the path name and it's OK and I know the name of the XL table is correct, so the only thing I can think of is that I should be using something other than the SourceTableName to define worksheet that is to be accessed.
I can manually ccreate a linked table to this data and all is good, however I need to make it easy for the users to change which XL spreadsheet they are getting the data from, so I need to be able to create the linked table programmatically.
Has anyone tried this before?
Hope this makes sense.
Can anyone help?
TIA
Alan
|