DAO. Recordset Error

Randomblink

The Irreverent Reverend
Local time
Today, 13:10
Joined
Jul 23, 2001
Messages
279
Error Description: Update or CancelUpdate without AddNew or Edit. Error Number: 3020 Source: DAO.Recordset

I have update buttons that run like this one:

*****************************************
Private Sub btnPrintSlip_Click()
Dim rst As Recordset
On Error GoTo ErrPrintSlip

Set rst = Me.Recordset

Select Case Me.Dirty
Case -1 'Yes, the Form is Dirty
Select Case Me.NewRecord
Case -1 ' Yes, the Form is Dirty AND this is a NEW RECORD
rst.Update
Case 0 ' Yes, the Form is Dirty BUT this is NOT a NEW RECORD
With rst
.Edit
.Update
End With
End Select
Case 0 ' No the Form is NOT Dirty
End Select

Select Case Pprwrk_Type
Case "Invoice": DoCmd.OpenReport "rptInvoiceRoutingSlip", , , "[Pprwrk_ID] =" & Me.Pprwrk_ID
Case "RFA": DoCmd.OpenReport "rptRFATrackingSlip", , , "[Pprwrk_ID] =" & Me.Pprwrk_ID
End Select

Exit_PrintSlip:
Exit Sub

ErrPrintSlip:
MsgBox Err.Description
Resume Exit_PrintSlip

End Sub
*****************************************

Can someone point out what is wrong? Help!

Thanks in advance...
 
It looks as if you're trying to ensure the record is saved before printing (which is a very good thing to do ...)

If so, you are being unnecessarily complicated:

If Me.Dirty = True then Me.Dirty = False

will save the record if it has been edited.

HTH

Jeff
 
You could also use:

Docmd.RunCommand acCMDSaveRecord
 
Save if Dirty YES!

Ok, but will the: Docmd.RunCommand acCMDSaveRecord

or the: If Me.Dirty = True then Me.Dirty = False

Save it despite the record status?

If it is an old record, and edited, I get glitches sometimes...
Or if it is NOT edited, I get glitches...
I just want to make sure it gets saved before it prints...and some of the commands do glitches if the record is new and edited or old and not edited...

Argh!
 
The record will be saved whatever its status - unless there is a problem with validation or duplicate keys or requiered data missing etc. in any fields. It's up to you to check any controls (fields) before you try to save.

Jeff
 
There is an rst.edit missing from the first update line. And there is nothing to update anyway... but I presume you've left out some of the code? Get rid of the other select statement as shown

Private Sub btnPrintSlip_Click()
Dim rst As Recordset
On Error GoTo ErrPrintSlip

Set rst = Me.Recordset

if not (Me.Dirty) then goto do pprwrk:
Select Case Me.NewRecord
Case -1 ' Yes, the Form is Dirty AND this is a NEW RECORD
with rst
. Edit

'edit something

.Update
End with
Case 0 ' Yes, the Form is Dirty BUT this is NOT a NEW RECORD
With rst
.Edit

'edit something

.Update
End With
End Select

Do Papprwrk:
Select Case Pprwrk_Type
Case "Invoice": DoCmd.OpenReport "rptInvoiceRoutingSlip", , , "[Pprwrk_ID] =" & Me.Pprwrk_ID
Case "RFA": DoCmd.OpenReport "rptRFATrackingSlip", , , "[Pprwrk_ID] =" & Me.Pprwrk_ID
End Select

Exit_PrintSlip:
Exit Sub

ErrPrintSlip:
MsgBox Err.Description
Resume Exit_PrintSlip

End Sub
*****************************************
 
rst.edit - misunderstood I guess

See, I am mainly self-taught here...
And from what I understood of the Edit Method was that it would copy the currently edited record to the copy buffer so that you could then update it...

I forget what happened but when I had just the Update Method running there were errors that went away with putting the Edit Method in there...But, it looks like the If Me.Dirty=True then Me.Dirty=False is working...I haven't tried the other, I am afraid that if I have a record that is old and is NOT edited that the Save Command will bring up an error...
Sometimes I want to print out a report based on an old record...

Anyway, thank you all for the help!
This is excellent...
 
Me.Dirty=False is a trick. If you want to save the record, do it with the correct command. That way, you won't later confuse yourself or others.

DoCmd.RunCommand acCmdSaveRecord

Regardless of what method you use, the trick, your code, the correct method, or some other trick, if the record contains errors, you will get an error message. What I do to overcome this, is to have my edits in a separate function. Then I run the function and check its return value to determine if I can save without error.

If MyEditFunc() = True Then
DoCmd.RunCommand acCmdSaveRecord
Your print code
Else
Msgbox "some error message"
End If

Public Function MyEditFunc() As Boolean
MyEditFunc = True
edits - set MyEditFunc to False as necessary
End Function
 
Question for Pat

Ok, so will the: DoCmd.RunCommand acCmdSaveRecord
bring an error IF

The record is old and has no changes?
The record is new?
 
As Jeff Bailey said and Pat Hartman confirmed, this command saves the record regardless of its status, provided there are no inherent errors in the record. It's the same as saving the record by going to the Records menu and selecting "Save Record". Errors in the record will ultimately prevent you from saving it, whatever you try. If you are getting "glitches", please give detailed descriptions to give us a better idea of what's going wrong.
 

Users who are viewing this thread

Back
Top Bottom