Syntax error in where clause in Access - sql

The error I am getting is Syntax error in where clause in Access.
Here is the code:
SQL = "Select * FROM tblPermitAgencyInformation & WHERE [RecordID] = " & Me.AgencyInfoRecordID.Value
Set rs = db.OpenRecordset(SQL)
RecordID is an autonumber field and the AgencyInfoRecordID is an integer.

It looks like you misread the article you state. It appears to be attempting to format the adhoc query in the text variable.
Note: it says
strSQL = "SELECT wazzle FROM bamsploot" & vbCrLf & " WHERE plumsnooker = 0"
You need to make sure that you have the ampersands outside of the quotes, (they are used to append variables and strings together in this case)
Follow June7 advice and remove the ampersand there. It should help you get running.
Make your code this:
SQL = "Select * FROM tblPermitAgencyInformation WHERE [RecordID] = " & Me.AgencyInfoRecordID.Value
Hope that helps

Related

Runtime error '3075' in VBA

I know there are plenty of posts with the same title, but I couldn't find anything similar to my problem.
I'm trying to update a record with SQL in VBA. In particular, my code goes to an excel file, extracts a value, asks the user for the ID of the record that needs to be updated, and then should procced to do the update. The thing is i tried running the same code without the user input, and works just fine, so the problem must be with the interpretation of the input. I checked that everything is a string but i don't know how to solve the problem.
Here is the code I am using:
Dim query2 as string
Dim myID as string
myID = InputBox("Insert ID:")
query2 = "UPDATE [Info test] " & "SET RESULT = " & var & "WHERE ID =" & myID
DoCmd.RunSQL (query2)
In this case, var is a string, and is the value i fetch from the excel file.
From various tests, i think the problem is with the last & myID, like there is an apostrophe before the value stored by myID, but i don't know what to do about it, or if this is really the problem.
Thanks to everybody who can help
Insert Debug.Print query2 before DoCmd, run the code and check output in immediate window. This SQl should work in query builder. I believe you need to enclose var in single quotes and add a space before WHERE
query2 = "UPDATE [Info test] SET RESULT = '" & Replace(var, "'", "''") & "' WHERE ID =" & myID
I added Replace in order to avoid errors if var contains single quote.
If var is a string, then you need to use single quotes and add space before WHERE like shown below
query2 = "UPDATE [Info test] " & "SET RESULT = '" & var & "' WHERE ID =" & myID

Inserting Single/double type to DB using VBA in access

I need some help with an issue that is doing my head in.
I need to update a database in access and its been working fine operating with Long and Integers.
Look at this code.
sql = "UPDATE DBNAME SET Long_Field = " & Long_Variable & " WHERE ID = " & id
DoCmd.SetWarnings (False)
DoCmd.RunSQL sql
This code runs flawlessly, it takes a long variable and puts it into the correct field which is set as Long.
However, if I want to populate a single/double field (ive tried both)
sql = "UPDATE DBNAME SET Double_Field = " & double_Variable & " WHERE ID= " & id
DoCmd.SetWarnings (False)
DoCmd.RunSQL sql
I keep getting Run-Time error 3144: Syntax error in update statement.
I can literally just switch out the field name and the variable name and the code runs flawlessly, but as soon as i try to send a double value, for example (5,8), to a field in the table that is set to double, it gives me this error.
Anyone?
I assume that you want a dot as decimal separator in your string.
The conversion from double to string is done using the separator from the system locale settings so in your case a comma.
This means that
double_variable = 5.8
sql = "... " & double_variable & " ..."
will produce ... 5,8 ... in the sql variable.
The easiest way to fix that is to use
"..." & Replace(CStr(double_variable), ",", ".") & "..."
This will replace all , with .. I put the CStr there to make sure it gets converted to a string first. It will also work if the system locale changes since nothing will happen if there is no ,. The only caveat is that if for some reason the conversion inserts 1000s separators it will fail but that would only be relevant in other circumstances as I don't think CStr will ever do that.
The current answer is not the easiest, neither the simplest.
The universal method is to use Str as it always returns a dot as the decimal separator:
sql = "UPDATE DBNAME SET Double_Field = " & Str(double_Variable) & " WHERE ID = " & id & ""

Syntax error in query expression with wildcard

I am getting a syntax error 3075 in the following code. I am trying to open a recordset where the field [subject] contains the [ID] from an open form (Edit_Shipment_frm!Text105). The subject field will be laid out like: Information Request - 1715, where 1715 is the [ID]. I know I need to use a wildcard before [ID] in the criteria. I am struggling to find a way to do this that access will accept. Does anyone have any ideas? I don't see any missing operators below so it must just not like my syntax. Thank you in advance!
Set rst1 = db.OpenRecordset("SELECT Subject, Contents FROM LinkedTable WHERE Subject = *" & [Forms]![Edit_Shipment_frm]![Text105])
You can't use the asterisk like this. Correct SQL would be
... WHERE Textfield LIKE '*" & [string that contains a part of Textfield] & "*'"
but you have it the other way around (your ID is part of the search string).
You need to extract the ID from the form field. If [Text105] (please give this field a meaningful name!) always looks like "some string - ID", you can use the Split() function:
S = "SELECT Subject, Contents FROM LinkedTable WHERE Subject = " & _
Split([Forms]![Edit_Shipment_frm]![Text105], " - ")(1)
Set rst1 = db.OpenRecordset(S)
Split() returns a 0-based array, so Split(..., " - ")(1) gives the part after " - ".
But to get some error checking (e.g. if " - " can be part of the string before the ID), you should put the extracting into a separate function, where you check for NULLs, and use UBound() of the array.

