M
MikeF
Posted this earlier, but now have some clarifying code below, converted from
an Access macro then modified for this example.
Previous post:
Need to set up a VBA sub-routine in Access that will import seven named
ranges [Rng1 thru Rng7] from Excel workbooks into seven different tables
in Access [tbl1 thru tbl7].
There are *numerous* Excel workbooks in *numerous* directories.
Therefore any routine that has "import from c:\access\etc" statically
implanted in it will not work.
Note - All Excel workbooks contain the same range names though.
The seven tables in Access are always the same, and have been constructed to
accept the named ranges from those Excel workbooks without errors.
1. The *first* thing that has to happen ...
Initiate the routine, and an "Import from which file?" dialog box comes up,
just like a regular "File Open".
Specifically this would be the "Get External Data - Excel Spreadsheet"
window in Access.
2. Then, once I chose which Excel file to import the ranges from, the vba
routine would continue, importing ranges 1 thru 7 from that file into the
current database.
Any assistance would be sincerely appreciated.
Thank you in advance.
Regards,
- Mike
As follows is the converted, modfied macro.
Thinking a bit like Excel vba, which I'm much more familiar with, see
between the tilde-strings.
Believe I just need to have the "which spreadsheet" dialog box open, choose
the Excel sheet/path, and assign the MyExcelFile variable to it.
Signed - Desperate for this Solution ...!!! <g>
Function ImportExcel ()
On Error GoTo ImportExcel_Err
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
INSERT ALL CODE HERE FOR – MyExcelFile = Open “Get External Data – Get
Spreadsheet†dialog box to choose what file will become “MyExcelFile†in this
variable.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DoCmd.TransferSpreadsheet acImport, 10, "tbl1", "MyExcelFile", True,
"Rng1"
DoCmd.TransferSpreadsheet acImport, 10, "tbl2", "MyExcelFile", True,
"Rng2"
DoCmd.TransferSpreadsheet acImport, 10, "tbl3", "MyExcelFile", True,
"Rng3"
DoCmd.TransferSpreadsheet acImport, 10, "tbl4", "MyExcelFile", True,
"Rng4"
DoCmd.TransferSpreadsheet acImport, 10, "tbl5", "MyExcelFile", True,
"Rng5"
DoCmd.TransferSpreadsheet acImport, 10, "tbl6", "MyExcelFile", True,
"Rng6"
DoCmd.TransferSpreadsheet acImport, 10, "tbl7", "MyExcelFile", True,
"Rng7"
ImportExcel_Exit:
Exit Function
ImportExcel_Err:
MsgBox Error$
Resume ImportExcel_Exit
End Function
an Access macro then modified for this example.
Previous post:
Need to set up a VBA sub-routine in Access that will import seven named
ranges [Rng1 thru Rng7] from Excel workbooks into seven different tables
in Access [tbl1 thru tbl7].
There are *numerous* Excel workbooks in *numerous* directories.
Therefore any routine that has "import from c:\access\etc" statically
implanted in it will not work.
Note - All Excel workbooks contain the same range names though.
The seven tables in Access are always the same, and have been constructed to
accept the named ranges from those Excel workbooks without errors.
1. The *first* thing that has to happen ...
Initiate the routine, and an "Import from which file?" dialog box comes up,
just like a regular "File Open".
Specifically this would be the "Get External Data - Excel Spreadsheet"
window in Access.
2. Then, once I chose which Excel file to import the ranges from, the vba
routine would continue, importing ranges 1 thru 7 from that file into the
current database.
Any assistance would be sincerely appreciated.
Thank you in advance.
Regards,
- Mike
As follows is the converted, modfied macro.
Thinking a bit like Excel vba, which I'm much more familiar with, see
between the tilde-strings.
Believe I just need to have the "which spreadsheet" dialog box open, choose
the Excel sheet/path, and assign the MyExcelFile variable to it.
Signed - Desperate for this Solution ...!!! <g>
Function ImportExcel ()
On Error GoTo ImportExcel_Err
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
INSERT ALL CODE HERE FOR – MyExcelFile = Open “Get External Data – Get
Spreadsheet†dialog box to choose what file will become “MyExcelFile†in this
variable.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DoCmd.TransferSpreadsheet acImport, 10, "tbl1", "MyExcelFile", True,
"Rng1"
DoCmd.TransferSpreadsheet acImport, 10, "tbl2", "MyExcelFile", True,
"Rng2"
DoCmd.TransferSpreadsheet acImport, 10, "tbl3", "MyExcelFile", True,
"Rng3"
DoCmd.TransferSpreadsheet acImport, 10, "tbl4", "MyExcelFile", True,
"Rng4"
DoCmd.TransferSpreadsheet acImport, 10, "tbl5", "MyExcelFile", True,
"Rng5"
DoCmd.TransferSpreadsheet acImport, 10, "tbl6", "MyExcelFile", True,
"Rng6"
DoCmd.TransferSpreadsheet acImport, 10, "tbl7", "MyExcelFile", True,
"Rng7"
ImportExcel_Exit:
Exit Function
ImportExcel_Err:
MsgBox Error$
Resume ImportExcel_Exit
End Function