MS Access: Link to Excel file with header on row X > 1? - ms-access

I need to link to an Excel file in Access but there are a bunch of needless rows that come before the actual header + data. Is there a way to skip the first few rows when importing this Excel file and tell it where the header is?
Note: I cannot modify the source Excel file directly. The files are provided as-is.

Link the sheet using the Range parameter with DoCmd.TransferSpreadsheet, as in this example from the DoCmd.TransferSpreadsheet Method help topic.
DoCmd.TransferSpreadsheet acImport, 3, _
"Employees","C:\Lotus\Newemps.wk3", True, "A1:G12"

Related

How would I transfer and replace my link specifications from one MS Access file to another using vba?

I currently have an Access file with about 30 custom link specs. I would like to transfer these specs into about 30 different access files using VBA. The end goal of this would be to automate quarterly txt data being put into Access according to certain link specifications. I need a code that will replace the tables MSysIMEXSpecs and MSysIMEXColumns in the new Access file with the tables in the main Access file that contains all the VBA code. If you could go one step farther and explain how to then use these specs to import a comma delimited txt file using the spec, that would be great.
Thanks
I believe these two lines of code (Display below) did the job:
DoCmd.TransferDatabase acExport, "Microsoft Access", DatabaseFullPath, acTable, "MSysIMEXColumns", "MSysIMEXColumns"
DoCmd.TransferDatabase acExport, "Microsoft Access", DatabaseFullPath, acTable, "MSysIMEXSpecs", "MSysIMEXSpecs"

MS Access unable to locate my excel range when importing

I have this code that works on one spreadsheet, but not another. I am just trying to automate the transfer of an excel data range to an access table, like so
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "UsysFastTrack", strFilePath, False, strRange
strFilePath and strRange are just strings that contain the full file path (including the worksheet name and extension) and the name of an excel range in the worksheet, respectively. This line causes an error
The Microsoft Access database engine could not find the object ...
This error appears a lot online and somewhere I saw the advice to try the import wizard to see what I get and lo and behold, right as I hit the last Next
This is the exact same error and it stops me dead in my tracks. What's funny is that you can see the range exists in the spreadsheet before your very eyes.
What's going on here?
Looks like the names of your named ranges are not valid, they are similar to regular ranges. Try to change names

VBA Importing long file names

I have a .csv file on my Desktop. The file name is very long and includes special characters such as brackets, e.g.:
[ABCD 012015] ACCT 1117 - Section A10 Grades-20150316_1937-comma_separated.csv
where the first 36 characters of the filename are a constant. The remainder of the file name changes upon each instance of a download.
I have tried VBA to import the file into an Access with a DoCmd.TransferText and I get errors. I have discovered that there is a limit to the length of a filename that the DoCmd.TransfertText can handle.
So, I need to first rename the file manually, then I can use VBA. Renaming a file is relatively easy with VBA, but I would like to use the info provided in the original filename such as Section A10 (which would make it unique, since there are other Sections labelled A01, A02, etc.) and rename the .csv file as A10.csv, so this means searching for a string AND replacing it. Since the Section can be different, how do I write the code and rename the file then import it with VBA?
So, far I have bits and pieces, but cannot put them together:
Name OldPathName As NewPathName
DoCmd.TransferText acImport, "AME_Grades", strTable, strPathFile, blnHasFieldNames
I am using an import specification AME_Grades to make it cleaner in the Access table.
Any suggestions? TIA
I'd suggest to use FileCopy function before you start importing data from csv file into MS Access.
Dim OldPathName As String, NewPathName As String
OldPathName = "FullPathToVeryVeryLonLongLongFileName.csv"
NewPathName = "FullPathToShortFileName.csv"
FileCopy OldPathName, NewPathName
DoCmd.TransferText acImport, "AME_Grades", strTable, NewPathName, blnHasFieldName

MS Access 2007: Add date to Excel Export

