0

SQLステートメントのエラーを理解するのに助けが必要です。私はいくつかのことを試みてきましたが、何もうまくいかないようですか?これは私が受け取るエラーメッセージです

Run-time error '3075':

Syntax error (missing operator) in query expression '([description] = Manufacturing and Delivery Schedule AND [pr_num] = 83)'.

これは私のコードです:

Private Sub Command6_Click()
' ===================================================
' Receives the selected item in the combo box
' ===================================================

' Open the Database connection
Dim data_base As Database
Set data_base = CurrentDb

' Grab description and pr number from the form
Dim desc As string
dim pr_number as long
desc = Combo4.Value
pr_number = Text8.Value

' Build the query
Dim query As String
query = "UPDATE VDR_Table " & _
    "SET [received] = [p1] " & _
    "WHERE ([description] = " & desc & _
    " AND [pr_num] = " & pr_number & ");"

Dim rec_set As DAO.Recordset
Set rec_set = data_base.OpenRecordset(query)

' Build the QueryDef
Set qd = data_base.CreateQueryDef("")
qd.SQL = query

' Execute query
qd.Parameters("p1").Value = true
qd.Execute

' Close nad null record set
rec_set.close
set rec_set = nothing

' Close the connection to the database
data_base.Close

' Prompt the user success
MsgBox "Item has been received"
End Sub

助けてくれてありがとう!

4

1 に答える 1

1

設定する説明フィールドの値は文字列フィールドであるため、引用符で囲む必要があります。次のようになります。

' Build the query
Dim query As String
query = "UPDATE VDR_Table " & _
    "SET [received] = [p1] " & _
    "WHERE ([description] = '" & desc & _
    "' AND [pr_num] = " & pr_number & ");"

この場合は問題ではないため、以下のリンクを削除しました。

また、SQLインジェクションを回避するために、文字列連結の代わりにパラメーターを使用することをお勧めします。これはVBAでパラメーターを使用する例です-http ://support.microsoft.com/kb/181734-そしてここにパラメーター化されたSQLを使用する理由のいくつかの理由があります-http ://www.codinghorror.com/blog/2005/04 /give-me-parameterized-sql-or-give-me-death.html

于 2012-08-27T15:59:11.847 に答える