|
Question : Import Data from Unsaved Excel Spreadsheet to Access Table
|
|
I have an Access 2000 database application that creates and populates an Excel spreadsheet.
The spreadsheet then does some calculations and generates some new data. I would like to import this data back into one of my Access tables.
How do I do this?
I looked at the TransferSpreadsheet method, but it has a couple of problems: 1) It requires a filename, but I have not yet stored my spreadsheet so it doesn't have a name 2) It doesn't let you specify a worksheet name and my workbook has multiple worksheets
I am just using the spreadsheet because it has functionality that I can't replicate in Access (e.g. Solver, Normsinv() function, etc.) so I don't want to have to save it. And I need the multiple worksheets because of the shear quantity and complexity of the data I am working with.
Any ideas?
For now I am just going to use a brute force technique of creating INSERT statements data field by data field as I need to get something working this weekend. But I would appreciate something quicker and more elegant.
Thanks.
|
|
Answer : Import Data from Unsaved Excel Spreadsheet to Access Table
|
|
ok, reading what u want to do, u want the user to possibly keep the Excel version? But if u are saving to DB, why bother? U can always query the new table and dump in Excel format anyway
Okay, back to your question. I think the best way is to create and save a temp file
Create a tempname, one simple way is to use datetimesec e.g. sfile = Environ("Username") & format(now(),"YYYYMMDDHHNNSS")
file based on userid and datetime
or u can generate a random word and use that
say u save in c:\myexcel
You can also specify sheet names e.g. this code renames Sheet1 to George
Sheets(1).Name = "George"
You can then import e.g. Importing a file, I am specifying sheet Geoge and cells A1 to Z10
DoCmd.TransferSpreadsheet acImport, , "mylocaltable", "C:\MyExcel\" & sFile, True, "George$A1:Z100"
Once u do your import, u can delete the file kill "C:\MyExcel\" & sFile
All this is done without .Visible = True so user not aware of excel file being created,imported and deleted
Now u can output your query in Access. If they want to save it, u can either create a button which outputs using DoCmd.OutputTo and use acFormatXLS. This should prompt the user if u dont specify a filename
Now with what u are doing, I am sure you can do this all in VBA without the use of Excel. but if u do use addins like Solver and if u dont want to do your own calculations, then stick with this
|
|
|
|