简体   繁体   English

access vba中的SQL查询

[英]SQL query in access vba

I have problem with this UPDATE query , I have got message about syntax error query.我有这个 UPDATE 查询的问题,我收到了关于语法错误查询的消息。 I think that this query is correct and I can't find what is giving this error.我认为此查询是正确的,但我找不到导致此错误的原因。

Link to error错误链接

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

Your code should look like this instead:您的代码应如下所示:

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

Be aware that you need to replace any single quotes that might exist inside the values from the textboxes with two single quotes to prevent SQL errors.请注意,您需要用两个单引号替换文本框中值中可能存在的任何单引号,以防止出现 SQL 错误。 That's why I posted the PQ function.这就是我发布PQ功能的原因。

The UPDATE command syntax is as follows 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

Of course the above will now work since you have to adopt it for str variable当然,以上现在可以工作,因为您必须将它用于 str 变量

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

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