Problema con query Update

di il
20 risposte

Problema con query Update

Con la seguente query con parametri vorrei aggiornare una tabella.

Nella tabella sono già inseriti l'ambulatorio, la Data e l'Orario questi ultimi nel formato data/ora.

   Dim dataInput As Date
    Dim oraInput As Date
    Dim dataOra As Date

   
  
    sTabella = "Diario"
   
    dataInput = CDate(txtData.Text)
    oraInput = CDate(txtTime.Text)
    dataOra = DateValue(dataInput) + TimeValue(oraInput)
    
    sSQL = " UPDATE " & sTabella & " SET"
         sSQL = sSQL & " Visita = @Visita"
         sSQL = sSQL & " ,Ambulatorio=@Ambulatorio"
         sSQL = sSQL & " ,Data = @Data"
         sSQL = sSQL & " ,Orario = @Orario"
         sSQL = sSQL & " ,tVisita=@tVisita"
         sSQL = sSQL & " ,Cognome= @Cognome"
         sSQL = sSQL & " ,Nome = @Nome"
         sSQL = sSQL & " ,Sesso = @Sesso"
         sSQL = sSQL & " ,Nota=@Nota"
         sSQL = sSQL & " ,Telefono = @Telefono"
         sSQL = sSQL & " ,CodiceEs = @CodiceEs"
         sSQL = sSQL & " ,Terapia=@Terapia"
         sSQL = sSQL & " ,Comune=@Comune"
         sSQL = sSQL & " WHERE Ambulatorio=" & Val(lblAmbulatorio.Caption)
         sSQL = sSQL & " And Data = #" & Format(dataOra, "MM/dd/yyyy HH:nn:ss") & "#"
     
       Set cmd = New ADODB.Command
    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        .CommandText = sSQL
        
        SetADOParameter cmd, sTabella, "Visita", "A"
        SetADOParameter cmd, sTabella, "Ambulatorio", lblAmbulatorio.Caption
        SetADOParameter cmd, sTabella, "Data", txtData.Text
        SetADOParameter cmd, sTabella, "Orario", Format(txtTime.Text, "hh:nn:ss")
        SetADOParameter cmd, sTabella, "tVisita", cboTVisita.Text
        SetADOParameter cmd, sTabella, "Cognome", txtCognome.Text
        SetADOParameter cmd, sTabella, "Nome", txtNome.Text
        SetADOParameter cmd, sTabella, "Sesso", txtSesso.Text
        SetADOParameter cmd, sTabella, "Nota", txtNote.Text
        SetADOParameter cmd, sTabella, "Telefono", txtTelefono.Text
        SetADOParameter cmd, sTabella, "CodiceEs", txtCodiceEs.Text
        SetADOParameter cmd, sTabella, "Terapia", cboTerapie.Text
        SetADOParameter cmd, sTabella, "Comune", txtComune.Text
        
         cmd.Execute
    End With
    
     MsgBox "Dati Inseriti!", vbExclamation, Me.Caption
             
      frmDiario.Enabled = True
      frmDiario.CaricaFlex
      frmDiario.Show
      Unload Me

Arriva sino in fondo (msgbox visualizzata), ma di fatto non aggiorna il record.

Se elimino dalla query il campo Orario ( che è ASSOLUTAMENTE necessario) funziona. 

Quindi suppongo che il problema sia proprio lì.

Pubblico pure  Debug.Print  (perfetto!).

Grazie.

UPDATE Diario SET Visita = @Visita ,Ambulatorio=@Ambulatorio ,Data = @Data ,Orario = @Orario ,tVisita=@tVisita ,Cognome= @Cognome ,Nome = @Nome ,Sesso = @Sesso ,Nota=@Nota ,Telefono = @Telefono ,CodiceEs = @CodiceEs ,Terapia=@Terapia ,Comune=@Comune WHERE Ambulatorio=1 And Data = #03/13/2026 19:30:00# 

