简体   繁体   中英

How to change ACE TypeGuessRows Registry Value for Access 2007 to Determine Field Data Type Properly When Importing From Excel File Using VBA

I have an Access database that imports data from an external Excel spreadsheet that contains a column of data called Date Delivered . While most data in this column is actually a date type, sometimes entries like 4/5/15,5/1/15 will be made to indicate two delivery dates.

This trips up Access when importing the data using:

DoCmd.TransferSpreadsheet acImport, 5, "Delivery Data", "\\File\Path.xlsx", True 

This causes it to make the column the date type rather than a string which drops any value from those fields which is problematic. I found what appeared to be a helpful solution here that suggested changing the TypeGuessRows registry value to 0 to make Jet look at all rows when determining the field data type.

I created and changed the registry value using the following code:

Dim RegLoc As String
Dim myWS As Object

'Writes registry value to make Jet check all rows (0) not just first 25 (Default value = 8) to determine type
RegLoc = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows"
Set myWS = CreateObject("WScript.Shell")
myWS.RegWrite RegLoc, 0, "REG_DWORD"

Unfortunately this did not seem to work as the problem persisted after changing the value and restarting Access and Excel.

Is there a separate registry value for Access vs Excel Jet engine or does this simply not do what I think it should?

Edit:

Looks like for Excel 2007 and newer (I am using 2007), the registry value should be:

HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows

However it still isn't working.

Edit 2:

Here is the code to do what @PractLogical suggested:

SQL = "CREATE TABLE [Delivery Data Import] ([F1] TEXT, [F2] TEXT,[F3] TEXT, ...)"
CurrentDb.Execute SQL

DoCmd.TransferSpreadsheet acImport, 5, "Delivery Data Import", "\\File\Path.xlsx", False

SQL = "INSERT INTO [Delivery Data] ( Buyer, [PO #], [Receipt Status],...) SELECT [Delivery Data Import].F1, [Delivery Data Import].F2, [Delivery Data Import].F3,...] WHERE (Get rid of headers);"
CurrentDb.Execute SQL

I am still looking for a one and done solution for all imports via registry key if it exists (knowing it doesn't exist would also be useful).

I'm unable to re-create the issue, but have run into this in the past. No clue why they dropped import specifications which were perfect for such things, but I've had luck doing the following (not TypeGuessRows related).

Make a copy of your [Delivery Data] table and name it something like [Import_DeliveryData]. Delete any data in the copy, go into design view and change all cols to short text data type. A bit of a hassle, but you'll need to rename all cols to F1, F2, F3, etc starting with the top col as F1. *You'll see why in a sec.

Now, transfer your sheet into the Import table (deleting any existing rows first) and be sure *HasFieldNames is false. Everything should come in fine as text. You'll need to do some cleanup on the import table's data before appending to the 'real' [Delivery Data] table, but at least you have control over what's what.

From my experience, Access checks only the first row of data to determine what data type the column is; the second row if HasFieldNames is true. Here we are telling it to check the first row which should be a text because it's field names. Not only that, but we're telling it to append to an existing table of all text columns instead of it (re)creating a table. This helps when Access thinks a column is a Date, creates a date column for it, and then tries to append non-dates...which is most likely the cause of your issue.

I always use this type of import proxy table when importing from Excel since the data can be so unpredictable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM