Copy record code

  • Thread starter lmcc via AccessMonster.com
  • Start date
L

lmcc via AccessMonster.com

When I converted the macro to code. Here is what I got:

On Error GoTo copyofrecord1_Err

With CodeContextObject
On Error Resume Next
DoCmd.RunCommand acCmdSelectRecord
If (.MacroError = 0) Then
DoCmd.RunCommand acCmdCopy
End If
If (.MacroError = 0) Then
DoCmd.RunCommand acCmdRecordsGoToNew
End If
If (.MacroError = 0) Then
DoCmd.RunCommand acCmdSelectRecord
End If
If (.MacroError = 0) Then
DoCmd.RunCommand acCmdPaste
End If
If (.MacroError <> 0) Then
Beep
MsgBox .MacroError.Description, vbOKOnly, ""
End If
End With


copyofrecord1_Exit:
Exit Function

copyofrecord1_Err:
MsgBox Error$
Resume copyofrecord1_Exit

End Function


What does the "With CodeContextObject" and "(.MacroError = 0)" mean?


Since I don’t quite understand the code, I decided to create my own code. I
created this code to copy a record to a new record.
Private Sub cmdCopyRecord_Click()

On Error GoTo ErrorHandler

DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdCopy
DoCmd.RunCommand acCmdRecordsGoToNew
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdPaste

Exit:
Exit Sub

ErrorHandler:
Call MsgBox("An error was encountered" & vbCrLf & vbCrLf & _
"Description: " & Err.Description & vbCrLf & _
"Error Number: " & Err.Number, , "Error")
Resume Exit

End Sub

My concern is that in the macro they do error checking throughout. Should I
be doing that also? Is my code okay?

Any opinions are appreciated.
 
J

John Spencer

Your code looks fine. If you encounter an error on any line the code will
skip to your error handler and then exit the sub.

I think the original code will check if there is an error before each line and
if one exists will skip to the next check. 0 equates to NO error.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
L

lmcc via AccessMonster.com

Thanks John,

You know I was thinking that it could be written shorter. I needed some
reassurance. I saw some copy record code with RecordsetClone, but I didn't
understand it.

Thanks!

John said:
Your code looks fine. If you encounter an error on any line the code will
skip to your error handler and then exit the sub.

I think the original code will check if there is an error before each line and
if one exists will skip to the next check. 0 equates to NO error.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
When I converted the macro to code. Here is what I got:
[quoted text clipped - 59 lines]
Any opinions are appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top