Question

I am trying to import data from MS SQL Server DB into MS Access using ODBC. For mostly all datatypes the import is mapping types correctly, but when the SQL Server column datatype is Decimal Number, MS Access converts it to text datatype. So I am trying to find a proper workaround that automatically converts this Text into Decimal Number.

E.g : one price is coming in as '3978.76821' and datatype is set to text so I can't run simple calculations on the field.

Screenshot showing data in both SQL server and MS Access

Was it helpful?

Solution

Access is converting the decimal(31,7) column to Text(31) because the maximum precision for an Access Decimal column is 28. I confirmed this by importing the following SQL Server table:

CREATE TABLE [dbo].[importTest](
    [seq] [int] NULL,
    [textCol] [nvarchar](50) NULL,
    [decimalCol] [decimal](31, 7) NULL
) ON [PRIMARY]

Workaround 1:

Create a View on the SQL Server to "downsize" the offending columns...

CREATE VIEW dbo.importTestForAccess AS
SELECT seq, textCol, 
    CONVERT(decimal(28,7), decimalCol) AS decimalCol
FROM dbo.importTest

...and then import the View instead of the Table.

Workaround 2:

Import the table as before, and then convert the column from Text to Decimal in Access via VBA

CurrentProject.Connection.Execute _
        "ALTER TABLE dbo_importTest " & _
        "ALTER COLUMN decimalCol DECIMAL(28,7)"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top