VBScript not executing sql statment properly

I write you this time because a VBScript that one of the application my company uses to retrieve information from an Oracle database does not seem to be working properly. Here are the facts:
There's part of the code that does the following:
sSql = "SELECT REQ_PAYMODE" & _
" FROM SYSADM.GBPRESTATIEGROEP" & _
" WHERE 1=1" & _
" AND SLEUTEL = " & sKeyPrestatiegroep
Set oRSGBPrest = connADO.execute(sSql)
If Not oRSGBPrest.EOF Then
sRequestPaymodeKey = oRSGBPrest("REQ_PAYMODE")
Else
//error handling
End If
Using a Statement Tracer for Oracle (www.aboves.com) I can capture that same statement with its corresponding value:
SELECT REQ_PAYMODE FROM
SYSADM.GBPRESTATIEGROEP WHERE 1=1 AND
SLEUTEL = 1572499
Now, the VBScript is supposed to take that value and execute another query:
sSql = "SELECT PAM_CODE" & _
" FROM SYSADM.PAYMODES" & _
" WHERE 1=1" & _
" AND PAM_KEY = " & sRequestPaymodeKey
Set oRSPaymodes = connADO.execute(sSql)
Right in this last line of code, the script throws an error that says:
ORA-00936: missing expression at line XXX --> Set oRSPaymodes = connADO.execute(sSql) <--
Which basically means that the query in (3) is not correct, which also means that for some reason sRequestPaymodeKey is empty. I cannot tell this for sure because this failing sql statement does not appear in the statement tracer, but that's the only explanation I could find. However, the worst part is that when running the query (2) on SQLDeveloper (that's where value sRequestPaymodeKey comes from) it shows a row with a value other than null or zero.
I can't think of anything else that might be happening here, maybe it's just a server thing... no idea.
Any suggestions from you guys? Any way I can actually debug a VBE file?
Your help is much appreciated!
You need to cast sRequestPaymodeKey as a vbLong which corresponds to sql's INT. I'm assuming PAM_KEY is an INT. A recordset will return a string value. So, your code would look like this:
If IsNumeric(sRequestPaymodeKey) Then
sSql = "SELECT PAM_CODE" & _
" FROM SYSADM.PAYMODES" & _
" WHERE 1=1" & _
" AND PAM_KEY = " & CLng(sRequestPaymodeKey)
Set oRSPaymodes = connADO.execute(sSql)
Else
'do error handling due to bad returned data(empty string?)
End If
Also, consider parameterizing your queries to prevent sql injection.
A few ideas to try:
Before Set oRSPaymodes = connADO.execute(sSql), put in a MsbBox and see what SQL is being executed. Is it valid? Will it run in a Oracle query analyzer(if there is one)?
Hard code a valid value in place of sRequestPaymodeKey. Does it work then?

Passing variables to stored procedure with Classic ASP

Ok so I am trying to pass some string variables from a classic ASP page to an MSSQL2000 db thusly:
strSQL = "exec UpdateEvent " & xID & ",'" & xEventID & "'," & xEventDisplayName & "," & xEventType & "," & xEventStatus & "," & xStartDate & "," & xEndDate & "," & xSurveyTemplateID & ""
Yet I end up with the error (including writing out the strSQL contents):
exec UpdateEvent 1,'1-44KTDL',,,,,,
Microsoft OLE DB Provider for SQL
Server error '80040e14'
Line 1: Incorrect syntax near ','.
/eventedit.asp, line 225
Now I am not sure if it is the dash in the EventID variable that is causing my problems (or why all the other variables are coming up with null values when there is data there...) . I have tried many many combinations of quotes and tics to appease the syntax interpreter but to no avail. What am I doing wrong? Is there a better way to do this simple stored procedure call?
That's very VERY bad; your code is subject to SQL injection attacks and needs to be fixed as soon as possible.
<!--#include virtual="/ASPSAMP/SAMPLES/ADOVBS.INC"-->
<%
Set cmd = Server.CreateObject("ADODB.Command")
' ... open connection and stuff ... '
cmd.CommandText = "UpdateEvent"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters(1) = xID
cmd.Parameters(2) = xEventID
cmd.Parameters(3) = xEventDisplayName
cmd.Parameters(4) = xEventType
cmd.Parameters(5) = xEventStatus
cmd.Parameters(6) = xStartDate
cmd.Parameters(7) = xEndDate
cmd.Parameters(8) = xSurveyTemplateID
cmd.Execute
%>
If no data exists for those variables you need to at least put NULLs in there otherwise the SQL will fail.
For example if you paste
exec UpdateEvent 1,'1-44KTDL',,,,,,
directly into Query Analyzer, you will get the same error.
Ideally you will need to load them into a parameter array, and create defaults for each parameter (e.g. NULL) in case no data is present for it.
This will ensure your Stored Procedure is built correcly.
e.g.
exec UpdateEvent 1,'1-44KTDL', NULL, NULL, NULL, NULL, NULL, NULL
How about this:
http://support.microsoft.com/kb/164485
I would suggest that you capture the SQL being generated in your code > run that SQL manually in the database > see if you can spot the problem.
Set a breakpoint in your code where strSQL is populated. Step over that line. Get the value of strSQL at that point.
This may help you to identify the problem, which may be in your syntax or may arise from unexpected values in the parameter variables.