简体   繁体   中英

What is the correct syntax Syntax for WHERE keyword in VBA Code

I'm building a string that is going to be a sql execution in my access db. for proper source citing, I'm using a guide by Allen Brown Audit as a template to save changes to my main db.

here is the code that is currently giving me trouble:

' If this was not a new record, save the old values.
If Not bWasNewRecord Then
    sSQL = "INSERT INTO " & sAudTmpTable & " ( audType, audDate, audUser, ID, Campus, Building, Room, `Device Type`, Model, Domain, `Serial Tag`, Barcode, HostName, `Custodian Name`, Notes ) " & _
        "SELECT 'EditFrom' AS Expr1, Now() AS Expr2, NetworkUserName() AS Expr3, " & sTable & "ID, Campus, Building, Room, `Device Type`, Model, Domain, `Serial Tag`, Barcode, Hostname, `Custodian Name`, Notes " & _
        "FROM " & sTable & " WHERE " & sTable & "." & sKeyField & " = " & lngKeyValue & ";"
    db.Execute sSQL, dbFailOnError
End If

currently it's returning an error of "Too few parameters. Expected 1.", which makes me think it is the WHERE clause and the string concatenation after that's causing it. My main question is whether this problem is a syntax problem, where I just needed to escape or add a double-quote somewhere, or a logic problem with the statement, and what I'm trying to do: which is select a record from a criteria, where the ID of the changed record matches an ID in the DB? any notes or comments are welcome, and thank you for your time.

If you're using SQL Server, you'll want something like

SELECT Room
, [Device Type]
, Model
, Domain
, [Serial Tag]

For other RDBMSs (maybe for JetDB = Access), you'll want quotes instead of square brackets. (Quotes may actually work for SQL Server as well, I just usually use square brackets.)

SELECT Room
, "Device Type"
, Model
, Domain
, "Serial Tag"

...and for RDBMSs who are case sensitive (like Oracle), quote everything (which doesn't hurt anyway)...

SELECT "Room"
, "Device Type"
, "Model"
, "Domain"
, "Serial Tag"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM