Access VBA - Import problems - excel

I'm importing some data form Excel into Access and I'm facing some strange issues.
Problem:
I'm using DoCmd.TransferSpreadsheet Method to import Excel data into Access like this :
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Excel_Data", "Filename", True
This "Excel_Data" table is not pre-created so Access creates It on It's own. Why ? If I pre-create It, then User has to import data to Destination table from Excel in exactly same order (column A in Excel is row 1 in Access etc..).
But If you don't pre-create It, then Access creates whatever table there is in Excel and you can Import only data that you wish - based on column names. Now here is where It get's stucked....
I don't know why, but on every other Imports I do like this, Access creates only Text fields - and then my Import to destination table works.
But in one of the Imports Access creates a Number field, and then Import into destination table doesn't work anymore. All Excel data are formatted as general.
Does anybody know how to avoid this ???
Basically I want just to Import excel data into Access, based on column names, in whatever column order there is in Excel.
thanks for help !

I would suggest to use the query like this instead of TransferSpreadsheet
SELECT * INTO Table1
FROM [Sheet1$D3:E24]
IN "C:\Temp\Test.xls" [Excel 12.0;HDR=YES;IMEX=1];
Note, that IMEX=1 allows you to suppress data type guessing and Access will create always text fields.
Also this will allow you to import data from specified ranges of spreadsheet and use WHERE clause for filtering unnecessary data

Thanks for all answers, but I have solved this on my own. Best thing to avoid any problems when doing Import is simply using ALTER Table command after DoCmd.TransferSpreadsheet method is done. I just altered all my columns to Text format, changed all my other tables to Text fields and now every manipulating with data works just fine.

Related

Import certain fields in Excel into Access based on field name -

I will be obtaining a few dozen MS Excel spreadsheets with 500+ columns and need to import just selected few columns into MS Access based on their column name. I have no control over this and yes, there doesn't need to be that many columns. I want to import a few selected fields only. In particular, I only want to import columns that have "c_" in the column name. The problem is I can't figure out how to do this. The other problem is the number of columns will vary each time, but I will always need the columns that start with "c_".
Normally when I use VBA code to import Excel data, it's not too difficult for me if I have to select a range. For example:
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel9, "ards_data_new", "H:\Misc. Projects\ARDS project\ards_data.xlsx", True, "ards_data!A1:A50"
The problem is that the range will vary for each file, so I can't do it this way. I wish there was a way I could use the command Like "c_" in obtaining the columns I need only, but I just don't know how. Any help would be appreciated.

How to Link a Excel Table with Access and prevent NULL Values due to wrong Data Type Conversion?

In the current Project i Need to Keep a Excel File which gets Values from a Machine to the Access Database to work with them and Import them in the Data Model.
Problem is some of the Values give invalid results due to the way they are saved. For example the timestamp is saved like
030420 instead of 03:04:20 and Access cant handle that and gives me a #NUMBER
I can not simply Change the datatype in Excel because the whole Excel gets refreshed every hour by a source that i cant influence.
Any help appreciated.
If Erik's proposal does not work, you can
- create a backup copy of your Excel source
- tweak the file: enter text in the first row of the problematic columns
- link the tweaked file into Access
- put back the real file in place.
Now the problematic columns should be read as Text, and you can build a query that solves any issue like conversion, null handling...
Link, don't import, the Excel file, and you have a linked table.
Now, use this linked table as source in a simpel select query where you modify the data and alias the fields as needed. For example:
Select
F1 As SomeName,
F2 As OtherName,
TimeSerial(Mid([F5],1,2),Mid([F5],3,2),Mid([F5],5,2)) As TrueTime
From
LinkedTable
Where
F7 Is Not Null
The use this query for your import.
Consider querying the Excel file instead of using a linked table.
The query can directly query an Excel range:
SELECT * FROM
[Excel 12.0 XML;DATABASE=PathToMyExcel;HDR=Yes;IMEX=1].[MyRange] t
Then, you can use functions like TimeSerial to cast numbers to time values.