I currently have a 'Save Export' task that is saving a table (+1 million records) to an .xlsx file on a SharePoint folder via mapped network drive. I want to add the date to the file Excel file name.
Currently for Export-Excel Spreadsheet:
File Name is: U:\Reporting\Extracts\Excel_filename.xlsx
File format: Excel Workbook (*.xlsx)
Under Specify export options: I don't have anything checked.
With Date:
File Name is: U:\Reporting\Extracts\Excel_filename_mm_dd_yyyy.xlsx
File format: Excel Workbook (*.xlsx)
Under Specify export options: I don't have anything checked.
I would want the final file name to be: Excel_filename_09_18_213.xlsx
I don't want to use VBA because creating the .xlsx file takes over 4 hours. Export the table directly to the SharePoint folder is faster, but I need to update the file name with the current date. Could I create a macro that adds the date to the file name before it is posted to SharePoint? Is there a 'RunCommand' or 'RunCode' command that I could run that would generate the file name with the date?
I have tried the following for the file name, and they didn't work. I get "Failed creating file." "The specification failed to execute. Try re-creating the specification.":
U:\Reporting\Extracts\Excel_filename&(Format(Date()),"yymmdd"))&.xlsx
U:\Reporting\Extracts\Excel_filename%Date:~12,2%%Date:~4,2,%%Date:7~2%.xlsx
"U:\Reporting\Extracts\Excel_filename"&(Format(Date()),"yymmdd"))&".xlsx"
U:\Reporting\Extracts\Excel_filename_(Format(Date()),"yymmdd")).xlsx
Many thanks in advance.
I'd use VBA for this task. I wouldn't save it directly from Access onto Sharepoint folder however, I'd save it to a temporary location on the local disk and then copy it over - much quicker. If you are talking about pulling the data from a local Access file - then it shouldn't be taking 4 hours.
DoCmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel7, "myAccessTable", _
"C:\MyExcelExport_" & format(date(),"yyyy-mm-dd") & ".xls", True
on the macro line set destination
="C:\YourFolder\fileName" & Format(Date(),"ddmmyy") & ".xls"

simple import form in Access

how to create an access form which has import excel file button. and after selecting excel file it automatically creates a table in the database with collumn headers as excel first row and data as excel other rows. if you think i am not putting any effort please give me suggestion or reference and ill do it on my own.
For versions of Access since 2003, you can use the File Dialog to allow the user to browse for the file they want, prior to that, you can use API calls. If this is overkill for you, you can have the user type in the file name and path, but you will have to check that it exists using code (Dir may suit).
It would be best to use TransferSpreadsheet method of the DoCmd object (available in any version of Access from, AFAIK, 1997 onward) to import the spreadsheet. This can be run as VBA (code) or a macro.
If we assume that you are able to create a form and wire up a button you have two issues:
The file open dialog.
Triggering the import.
For 1 you should be able to use the standard Microsoft file dialogs - my VB.OLD and Access are spectacularly rusty (no access 2007) but you can reference the appropriate COM assemblies from Access after which it becomes fairly easy.
2 is a bit more interesting - I beleive you can pretty much do this by menu selection from within access in which case, at least as a first step, you should be able to automate the same steps - pretty much anything you can do from a menu you can also do by calling the relevant command from VBA. The more complex solution would be to create VBA logic to create a linked table that links to the Excel file and then do a create table query and then drop the link.
In terms of effort, the form is something one would expect you to be able to do without much help - however automating something like an import from excel is not necessarily obvious.
An example using Access 2003 would be as follows for selecting a file:
Dim fDialog As Office.FileDialog
Dim strFile As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.InitialFileName = "C:\temp\*.xls"
.Filters.Clear
.Filters.Add "Excel file", "*.xls"
.Filters.Add "All Files", "*.*"
If .Show = True Then
strFile = .SelectedItems(1)
End If
End With
Debug.Print strFile
Note you would need to add a Reference to the Office 12 Object Library
To Import the file you can use the TransferSpreadsheet Function of the DoCmd Object. For E.g.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "ExcelImport", strFile, True
The Access table called ExcelImport would have to already exist in the database.