Avatar of mak345
mak345
Flag for United States of America asked on

Import rounded numbers into Access from Excel

I have a file in Excel 2010 that has fields with numbers rounded to the nearest dollar.  The value has cents in it, but it is just formatted to only show whole dollars.  However, I wish to import the cents into an Access 2010 table as well.  I currently use the following command to import the worksheet:

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
         strTable, strFile, True, wks.Name & "$"

For the most part, all of the values are being imported correctly.  However, when there is a column where the top several rows do not have any cents, it seems that when it will not bring in the cents in the subsequent records.  It will bring in the rounded number instead.  I am not 100% sure, but I think there is something in Access where it looks at the top rows to determine the datatype it is importing.  In this case, it seems that it thinks everything will be whole dollars and it ignores the cents.

I know that if someone manually formats the sheet to show 2 decimal places prior to importing, it will work fine.  But, I am trying to eliminate the need for any modification to the original dataset.  What is the best approach at doing this?

Thanks
Microsoft AccessMicrosoft ExcelVBA

Avatar of undefined
Last Comment
PatHartman

8/22/2022 - Mon
Jim Dettman (EE MVE)

With the built-in Transferspreadsheet command, there are not many options.  There are a few approaches:

1. Link to the spreadsheet as a table.   You can then work with it that way.

2. Have the user save the spreadsheet in a CSV format (or other text format).  Then you can use TransferText and specify a import specification that you've written for the import.

3. Same as #2, but link to it as a table and use a schema.ini file to describe the fields.

4. Open Excel as an automation option (Excel needs to be installed where your doing the import), and then loop through the cells and pull the data.  This gives you total control.

Let me know if you need anything more...


Jim.
Kanti Prasad

hi

Is your field in access defined as currency if not try it as currency.
mak345

ASKER
Unfortunately, I can't change to csv, because the spreadsheet is sent over with multiple tabs.  #4 seems to be the best option for me unless I can find somehtng else.  I'd rather not write code to loop through each cell, since there are several thousand rows with 30+ fields.

The field format is standard.  I tried to change it to currency, but that did not work.  It still imported the rounded number.

Thanks
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Jim Dettman (EE MVE)

Your best bet is #1 then.  You'll need to create a link for each sheet in the workbook.  That can be done through the UI, or via code.

Try it manually first and see if you get the results you want.

Jim.
Kanti Prasad

Hi

Will it be possible to ask the source team who gives you the files\outputs to set those columns as 2 decimals?
mak345

ASKER
Creating links is also tough, because the number of links will grow indefinitely, since we are getting multiple files monthly and all have to go into 1 table, which will get messy.

I'm being told the source file will not change.....
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jim Dettman (EE MVE)

<<Creating links is also tough, because the number of links will grow indefinitely, since we are getting multiple files monthly and all have to go into 1 table, which will get messy.>>

 What you do is create a link on the fly.  Once your done with it, delete it.  

or

 create links that point to a "known" dummy spreadsheet.  As you work with each file, you will delete the existing dummy spreadsheet and copy the one you want to work with to that name.

  All you will need then is one link per possible sheet in a workbook no matter how many files you have.

 As long as you don't try and use the links while the file is not there, you'll get no errors.   I've used this technique a number of times.

Jim.
ASKER CERTIFIED SOLUTION
PatHartman

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.