Solved

Insert INTO data mismatch Access field

Posted on 2014-02-18
4
448 Views
Last Modified: 2014-02-21
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
Comment
Question by:lincstech
4 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 39866813
You should post the INSERT command.

This works for me:
INSERT INTO SomeTable (NumCol1, CurrCol2) VALUES (1, 2) ;
0
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 39866989
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
 
LVL 12

Expert Comment

by:jkaios
ID: 39867016
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
 
LVL 35

Assisted Solution

by:PatHartman
PatHartman earned 250 total points
ID: 39867281
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about achieving the basic levels of HRIS security in the workplace.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question