0

I've two queries I'm trying to create with QueryDef.

This insert query works perfectly and I can assign the parameters:

Dim qryAddPackage As String: qryAddPackage = "" & _
    "INSERT INTO `RMF_tblPackages` " & _
    "(`SystemID`, `DoD`, `DateCreated`, `LastModified`, `LastUpdatedBy`) " & _
    "VALUES ([SystemID], [DoD], NOW(), NOW(), [UID])"

With CurrentDb.CreateQueryDef(vbNullString, qryAddPackage)
    .Parameters!SystemID.Value = cbbSystems.Value
    .Parameters!DoD.Value = Me.txtDoD.Value
    .Parameters!UID.Value = strGetUserID()
    .Execute
End With

However, in this update query, I am only able to access the Parameter UID in the WHERE statement:

Dim qryUpdatePkg As String: qryUpdatePkg = "" & _
"UPDATE `RMF_tblPackages` " & _
"SET " & _
    "`SystemID` = [SystemID], `DoD` = [DoD], `LastModified` = NOW(), `LastUpdatedBy` = [UID] " & _
" WHERE `PackageID` = [PackageID]"

With CurrentDb.CreateQueryDef(vbNullString, qryUpdatePkg)
    .Parameters("SystemID").Value = cbbSystems.Value
    .Parameters!DoD.Value = Me.txtDoD.Value
    .Parameters!UID.Value = strGetUserID()
    .Parameters!PackageID.Value = Me.lbxPackages.Value
    .Execute dbSeeChanges
End With

In every other line trying to assign a parameter, it just says that the parameter's name is not in the collection.

I've tried assigning them like this:

.Parameters("PackageID").Value = Me.lbxPackages.Value

As well, but that doesn't work either.

I tried just running the update query with some data and it worked fine so the query works.

.Params.count returns 1 as you would expect given it's only recognizing one param.

Any other ideas as to why the parameters aren't being recognized by that update query?

1
  • 2
    This SQL looks like MySQL syntax, not Access SQL. Commented Jul 15, 2019 at 20:32

1 Answer 1

2

Use [ ] for quoting identifiers. In Access the backtick is not valid (unlike MySQL). Also column names and parameter names use the same quoting characters, so you must choose different names for the parameters. Quoting identifiers is only necessary if the names collide with a reserved word or function name (like Date, From, By, Order etc).

Dim qryUpdatePkg As String

qryUpdatePkg = "UPDATE RMF_tblPackages " & _
"SET " & _
  "SystemID = prmSystemID, DoD = prmDoD, LastModified = Now(), LastUpdatedBy = prmUID " & _
" WHERE PackageID = prmPackageID"

With CurrentDb.CreateQueryDef(vbNullString, qryUpdatePkg)
    .Parameters("prmSystemID").Value = cbbSystems.Value
    .Parameters!prmDoD.Value = Me.txtDoD.Value
    .Parameters!prmUID.Value = strGetUserID()
    .Parameters!prmPackageID.Value = Me.lbxPackages.Value
    .Execute dbSeeChanges
End With

Do the same changes in the first query.

Sign up to request clarification or add additional context in comments.

1 Comment

Hope you don't mind, I corrected "escapes" to "quoted identifiers" terminology; "escapes" has more to do with e.g. doubling single-quote characters inside a string literal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.