Solved Update a record with a recordset (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 07:20
Joined
Oct 14, 2019
Messages
463
I have a continuous form that's a check register and the query behind it is not updatable.
Double clicking in any field transfers the record fields to unbound fields in the header.
I have saving a new record working but I'm a little lost on updating an existing record.
How do I make sure the update goes back to the same record?
In other words, if the ID is null a new record is created in the table.
The second part (else) is what doesn't work:

Code:
Private Sub btnSave_Click()
    Dim rst As DAO.Recordset
    Dim strSql As String
    strSql = "Select * from tblTransactions " & _
        "WHERE TransactionID = " & Nz(Me.txtTransactionID, 0)
    Set rst = CurrentDb.OpenRecordset(strSql)
   
    If IsNull(Me.txtTransactionID) Then
        With rst
            .AddNew
            fAccountID = Me.txtfAccountID
            TransactionID = Me.txtTransactionID
            CkDate = Me.txtDate
            Num = Me.txtNum
            Payee = Me.txtPayee
            Debit = Me.txtDebit
            Credit = Me.txtCredit
            Cleared = Me.txtCleared
            .Update
        End With
    Else
        With rst
            .Update  'update or cancel without add new or edit
            fAccountID = Me.txtfAccountID
            TransactionID = Me.txtTransactionID
            CkDate = Me.txtDate
            Num = Me.txtNum
            Payee = Me.txtPayee
            Debit = Me.txtDebit
            Credit = Me.txtCredit
            Cleared = Me.txtCleared
        End With
    End If
    Set rst = Nothing
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:20
Joined
Oct 29, 2018
Messages
21,473
You need to make sure you're on the right record before using .Update. You could probably use .FindFirst or something.
 

plog

Banishment Pending
Local time
Today, 09:20
Joined
May 11, 2011
Messages
11,646
Debit and credits shouldn't be in separate fields in your table. You should just have a TransAmount field and either use positive or negative numbers to differentiate. Makes obtaining a balance easier.
 

cheekybuddha

AWF VIP
Local time
Today, 15:20
Joined
Jul 21, 2014
Messages
2,280
You aren't referencing fields in the recordset rst, just the fields in your form with those field names.

Either add a bang (!) before each field name, or reference it using the syntax: .Fields("FieldName") = ...

But also your AddNew and Update logic seems off too.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:20
Joined
May 7, 2009
Messages
19,245
Code:
Private Sub btnSave_Click()
    Dim rst As DAO.Recordset
    Dim strSql As String
    strSql = "Select * from tblTransactions " & _
        "WHERE TransactionID = " & Nz(Me.txtTransactionID, 0)
    Set rst = CurrentDb.OpenRecordset(strSql)
 
    With rs
        If IsNull(Me.txtTransactionID) Then
            .AddNew
        Else
            .Edit
        End If
        !fAccountID = Me.txtfAccountID
        !CkDate = Me.txtDate
        !Num = Me.txtNum
        !Payee = Me.txtPayee
        !Debit = Me.txtDebit
        !Credit = Me.txtCredit
        !Cleared = Me.txtCleared
        .Update
    End With
    rs.Close
    Set rst = Nothing
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:20
Joined
Feb 19, 2002
Messages
43,275
Rather than copying the fields to unbound controls on the form's header, just open a one record form. Then you can use a bound form and do proper validation and ignore all the other code that unbound updating requires.

DoCmd.OpenForm "frmMDOffce", , , "MDID = " & Me.MDID, , acDialog
Me.Refresh

I open popup forms in dialog mode so that focus always stays on the popup until it closes. Then when focus returns to the list form, refresh it so you see any changes made by the popup.
 

ClaraBarton

Registered User.
Local time
Today, 07:20
Joined
Oct 14, 2019
Messages
463
I do have a pop up form and that's where I go for categories, etc. But I wanted this option too.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:20
Joined
May 7, 2009
Messages
19,245
@arnelgp - rs / rst
1rapiq.jpg
 

cheekybuddha

AWF VIP
Local time
Today, 15:20
Joined
Jul 21, 2014
Messages
2,280
You have a typo in your code in Post #5:
Code:
' ...
    Set rst = CurrentDb.OpenRecordset(strSql)
 
    With rs
' ...
    rs.Close
    Set rst = Nothing
' ...
 

Users who are viewing this thread

Top Bottom