I have a fairly simple Access Form to import new records when reports are run. Reports are run from an Oracle database (no, I can't just connect to it) and put into an Excel worksheet (only output option).
So, I just have to import the excel spreadsheet into Access.
The code that I'm currently using works fine (see below), however there are several things that I need to have done to the spreadsheet before Access imports it, to stop data type errors, etc.
Reports are run too frequently, and by too many different people to just have them change the formats and run code in Excel. (I tried, won't happen)
Here are the things I need to happen to the spreadsheet first:
1. Trim() ALL of the cells in the Used Range of the Sheet. (Oracle puts SO many trailing spaces in on export that a 10 character word won't fit in a text field!)
2. Delete Rows 1 and 2. (These are like the Report "Titles", the Header names are in Row 3.)
3. Delete the Last Row in the Used Range. (After the last valid record, there is a blank row, then a row with the text "End of the Report", so Access tries to import both that row and the blank one!)
4. Convert 2 of the columns to Date data types, or formats. (but not the header, just the rest of the column.)
5. Convert 7 of the columns (non-consecutive) to a Number data type or format (with no decimals)
6. And this one would be nice, but we can function without it - One column "ISBN" is numbers, however for some reason Oracle puts ' (single quotes)' around the number. It would be great to strip those.
Okay, I know that looks like a lot, but really it should only be a few commands. The way I see it I have 2 options.
1. Have VBA alter the Spreadsheet with the above updates BEFORE the DoCmd.TransferSpreadsheet. However, I can't seem to get that to work. Especially Trim, inserting new Columns, then trimming and dragging down all the cells in the columns, then pasting the Value (not formula) back into the original column, then deleting the column you had added. Very Complex....at least to me!
2. Create a "Temp" table, with all memo fields, Transfer the spreadsheet as is into that. Then run a few queries, one to trim, one to delete rows 1, 2 and Last, and convert those data types. Then An append query to take all those records and move them to the correct table. Then another query to change all the fields in "Temp" back to Memo, and finally delete all the records in the Temp table.
Either way sounds ridiculously complex to me. And regardless, I can't seem to figure out how to do either.
If anyone has any ideas/code that could make either of those options work....Please let me know. I would also GREATLY appreciate any other suggestions on another method of doing this that I haven't thought of! (Sorry about the length of the post, didn't mean to make you read a book!)
Thank you in advance!!
Private Sub FindReport_Click()
Dim fd As FileDialog
Dim objfl As Variant
Dim FileName As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.ButtonName = "Select"
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xls"
.Title = "Choose File to Import"
.InitialView = msoFileDialogViewDetails
.Show
For Each objfl In .SelectedItems
FileName = objfl
Next objfl
On Error GoTo 0
End With
SheetName = InputBox("Enter the Sheet Name")
DoCmd.TransferSpreadsheet acImport, , "NewWork", FileName, True, SheetName & "!"
End Sub
So, I just have to import the excel spreadsheet into Access.
The code that I'm currently using works fine (see below), however there are several things that I need to have done to the spreadsheet before Access imports it, to stop data type errors, etc.
Reports are run too frequently, and by too many different people to just have them change the formats and run code in Excel. (I tried, won't happen)
Here are the things I need to happen to the spreadsheet first:
1. Trim() ALL of the cells in the Used Range of the Sheet. (Oracle puts SO many trailing spaces in on export that a 10 character word won't fit in a text field!)
2. Delete Rows 1 and 2. (These are like the Report "Titles", the Header names are in Row 3.)
3. Delete the Last Row in the Used Range. (After the last valid record, there is a blank row, then a row with the text "End of the Report", so Access tries to import both that row and the blank one!)
4. Convert 2 of the columns to Date data types, or formats. (but not the header, just the rest of the column.)
5. Convert 7 of the columns (non-consecutive) to a Number data type or format (with no decimals)
6. And this one would be nice, but we can function without it - One column "ISBN" is numbers, however for some reason Oracle puts ' (single quotes)' around the number. It would be great to strip those.
Okay, I know that looks like a lot, but really it should only be a few commands. The way I see it I have 2 options.
1. Have VBA alter the Spreadsheet with the above updates BEFORE the DoCmd.TransferSpreadsheet. However, I can't seem to get that to work. Especially Trim, inserting new Columns, then trimming and dragging down all the cells in the columns, then pasting the Value (not formula) back into the original column, then deleting the column you had added. Very Complex....at least to me!
2. Create a "Temp" table, with all memo fields, Transfer the spreadsheet as is into that. Then run a few queries, one to trim, one to delete rows 1, 2 and Last, and convert those data types. Then An append query to take all those records and move them to the correct table. Then another query to change all the fields in "Temp" back to Memo, and finally delete all the records in the Temp table.
Either way sounds ridiculously complex to me. And regardless, I can't seem to figure out how to do either.
If anyone has any ideas/code that could make either of those options work....Please let me know. I would also GREATLY appreciate any other suggestions on another method of doing this that I haven't thought of! (Sorry about the length of the post, didn't mean to make you read a book!)
Thank you in advance!!
Private Sub FindReport_Click()
Dim fd As FileDialog
Dim objfl As Variant
Dim FileName As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.ButtonName = "Select"
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xls"
.Title = "Choose File to Import"
.InitialView = msoFileDialogViewDetails
.Show
For Each objfl In .SelectedItems
FileName = objfl
Next objfl
On Error GoTo 0
End With
SheetName = InputBox("Enter the Sheet Name")
DoCmd.TransferSpreadsheet acImport, , "NewWork", FileName, True, SheetName & "!"
End Sub