Closing a form unwillingly edits first record in table (1 Viewer)

MikeLeBen

Still struggling
Local time
Today, 09:59
Joined
Feb 10, 2011
Messages
187
I have a form that is used to only add new records, and I noticed this odd behavior even though I haven't been able to reproduce it on purpose.

Basically there's a button to save the new record (for which the data is entered via a few combo boxes and text boxes), and only leave visible another button to add a new one.

When it misbehaves, basically a field in the first (i.e.: oldest) record in the table inherits the value from one of the comboboxes. I think, but I'm not certain, this happens after the new record is saved - as if after saving it the "bookmark" goes back to the first record in the table - and the form is closed.

It seems complex but actually it isn't, so here goes the code for this:
Code:
Option Compare Database
Option Explicit

Dim ctl As Control
Dim decorrenza As String


Private Sub Form_Open(Cancel As Integer)

Dim ctl As Control

If Not IsNull(Me.OpenArgs) Then
    For Each ctl In Me.Controls
    ctl.Visible = True
    Next ctl
    Debug.Print Me.OpenArgs
    
    DoCmd.GoToRecord , , acNewRec
    Me.IDCliente = Me.OpenArgs
End If

End Sub

Private Sub cboMese_AfterUpdate()

If IsNull(Me.cboAnno) Then
decorrenza = Switch(Me.cboMese = "GEN", "01/01/" & Year(Date), _
                    Me.cboMese = "FEB", "01/02/" & Year(Date), _
                    Me.cboMese = "MAR", "01/03/" & Year(Date), _
                    Me.cboMese = "APR", "01/04/" & Year(Date), _
                    Me.cboMese = "MAG", "01/05/" & Year(Date), _
                    Me.cboMese = "GIU", "01/06/" & Year(Date), _
                    Me.cboMese = "LUG", "01/07/" & Year(Date), _
                    Me.cboMese = "AGO", "01/08/" & Year(Date), _
                    Me.cboMese = "SET", "01/09/" & Year(Date), _
                    Me.cboMese = "OTT", "01/10/" & Year(Date), _
                    Me.cboMese = "NOV", "01/11/" & Year(Date), _
                    Me.cboMese = "DIC", "01/12/" & Year(Date))
Else
decorrenza = Switch(Me.cboMese = "GEN", "01/01/" & Me.cboAnno, _
                    Me.cboMese = "FEB", "01/02/" & Me.cboAnno, _
                    Me.cboMese = "MAR", "01/03/" & Me.cboAnno, _
                    Me.cboMese = "APR", "01/04/" & Me.cboAnno, _
                    Me.cboMese = "MAG", "01/05/" & Me.cboAnno, _
                    Me.cboMese = "GIU", "01/06/" & Me.cboAnno, _
                    Me.cboMese = "LUG", "01/07/" & Me.cboAnno, _
                    Me.cboMese = "AGO", "01/08/" & Me.cboAnno, _
                    Me.cboMese = "SET", "01/09/" & Me.cboAnno, _
                    Me.cboMese = "OTT", "01/10/" & Me.cboAnno, _
                    Me.cboMese = "NOV", "01/11/" & Me.cboAnno, _
                    Me.cboMese = "DIC", "01/12/" & Me.cboAnno)
End If

Debug.Print Me.cboMese

Me.txtDataDecorrenza = decorrenza

End Sub

Private Sub cboAnno_AfterUpdate()

If (decorrenza = "") Or (decorrenza = Null) Then
decorrenza = "01/01/" & Me.cboAnno
Else
decorrenza = Mid(decorrenza, 1, 6) & Me.cboAnno
End If

Me.txtDataDecorrenza = decorrenza

End Sub

Private Sub cmdConferma_Click()

Dim cdb As DAO.Database
Dim tbl As DAO.Recordset
Dim tbl_copy As DAO.Recordset

If IsNull(Me.cboNominativo) Then
    MsgBox "Devi inserire un nominativo!", 0, "Attenzione!"
Else
    If IsNull(Me.cboAnno) Or IsNull(Me.cboMese) Then
        MsgBox "Devi inserire il periodo ricetta!", 0, "Attenzione!"
    Else
        If IsNull(Me.cboValore) Or (Me.cboValore = "") Then
            MsgBox "Devi inserire il valore ricetta!", 0, "Attenzione!"
        Else
    
            'Se il box doppia ricetta è checkato, crea 2 record uguali
            If Me.chkdoppia = -1 Then
        
                Set cdb = CurrentDb
                Set tbl = cdb.OpenRecordset("Clienti_Ricette")
                Set tbl_copy = cdb.OpenRecordset("Clienti_Ricette")
            
                tbl.FindFirst "IDRicetta = " & Me.IDRicetta
                tbl_copy.AddNew
                tbl_copy.Fields("IDCliente") = tbl.Fields("IDCliente")
                tbl_copy.Fields("MeseRicetta") = tbl.Fields("MeseRicetta")
                tbl_copy.Fields("AnnoRicetta") = tbl.Fields("AnnoRicetta")
                tbl_copy.Fields("ValoreRicetta") = tbl.Fields("ValoreRicetta")
                tbl_copy.Fields("DataInserimento") = tbl.Fields("DataInserimento")
                tbl_copy.Fields("DataDecorrenza") = tbl.Fields("DataDecorrenza")
                tbl_copy.Update
            
                tbl.Close
                tbl_copy.Close
                cdb.Close
            End If
    
            'nasconde i controlli
            For Each ctl In Me.Controls
                If ctl.Name = "cmdNuovaRicetta" Then
                    ctl.Visible = True
                    ctl.SetFocus
    
                Else
    
                    If ctl.Name = "Auto_Logo0" Or ctl.Name = "Auto_Title0" Then
                        ctl.Visible = True
                    Else
                        ctl.Visible = False
    
                    End If
                End If
            Next ctl
    
            DoCmd.RunCommand acCmdSaveRecord
            MsgBox "Inserimento completato con successo.", 0, "Informazione"
            
            'Me.cboNominativo.Column(0) = 0
            decorrenza = ""
        End If
    End If
End If

End Sub

Private Sub cmdNuovaRicetta_Click()

For Each ctl In Me.Controls
ctl.Visible = True
Next ctl

DoCmd.GoToRecord , , acNewRec

End Sub
 

MikeLeBen

Still struggling
Local time
Today, 09:59
Joined
Feb 10, 2011
Messages
187
just found the problem was caused by an instruction on another form.
 

Users who are viewing this thread

Top Bottom