Data type mismatch Acess 2010 VBA - vba

I get a Data type mismatch error when I try to run the following code:
Dim myString As String
Dim rs As Recordset
sSQL = "SELECT * FROM KLIST WHERE KLIST = " & Me.bleh
Set rs = CurrentDb.OpenRecordset(sSQL)
myString = rs.Fields("DATEK")
The "bleh" field is a normal text field in a form. The field "KLIST" in the "KLIST" table is also a text field, containing numbers.
Does the fact that i enter numbers in the text field in the form change the format of the field? And if it does, how can I compare them in the WHERE statement.
Thanks.

As follow up from comments, this one works
sSQL = "SELECT * FROM KLIST WHERE TRIM(KLIST) = '" & TRIM(Me.bleh) & "'"
also you may want to see this on how to use VBA variables in SQL statements for different data types: Global Variables in SQL statement

If KLIST is a text column, you must add string delimiters (single quotes in SQL):
SELECT * FROM KLIST WHERE KLIST = '123'
In VBA
sSQL = "SELECT * FROM KLIST WHERE KLIST = '" & Me.bleh & "'"
A single quote within a string in SQL can be escaped by doubling the quote:
SELECT * FROM KLIST WHERE KLIST = 'that''s it'
Use the Replace function in VBA to achieve this:
sSQL = "SELECT * FROM KLIST WHERE KLIST = '" & Replace(Me.bleh, "'", "''") & "'"

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

LIKE query to find column values that are substrings of a longer string

I want to use this code to find a value in field PID where it may exist anywhere in field MP_PID but my SQL is not catching them. No errors are thrown. What am I doing wrong?
It is comparing values from text fields in both recordsets.
Thanks
TMA
strPID = "DELETE * FROM tblPID WHERE PID Like '*" & !MP_PID & "*';"
If I use:
strPID = "DELETE * FROM tblPID WHERE PID = '" & !MP_PID & "';"
It works fine for exact matches.
Access 2007 DAO
What is wrong with my like statement? Thanks!
LIKE expressions are used to identify records where the value on the left side of the LIKE operator contains a string (or pattern of characters) represented by the wildcard string on the right side of the LIKE operator.
In your case
... '0123456A' LIKE '*0000623E 0123456A*'
will return False because the left side does not contain the entire string on the right side.
What you really want is
... '0000623E 0123456A' LIKE '*0123456A*'
which means that you need the MP_PID value on the left side of the LIKE operator.
To do that correctly (i.e., with a parameterized query) would look something like this:
Option Compare Database
Option Explicit
Sub LikeTest()
' test data
Dim MP_PID As String
MP_PID = "0000623E 0123456A"
Dim cdb As DAO.Database
Set cdb = CurrentDb
Dim qdf As DAO.QueryDef
Set qdf = cdb.CreateQueryDef("", _
"PARAMETERS [LongerText] TEXT(255);" & _
"DELETE * FROM tblPID WHERE [LongerText] LIKE '*' & PID & '*'")
qdf!LongerText = MP_PID
qdf.Execute dbFailOnError
Debug.Print qdf.RecordsAffected & " record(s) deleted."
Set qdf = Nothing
Set cdb = Nothing
End Sub
PID is most likely a numeric field and you use the LIKE operator which traverses string values. Try converting numeric value to string:
strPID = "DELETE * FROM tblPID WHERE CSTR(PID) Like '*" & !MP_PID & "*';"
Alternatively, concatenate an empty length string value to numeric value (similar to converting textbox numeric value to string by concatenating asterisks and quotes):
strPID = "DELETE * FROM tblPID WHERE PID & '' Like '*" & !MP_PID & "*';"
Ok Gord and Parfait. You both got me on the correct path and yes I did have it back asswards. Here is what I used to create a delete query:
strDelMP = "DELETE tblPID.* FROM tblPID INNER JOIN tblMgtPlan ON tblMgtPlan.MP_PID Like '*' + tblPID.PID + '*';"
db.Execute (strDelMP)
Thanks!

Store a sql select statement, ran from VBA, in a numeric variable

