Mismatch datatype criteria expression, string field - sql

I have a ms access database and when I try to update a column in a module I get the error "Mismatch datatype criteria expression", when I create the column the datatype was TEXT(25) and the value that I'm trying to assign is a String:
Dim str As String
str = "test"
sql = "UPDATE Table "
sql = sql & "SET Table.[column] ='" & str & "' "
sql = sql & "WHERE Table.[id] = 1;"
MsgBox(sql)
Application.CurrentDb.Execute(sql)
In the MsgBox the string of query appears to be right: "UPDATE Table SET Table.[Column]='test' WHERE Table.[id]=1;
Which is the error?, how to fix this?

If Table.id is a numeric column your where clause is okay, but if it is a text column it should read:
WHERE Table.id = '1'

Related

Data type mismatch in criteria expression SQL Select In Access

Dim db As Database
Dim rrs As Recordset
Dim strselect As String
Set db = CurrentDb
strselect = "SELECT [Stok Pembelian] FROM t_databarang WHERE [ID Barang]= '" & Me.Text7 & "'"
Set rrs = db.OpenRecordset(strselect)
Me.Label1.Caption = rrs![Stok Pembelian]
I have a project from my school. I'm beginers and i'm so confused about my Access. I think that code it's correct, but there's an error "Data type mismatch in criteria expression." I already search much articles about how tow to fix it but not found. Please, help me.
Your id is most likely a number, thus no quotes:
strselect = "SELECT [Stok Pembelian] FROM t_databarang WHERE [ID Barang]= " & Me!Text7.Value & ""
Try to convert the textbox value into a double or int...
Dim Text7a as Double
Text7a = CLng(Me.Text7) 'Beware of rounding with Long Integers
Now use the alias Text7a for:
strselect = "SELECT [Stok Pembelian] FROM t_databarang WHERE [ID Barang]= '" & Me.Text7a & "'"
Check data types!
Like "Long Text do not match Number data type." Do it both them same data type on your database design

Not match the datatypes of the criteria expression ms access

I have a update like this in a module of ms access:
sql = "ALTER TABLE Carrera "
sql = sql & "ADD COLUMN carrera_nombre TEXT(25);"
Application.CurrentDb.Execute (sql)
sql = "UPDATE Carrera "
sql = sql & "SET Carrera.[carrera_nombre] = '" & strFunction(Carrera.[NombreCarrera]) & "' "
sql = sql & "WHERE Carrera.[CarreraId] > 1000;"
Application.CurrentDb.Execute (sql)
In database the column NombreCarrera exist, and I declared the function:
Function strFunction(str) As String
strFunction = str & "test"
End Function
But I get the error 3464 Not match the datatypes of the criteria expression, and I don't understand why, the strFunction return a String and the carrera_nombre and CarreraNombre columns type is TEXT.
EDIT: If I check string sql through MsgBox(sql) y get the right query: UPDATE Carrera SET Carrera.[carrera_nombre] = 'mathtest' WHERE Carrera.[CarreraId] > 1000;
I believe since you declare the sql command as a string, you will have to get it first.
so one of your code
sql = sql & "SET Carrera.[carrera_nombre] = strFunction(Carrera.[NombreCarrera])"
Should become
sql = sql & "SET Carrera.[carrera_nombre] = " & strFunction(Carrera.[NombreCarrera]) & """

Placing a Query Result in A String Variable

Was wondering how can we put an SQL statement in a String Variable?
Example:
Dim sqlQuery as String, qHolder as String
sqlQuery = "SELECT ID.table from table WHERE ID ='" userInputTextBox "'"
Whenever i do the above statement using either OpernRecordset() or RecordSource it gives me a bug. Can you give an example on how we do this, so we can use the value of sqlQuery for comparison for example
if(sqlQuery = userInputTextBox)then
MSgBox(" Match Found " )
Else
MsgBox("No Match Found")
Big Thanks in Advance.
Use the string concatenation operator (&).
sqlQuery = "SELECT ID.table from table WHERE ID ='" & userInputTextBox & "'"
Otherwise, it's a syntax error.
UPDATE
To get the value of ID.table into a string, use the Fields property of the Recordset:
Dim s As String
' by index
s = MyRec.Fields(0)
' or by name
s = MyRec.Fields("ID.table")

select [combobox value] & "text"

I am trying to build SELECT statement in Access VBA, based on value chosen by user in ComboBox.
Example :
"SELECT [8_SV_RT] FROM DATA WHERE condition = value
I need 8 to be dependent on ComboBox value from form. (8,10,12 ....)
The name of ComboBox is DN, and I created string COL = "_SV_RT"
So far I have :
"SELECT [DN] & '" & COL & "' FROM DATA WHERE condition = value
It returns value 8_SV_RT to ComboBox, which I want to use after SELECT statement, but not as result. I must be missing some syntax or something? Can anyone please advise?
If I understand your question right:
Dim SQL As String
SQL = "SELECT [" & Me.MyComboBox & "_SV_RT] FROM DATA WHERE condition = value"
EDIT:
To use the exact object names from the question (before Siddharth edited it, I over-read that the combobox is named DN) and to take Siddharth's (now deleted) comment into consideration:
Dim SQL As String
Dim COL As String
COL = "_SV_RT"
SQL = "SELECT [" & DN & COL & "] FROM DATA WHERE condition = value"

Return Empty String as NULL

I have a listbox select and I want when the user selects null for the empty string it produces to pull the nulls from the SQL table.
Here's what I have now. Blank strings return nothing because there are no empty fields in the table.
SELECT * FROM dbo.Table WHERE ID = " & TextBox2.Text & " and And Field1 IN (" & Msg1 & ")
How do I code that?
Use an If statement. When your textbox is empty, have the SQL string contain "ID is null" instead of appending the textbox's value.
If (TextBox1.Text = "") Then
' use Is Null in your sql statement
Else
' use the textbox text value in your sql statement
End If
(Assuming you're talking about the textbox and not whatever Msg1 is.)
dim sql
If (TextBox2.Text = null) Then
sql = "SELECT * FROM dbo.Table WHERE ID is null and And Field1 IN (" & Msg1 & ")"
Else
sql = "SELECT * FROM dbo.Table WHERE ID = " & TextBox2.Text & " and And Field1 IN (" & Msg1 & ")"
End If
See #John Saunders comment, you are risking sql injections. When passing paremeter to an sql query, be sure to use parameters, and not concatenating strings.
SELECT * FROM dbo.Table WHERE ID = " & TextBox2.Text &
" And Field1 " & IIF(Trim(Msg1) = "", "IS NULL", "IN (" & Msg1 & ")")
Yes, it is crude.
One should not write query in this style.
EDIT: Corrected. Please check.