20 Risposte

  • Re: Problema con query Update

    13/03/2026 - pierovb ha scritto:

    Se elimino dalla query il campo Orario ( che è ASSOLUTAMENTE necessario) funziona. 

    Ma nel DB, il campo Orario è di tipo testo?

    Ma il metodo SetADOParameter è quello di Gibra?

  • Re: Problema con query Update

    14/03/2026 - Sgrubak ha scritto:

    Ma nel DB, il campo Orario è di tipo testo?

    No. ho provato sia  Ora breve 24h che con Ora Estesa.

    14/03/2026 - Sgrubak ha scritto:

    Ma il metodo SetADOParameter è quello di Gibra?

    Si. Del grande e compianto Giorgio Brausi. 

    Ho  Impostato la query in tanti modi, se escludo il campo orario (essenziale) funziona , altrimenti, pur non sollevando alcun errore, non aggiorna il recordset. 

  • Re: Problema con query Update

    14/03/2026 - pierovb ha scritto:

    No.

    e allora perché ci metti un testo? XD

    Non usare la Format che restituisce un testo. Inserisci l'orario direttamente. Prova

    SetADOParameter cmd, sTabella, "Orario", CDate(txtTime.Text)
  • Re: Problema con query Update

    Ho provato come da te suggerito, ma non funziona

     SetADOParameter cmd, sTabella, "Visita", "A"
     SetADOParameter cmd, sTabella, "Ambulatorio", lblAmbulatorio.Caption
     SetADOParameter cmd, sTabella, "Data", txtData.Text
     SetADOParameter cmd, sTabella, "Orario", CDate(txtTime .Text)
     SetADOParameter cmd, sTabella, "tVisita", cboTVisita.Text
     SetADOParameter cmd, sTabella, "Cognome", txtCognome.Text
     SetADOParameter cmd, sTabella, "Nome", txtNome.Text
     SetADOParameter cmd, sTabella, "Sesso", txtSesso.Text
     SetADOParameter cmd, sTabella, "Nota", txtNote.Text
     SetADOParameter cmd, sTabella, "Telefono", txtTelefono.Text
     SetADOParameter cmd, sTabella, "CodiceEs", txtCodiceEs.Text
     SetADOParameter cmd, sTabella, "Terapia", cboTerapie.Text
     SetADOParameter cmd, sTabella, "Comune", txtComune.Text
    
     sSQL = sSQL & " WHERE Ambulatorio=" & Val(lblAmbulatorio.Caption)
     sSQL = sSQL & " And Data = #" & Format(txtData.Text, "MM/dd/yyyy") & "#"
     sSQL = sSQL & " And Orario = #" & txtTime.Text & "#"

    Nel frattempo ci ho una pezza...  

    
            
    sSQL = sSQL & " WHERE Ambulatorio=" & Val(lblAmbulatorio.Caption)
    sSQL = sSQL & " And Id =" & Val(txtId.Text) ' ho inserito il campo univoco Id
    sSQL = sSQL & " And Data = #" & Format(txtData.Text, "MM/dd/yyyy") & "#"

    e funziona, ma vorrei capire perché non funziona con il campo Orario.

  • Re: Problema con query Update

    NON BASTA dire "non funziona", 

    DEVI ricuperare anche l'errore che viene generato lato database e il messaggio associato.

    Il messaggio ti dira' SICURAMENTE quale e' il problema.

  • Re: Problema con query Update

    14/03/2026 - migliorabile ha scritto:

    NON BASTA dire "non funziona", 

    DEVI ricuperare anche l'errore che viene generato lato database e il messaggio associato.

    Il messaggio ti dira' SICURAMENTE quale e' il problema.

    Il problema è proprio che non dà nessun errore, e nessun messaggio!

    Arriva sino in fondo (msgbox visualizzata), ma di fatto non aggiorna il record.

  • Re: Problema con query Update

    Ciao,

    Trattandosi di un orario (che non so come memorizzi), è possibile che il record da aggiornare non venga trovato perchè sono inclusi anche i secondi?

    In pratica se quando memorizzi il record la prima volta l'orario è 19:30:25, è esattamente quello il dato che deve trovarsi nella WHERE

    Questo potrebbe essere il motivo per il quale se inserisci ID e Data, senza orario, l'aggiornamento funziona.

    TheTruster

  • Re: Problema con query Update

    14/03/2026 - pierovb ha scritto:

    ma non funziona

    In questo codice hai modificato la WHERE della UPDATE. Prima l'avevi scritta

    sSQL = sSQL & " And Data = #" & Format(dataOra, "MM/dd/yyyy HH:nn:ss") & "#"

    ora è diventata

    sSQL = sSQL & " And Data = #" & Format(txtData.Text, "MM/dd/yyyy") & "#"
    sSQL = sSQL & " And Orario = #" & txtTime.Text & "#"

    14/03/2026 - pierovb ha scritto:

    vorrei capire perché non funziona con il campo Orario.

    A questo punto, credo dipenda da cosa intendi per il "campo Orario". Quello nel SET o quello nella WHERE? Nel caso sia nel SET, è probabile che non digerisca il testo, e serve la CDate. Nel caso sia nella WHERE, potrebbe essere che non vengano trovati record da aggiornare.

    Se fai una SELECT con quella WHERE, restituisce qualcosa? Se ottieni risultati, è impossibile che non aggiorni.

  • Re: Problema con query Update

    Ho inserito tutto in un nuovo pulsante. 

    Non dovrei mettere i campi Ambulatorio-Data-Orario perché non devono essere aggiornati, ma comunque devono far parte della query.

    Ho incluso i campi Data Orario

     Dim sTabella As String
       
       If VerificaCampi = False Then Exit Sub
        
        sTabella = "Diario"
             sSQL = " UPDATE " & sTabella & " SET"
             sSQL = sSQL & " Visita = @Visita"
             sSQL = sSQL & " ,tVisita=@tVisita"
             sSQL = sSQL & " ,Data = @Data"
             sSQL = sSQL & " ,Orario = @Orario"
             sSQL = sSQL & " ,Cognome= @Cognome"
             sSQL = sSQL & " ,Nome = @Nome"
             sSQL = sSQL & " ,Sesso = @Sesso"
             sSQL = sSQL & " ,Nota=@Nota"
             sSQL = sSQL & " ,Telefono = @Telefono"
             sSQL = sSQL & " ,CodiceEs = @CodiceEs"
             sSQL = sSQL & " ,Terapia=@Terapia"
             sSQL = sSQL & " ,Comune=@Comune"
             
             sSQL = sSQL & " WHERE Ambulatorio=" & Val(lblAmbulatorio.Caption)
             sSQL = sSQL & " And Data = #" & Format(txtData.Text, "MM/dd/yyyy") & "#"
             sSQL = sSQL & " And Orario = #" & txtTime.Text & "#"
            
             
             Set cmd = New ADODB.Command
        With cmd
            .ActiveConnection = cn
            .CommandType = adCmdText
            .CommandText = sSQL
            
            SetADOParameter cmd, sTabella, "Visita", "A"
            SetADOParameter cmd, sTabella, "tVisita", cboTVisita.Text
            SetADOParameter cmd, sTabella, "Data", txtData.Text
            SetADOParameter cmd, sTabella, "Orario", CDate(txtTime.Text)
            SetADOParameter cmd, sTabella, "Cognome", txtCognome.Text
            SetADOParameter cmd, sTabella, "Nome", txtNome.Text
            SetADOParameter cmd, sTabella, "Sesso", txtSesso.Text
            SetADOParameter cmd, sTabella, "Nota", txtNote.Text
            SetADOParameter cmd, sTabella, "Telefono", txtTelefono.Text
            SetADOParameter cmd, sTabella, "CodiceEs", txtCodiceEs.Text
            SetADOParameter cmd, sTabella, "Terapia", cboTerapie.Text
            SetADOParameter cmd, sTabella, "Comune", txtComune.Text
            
             cmd.Execute
        End With
        
        MsgBox "Dati Aggiornati!", vbExclamation, Me.Caption

    Niente..

    escludendo i campi Data e  Orario

     Dim sTabella As String
       
       If VerificaCampi = False Then Exit Sub
        
        sTabella = "Diario"
             sSQL = " UPDATE " & sTabella & " SET"
             sSQL = sSQL & " Visita = @Visita"
             sSQL = sSQL & " ,tVisita=@tVisita"
             'sSQL = sSQL & " ,Orario = @Orario"
             sSQL = sSQL & " ,Cognome= @Cognome"
             sSQL = sSQL & " ,Nome = @Nome"
             sSQL = sSQL & " ,Sesso = @Sesso"
             sSQL = sSQL & " ,Nota=@Nota"
             sSQL = sSQL & " ,Telefono = @Telefono"
             sSQL = sSQL & " ,CodiceEs = @CodiceEs"
             sSQL = sSQL & " ,Terapia=@Terapia"
             sSQL = sSQL & " ,Comune=@Comune"
             
             sSQL = sSQL & " WHERE Ambulatorio=" & Val(lblAmbulatorio.Caption)
             sSQL = sSQL & " And Data = #" & Format(txtData.Text, "MM/dd/yyyy") & "#"
             sSQL = sSQL & " And Orario = #" & CDate(txtTime.Text) & "#"
           Set cmd = New ADODB.Command
        With cmd
            .ActiveConnection = cn
            .CommandType = adCmdText
            .CommandText = sSQL
            
            SetADOParameter cmd, sTabella, "Visita", "A"
            SetADOParameter cmd, sTabella, "tVisita", cboTVisita.Text
            
    SetADOParameter cmd, sTabella, "Cognome", txtCognome.Text
            SetADOParameter cmd, sTabella, "Nome", txtNome.Text
            SetADOParameter cmd, sTabella, "Sesso", txtSesso.Text
            SetADOParameter cmd, sTabella, "Nota", txtNote.Text
            SetADOParameter cmd, sTabella, "Telefono", txtTelefono.Text
            SetADOParameter cmd, sTabella, "CodiceEs", txtCodiceEs.Text
            SetADOParameter cmd, sTabella, "Terapia", cboTerapie.Text
            SetADOParameter cmd, sTabella, "Comune", txtComune.Text
            
             cmd.Execute
        End With
        
        MsgBox "Dati Aggiornati!", vbExclamation, Me.Caption

    nada...

    Sicuramente sbaglio o ometto qualcosa, ma non riesco a venirne a capo anche perché non mi dà nessun errore.

    Grazie

  • Re: Problema con query Update

    14/03/2026 - pierovb ha scritto:

    non riesco a venirne a capo

    Oltre a verificare la questione dei secondi, di cui parlavo prima, potresti provare ad impostare anche il valore passato per la clausola WHERE come parametro, così da liberarti della conversione #MM/dd/yyyy#

    TheTruster

  • Re: Problema con query Update

    Direttamente da access  

    SQL 

    UPDATE Diario SET Diario.Cognome = "Piccolomini", Diario.Nome = "Arturo"
    WHERE (((Diario.Ambulatorio)=1) AND ((Diario.Data)=#3/13/2026#) AND ((Diario.Orario)=#12/30/1899 7:40:0#));

    Formattata correttamente la data, mentre l'orario ?!?

    Il campo Orario ho provato ad  impostarlo come data breve 12 o 24 (per evitare i secondi), ma nella sql non cambia la formattazione.

  • Re: Problema con query Update

    14/03/2026 - pierovb ha scritto:

    Formattata correttamente la data, mentre l'orario

    L'impostazione del campo come DataBreve dovrebbe incidere solo sulla visualizzazione del dato e non sull'effettivo valore inserito nel DB, quindi è relativo.

    Io credo che la data aggiunta (che poi è la data "zero" di Access) sia il risultato del campo impostato (correttamente) come DateTime, solo che mancando la data nel campo della WHERE, Access la setta sul suo valore zero.
    Secondo me, se passi il parametro piuttosto che la stringa, alla WHERE, potrebbe funzionare:

            sSQL = sSQL & " WHERE Ambulatorio=@AmbulatorioW" '<--- tanto vale passare anche questo
            sSQL = sSQL & " And Data = @DataW"
            sSQL = sSQL & " And Orario = @OrarioW"

    quindi poi aggiungi i parametri:

            SetADOParameter cmd, sTabella, "AmbulatorioW", Val(lblAmbulatorio.Caption)
            SetADOParameter cmd, sTabella, "DataW", CDate(txtData.Text)
            SetADOParameter cmd, sTabella, "OrarioW", CDate(txtTime.Text)

    TheTruster

  • Re: Problema con query Update

    Niente, neanche così.

    comunque ho risolto impostando la query con il campo id.

     sSQL = sSQL & " WHERE Ambulatorio=" & Val(lblAmbulatorio.Caption)
     sSQL = sSQL & " And Id =" & Val(txtId.Text)
     sSQL = sSQL & " And Data = #" & Format(txtData.Text, "MM/dd/yyyy") & "#"

    Grazie.

  • Re: Problema con query Update

    Comunque, perdonami - forse non ho ben chiara la struttura della tua tabella - ma se devi aggiornare una riga del tuo Diario, perchè hai bisogno fare una WHERE che comprenda Data ed Ora se, a quanto pare, hai a disposizione l'ID (che suppongo essere quello della prenotazione)?

    Non basterebbe solo quello, per identificare univocamente il record su cui effettuare l'Update?

    TheTruster

Devi accedere o registrarti per scrivere nel forum
20 risposte