I'm working on creating a dynamic pass-through query and in order to do so I first need to query my local db and get an ID.
This ID is what I will put into my pass-through query for my WHERE clause of the query.
My string:
getCorpID = "SELECT corpID " & _
"FROM dbo_corp " & _
"WHERE name = " & Forms.frmMain.Combo4.Value
I'm then trying to do something akin to:
CorpID (integer) = docmd.runsql (getCorpID)
I realize, however that docmd runsql doesn't work with select statements, or return a value even. What can I use to run my string
getCorpId
as sql and store the result (It will only be one result, every time.. one number) in my variable CorpID
Thank you.
Consider about using Recordset :
Dim dbs As DAO.Database
Dim rsSQL As DAO.Recordset
Dim getCorpID As String
Dim CorpID
Set dbs = CurrentDb
getCorpID = "SELECT corpID " & _
"FROM dbo_corp " & _
"WHERE name = " & Forms.frmMain.Combo4.Value
Set rsSQL = dbs.OpenRecordset(getCorpID , dbOpenSnapshot)
rsSQL.MoveFirst
CorpID = rsSQL.Fields("corpID")

Microsoft Access can't find the field '|1'

I keep getting a run time error '2465' when running a query via VBA in Access.
Error: Microsoft Access can't find the field '|1' referred to in your expression
I can't seem to find where this issue is occuring. Below is the VBA code that I'm currently using to requery a form.
Dim Test As String
Test = "*" & Combo161.Value
Dim strSQL As String
Dim strWhere As String
strWhere = (Chr(34) + Test + (Chr(34)))
'MsgBox (strWhere)
strSQL = "SELECT * FROM Test_Query WHERE TestID " & strWhere
'MsgBox (strSQL)
[Form_Test (subform)].RecordSource = strSQL
[Form_Test (subform)].Requery
The TestID had a field formatting of text, rather than a number. Does this matter at all?
I have just fixed this error. I was referencing the subform's source object, rather than its name given in the form properties.
I had the same error. What I missing was the double quotes around a string. This error is a bit misleading. Check the syntax etc and you will find the issue was related to comma or double quotes etc.
Try:
Dim Test As String
Test = "*" & Combo161.Value
Dim strSQL As String
Dim strWhere As String
strWhere = (Chr(34) & Test & (Chr(34)))
'MsgBox (strWhere)
strSQL = "SELECT * FROM Test_Query WHERE TestID Like " & strWhere
'To test
'Debug.print strSQL
If this is a subform, then:
Me.[Form_Test (subform)].Form.RecordSource = strSQL
''Not needed when changing record source
''Me.[Form_Test (subform)].Form.Requery
You did not have an equals sign / Like and the concatenator in VBA is &, not +, using + can lead to problems with nulls, but in this case, I reckon the problen is the missing Like, that is
TestID Like "*something"
You can control the contents of a subform with a combo and a link field:

VB.NET: convert text with single quote to upload to Oralce DB

I have a function in VB.NET that runs a query from an MS SQL DB, puts the results into temporary variables, then updates an Oracle DB. My question is, if the string in the MS SQL contains a single quote ( ' ), how do I update the Oracle DB for something that has that single quote?
For example: Jim's request
Will produce the following error: ORA-01756: quoted string not properly terminated
The ueio_tmpALM_Comments (coming from MS SQL) is the culprit that may or may not contain the single quote.
update_oracle =
"update Schema.Table set ISSUE_ADDED_TO_ALM = '1'," & _
"ISSUE_COMMENTS = '" & ueio_tmpALM_Comments & "'," & _
"where ISSUE_SUMMARY = '" & ueio_tmpALM_Summary & "' "
Dim or_cmd_2 = New NetOracle.OracleCommand(update_oracle, OracleConn)
or_cmd_2.ExecuteNonQuery()
From your question it's clear that you are building the update query using string concatenation.
Something like this
Dim myStringVar as string = "Jim's request"
Dim sqlText as String = "UPDATE MYTABLE SET MYNAME = '" + myStringVar + "' WHERE ID = 1"
this is a cardinal sin in the SQL world. Your code will fail for the single quote problem, but the most important thing is that this code is subject to Sql Injection Attacks.
You should change to something like this
Dim cmd As OraclCommand = new OracleCommand()
cmd.Connection = GetConnection()
cmd.CommandText = "UPDATE MYTABLE SET MYNAME = :myName WHERE ID = 1"
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue(":myName", myStringVar)
cmd.ExecuteNonQuery()