Errore in UPDATE statement

di Anonimizzato16186 il
2 risposte
Ciao a tutti.

Sto cercando di eseguire una query di aggiornamento da un modulo VBA per aggiornare una tabella usando gli inserimenti dell'utente in due form differenti con DoCmd.RunSQL. il codice genera un messaggio di errore (run time error 3144, syntax error in UPDATE statement) che non riesco a risolvere. il codice che sto usando è il seguente:

Sub EditRecord()

Dim SQL As String
Dim Supp As Integer
Dim Fname As String
Dim Lname As String
Dim Tel As String
Dim Mob As String
Dim Mai As String
Dim Note As String
Dim IDCon As Integer

Supp = Forms!Add_Contact.Cbo_ID_SUPPLIER
Fname = Forms!Add_Contact.First_Name
Lname = Forms!Add_Contact.Last_Name
Tel = Forms!Add_Contact.Telephone
Mob = Forms!Add_Contact.Mobile
Mai = Forms!Add_Contact.Mail
Note = Forms!Add_Contact.Notes
IDCon = Forms!Contact.ID_CONTACT

SQL = "UPDATE Contacts" & _
"SET Contacts.ID_SUPPLIER = " & Supp & ", Contacts.First_Name = '" & Fname & "', Contacts.Last_Name = '" & Lname & "', " & _
"Contacts.Telephone = '" & Tel & "', Contacts.Mobile = '" & Mob & "', Contacts.Mail = '" & Mai & "', Contacts.Notes = '" & Note & "' " & _
"WHERE Contacts.ID_CONTACT = " & IDCon & "; "

DoCmd.RunSQL SQL

End Sub

Vi sarei grato se mi poteste aiutare nell'individuare l'errore, o consigliare una procedura diversa.
Pietro

2 Risposte

  • Pietro Diterlizzi ha scritto:


    ...
    SQL = "UPDATE Contacts" & _
             "SET Contacts.ID_SUPPLIER = " & Supp & ", Contacts.First_Name = '" & Fname & "', Contacts.Last_Name = '" & Lname & "', " & _
          "Contacts.Telephone = '" & Tel & "', Contacts.Mobile = '" & Mob & "', Contacts.Mail = '" & Mai & "', Contacts.Notes = '" & Note & "' " & _
          "WHERE Contacts.ID_CONTACT = " & IDCon & "; "
    Vi sarei grato se mi poteste aiutare nell'individuare l'errore, o consigliare una procedura diversa.
    Pietro
    In debug fai
     ? SQL
    e vedi come risulta la stringa che dovrebbe costituire la query di aggiornamento.
    Se non ci sono errodi di copia incolla, manca uno spazio dopo "UPDATE Contacts". La stringa infatti parte subito dopo con "SET". Quindi
    SQL = "UPDATE Contacts " & ...
    Suggerimenti vari:
    1) non usare SQL come nome di variabile ma strSQL, sai mai...
    2) visto che la stringa è piuttosto lunga, dividine la creazione ma non andando a capo con l'underscore ma concatenando in serie la stringa a se stessa
    strSQL = "UPDATE Contacts "
    strSQL = strSQL & "SET Contacts.ID_SUPPLIER = " & Supp
    strSQL = strSQL & ", Contacts.First_Name = '" & Fname & "'"
    e via andare
  • Cavolo quanto sono stupido!!

    Il problema era lo spazio mancante. grazie mille
Devi accedere o registrarti per scrivere nel forum
2 risposte