Solved

Insert INTO data mismatch Access field

Posted on 2014-02-18
4
447 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 41

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

776 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