Solved

Insert INTO data mismatch Access field

Posted on 2014-02-18
4
442 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 34

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Creating and Managing Databases with phpMyAdmin in cPanel.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now