• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

Insert INTO data mismatch Access field

Hello,

I have a issue with  fields format with Microsoft Access. If I format the field to Currency or Number and use the "Insert Into" I get data type mismatch.
0
lincstech
Asked:
lincstech
2 Solutions
 
pcelbaCommented:
You should post the INSERT command.

This works for me:
INSERT INTO SomeTable (NumCol1, CurrCol2) VALUES (1, 2) ;
0
 
mbizupCommented:
If you are using Visual Basic (per the question's Topic Areas), your SQL statement might look something like this (note the embedded single quotes to delimit text around the values):


strSQL = "INSERT INTO Table1 (TextField1,TextField2) VALUES ('" & Somevalue1 & "','" & SomeValue2 & "')" 

Open in new window


Or this (which uses chr(34) -- quotes-- to delimit text):

strSQL = "INSERT INTO Table1 (TextField1,TextField2) VALUES (" & CHR(34) & Somevalue1 & CHR(34) & "," & CHR(34) & SomeValue2 & CHR(34) & ")" 

Open in new window


If you change the data type of text fields to numeric, you need to drop the quotes (which delimit text) to avoid Type Mismatch errors.    So this would be the correct syntax if your values are currency or other numeric format:

strSQL = "INSERT INTO Table1 (TextField1,TextField2) VALUES (" & Somevalue1 & "," & SomeValue2 & ")" 

Open in new window

0
 
jkaiosCommented:
If I format the field to Currency or Number...

You should leave the values "as is" and not formatting them when inserting into the table.

1234.56 will become $1,234.56 after being formatted to currency style.  The comma separator (and the dollar symbol) will cause your sql INSERT statement to fail.

This works:

INSERT INTO SomeTable (NumCol, CurrencyCol) VALUES (1234, 1234.56) ; 

Open in new window


This will fail:
INSERT INTO SomeTable (NumCol, CurrencyCol) VALUES (1,234, 1,234.56) ; 

Open in new window


This will also fail:
INSERT INTO SomeTable (NumCol, CurrencyCol) VALUES (1,234, $1,234.56) ; 

Open in new window

0
 
PatHartmanCommented:
Formatting is for humans and doesn't affect how a field is stored but formatting will turn a number into a text string and that is why you get type mismatch.  So, you would format for display but not for import (unless you actually want to change the data type).
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now