繁体   English   中英

access vba中的SQL查询

[英]SQL query in access vba

我有这个 UPDATE 查询的问题,我收到了关于语法错误查询的消息。 我认为此查询是正确的,但我找不到导致此错误的原因。

错误链接

Private Sub cmdModifyBook_Click() 'approves modifying books to the database

Dim str As String
Dim dbs As DAO.Database

Set dbs = CurrentDb()

'checks if the typed all the data of book
If (Me.txtModifyTitle.Value = "") Or (Me.txtModifyTitleWeb.Value = "") Or (Me.txtModifyVerkaufpreis.Value = "") _
   Or (Me.txtModifyThemengruppe.Value = "") Then
   MsgBox "Nicht alle von Ihnen eingegebenen Daten"
   Exit Sub
End If

str = " UPDATE Katalog " _
    & "(Bezeichnung, BezeichnungWeb, Verkaufspreis, Themengruppe) SET " _
    & "('" & Me.txtModifyTitle.Value & "', '" & Me.txtModifyTitleWeb.Value & "', '" & Me.txtModifyVerkaufpreis.Value & "', '" & Me.txtModifyThemengruppe.Value & "') WHERE ID_Buch =" & Me.lblModifyID.Caption & ";"

dbs.Execute str, dbFailOnError

MsgBox "Das Buch wurde in der Datenbank geändert", vbInformation

dbs.Close
Set dbs = Nothing

End Sub

您的代码应如下所示:

Private Sub cmdModifyBook_Click() 'approves modifying books to the database

    Dim str As String
    Dim dbs As DAO.Database

    Set dbs = CurrentDb()

    'checks if the typed all the data of book
    If (Me.txtModifyTitle.Value = "") Or (Me.txtModifyTitleWeb.Value = "") Or (Me.txtModifyVerkaufpreis.Value = "") _
       Or (Me.txtModifyThemengruppe.Value = "") Then
       MsgBox "Nicht alle von Ihnen eingegebenen Daten"
       Exit Sub
    End If

    str = "UPDATE Katalog " & _
    "SET Bezeichnung = '" & PQ(Me.txtModifyTitle.Value) & "', " & _
    "BezeichnungWeb = '" & PQ(Me.txtModifyTitleWeb.Value) & "', " & _
    "Verkaufspreis = '" & PQ(Me.txtModifyVerkaufpreis.Value) & "', " & _
    "Themengruppe = '" & PQ(Me.txtModifyThemengruppe.Value) & "' " & _
    "WHERE ID_Buch = " & Me.lblModifyID.Caption & ";"

    Debug.Print str
    MsgBox str

    dbs.Execute str, dbFailOnError

    MsgBox "Das Buch wurde in der Datenbank geändert", vbInformation

    dbs.Close
    Set dbs = Nothing

End Sub 

Private Function PQ(s as string) as String
    PQ = Replace(s, "'", "''")
End Function

请注意,您需要用两个单引号替换文本框中值中可能存在的任何单引号,以防止出现 SQL 错误。 这就是我发布PQ功能的原因。

UPDATE命令语法如下

UPDATE Katalog 
SET
Bezeichnung = Me.txtModifyTitle.Value ,
BezeichnungWeb = Me.txtModifyTitleWeb.Value ,
Verkaufspreis = Me.txtModifyVerkaufpreis.Value,
Themengruppe = Me.txtModifyThemengruppe.Value
WHERE ID_Buch = Me.lblModifyID.Caption

当然,以上现在可以工作,因为您必须将它用于 str 变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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