Access VBA to import dynamic excel spreadsheets as text

I am looking for a way to use access vba to import an entire excel spreadsheet as text. The hard part with this one is that the import needs to be dynamic. I know that normally an import can easily be accomplished via a saved import or using a DoCmd.TransferSpreadsheet method, but unfortunately those do not suit my needs as the number of columns and certain headernames may change between files (these are files from clients and therefore I have no control over how they come in). Obviously performance is not a priority for this method.
Sudo-code:
Dim db AS DAO.Database
Dim file_to_import AS String
Set db = CurrentDb
Set file_to_import = "C:/Foo/bar/Access/Imports/FORIMPORT.xlsx"
'Read headers of spreadsheet into array
Loop through header row (A1:ZZ1) and create an array
'Create table creation string based on array
Creation_String = "CREATE TABLE TBL_Import ([Array_Element1] TEXT(255), [Array_Element2] TEXT(255), [Array_Element3] TEXT(255), ...etc, etc"
'Create TBL_Import
CurrentDb.Execute Creation_String
'Import data
'(Imports data from file_to_import into TBL_Import)
Somewhat similar to this question, may be able to use this as a starting point:
[MS Access VBA script to interface with Excel
Thanks in advance for the help with this. I know some code but obviously I am still learning the ins and outs of VBA.
The easy workaround is not to import the sheet but link it.
Then you have an attached (linked) table you can use as source in a simple select query where you modify and/or convert values.
Now, use this query for your further processing.

CSV Connection into Named Table not Range

If you make a data connection to another Excel file or an Access table it will import that data into excel as a Table. This is great! But when I do the same with "From Text" and choose a CSV, it loads no problem, but it loads as a RANGE not a TABLE. This is highly frustrating as I NEED it to be in the table format so that I can take advantage of the dynamic column names.
Am I missing a tickbox somewhere? I'm not opposed to using VBA but it really seems odd that Excel can't do CSV to Table so I'm hoping for a native solution.
I should mention that if it's a VBA solution, it CANT break the workbook. So if I'm doing something like:
=SUMIFS(CSVDATA[SalesDollars], CSVDATA[RepName], "BOB")
It will still work after refreshing.
So you can get around the 'data connection' not creating a table by downloading the MS Excel 2010 plugin (from Microsoft) called Power Query. It's free and is a default feature in MS Excel 2013.
It will allow you to choose to create a Power Query Table from External Data >> From File >> From CSV
Upon doing this, it will create a named table for you and a Power Query object attached to the table. You can use the table itself the same way you normally would - with the Columns as references for formulas.
For instance, the default table that I just created using the steps above was auto-named: Table_ExternalData_1
I can then select it using the normal method in formulas:
=Table_ExternalData_1[Column2]
etc...
Hope that helps.

Import additional excel data into Access Table

We need to upload a small amount of additional records to a table from an Excel sheet. Is there a way to use the Access Import function to add the additional data to the table (truncate it). The table was created by uploading the same Excel sheet. But now, when records are added, we need to add them to the table. The tables are linked to SQL but I do not want to use an SSIS because there are only a few records and there must be a way to use Access functions. Suggestions please.
It may be easiest to link the excel sheet and run an append query to add data from Excel to existing table. Once linked, this can be done in the query design window.
You did not specify versions of Excel or Access.
I did this with a test 2003 Excel sheet with cells containing 1000+ characters. An import in Access 2003 detects the data type as a memo field, which is correct, when there are that many characters, so it should work for you. It may be your Excel data has other ingredients causing an import issue. How is the excel data derived?
Have you tried importing to Access? It should work fine. If your ultimate target is another database why use Access as an intermediary?
I agree a linked table seems like a really simple method to update a table if you are using Access, but that is your choice.

Resources