Rabu, 05 April 2017

Re: [MS_AccessPros] Error Handling MS Template

 

OK, thanks.  Your way makes sense.  I just thought I must be missing something.  They did all their sub error handling in this manner so I thought there was some reason; maybe some error that would be caught with the "click" but before any of the sub statements.



---In MS_Access_Professionals@yahoogroups.com, <JohnV@...> wrote :

David-

Ooops.  Well, it won t catch anything because it s immediate replaced with the Resume Next.  If I were writing it from scratch, I would do:

Private Sub cmdPrint_Click()
Dim lngID As Long

On Error GoTo cmdPrint_Click_Err

    ' Save any edits
    If Me.Dirty Then Me.Dirty = False
    ' If nothing in ID,
    If (IsNull(Me.ID)) Then
        DoCmd.Beep
        ' Bail
        Exit Sub
    End If
    ' Save the value
    lngID = Me.ID
    ' Close me
    DoCmd.Close acForm, Me.Name
    ' Open the report filtered
    DoCmd.OpenReport "Task Details", acViewReport, WhereCondition:= "[TaskID]=" & lngID
    ' Print the report
    DoCmd.RunCommand acCmdPrint
    ' Open the next form filtered
    DoCmd.OpenForm "frmTaskDetails", WhereCondition:="[TaskID]=" & lngID

cmdPrint_Click_Exit:
    Exit Sub
 
cmdPrint_Click_Err:
    MsgBox Error$
    Resume cmdPrint_Click_Exit
 
 End Sub

John Viescas, Author
Effective SQL
SQL Queries for Mere Mortals 
Microsoft Access 2010 Inside Out
Microsoft Access 2007 Inside Out
Microsoft Access 2003 Inside Out
Building Microsoft Access Applications 
(Paris, France)




On Apr 5, 2017, at 9:01 PM, david.pratt@... [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:



John,
I think you missed the first OnError statement.  I did not use spacing very well when I copied the procedure.  The first OnError is the On Error GoTo statement.  And then it is followed immediately by the On Error Resume Next statement.  I am confused about what error the first On Error Go To statement can catch.


---In MS_Access_Professionals@yahoogroups.com, <JohnV@...> wrote :

David-

The first On Error tells Access to ignore any errors and go on to the next statement.  It s possible that the acCmdSaveNow will error out.  Note that the very next If statement checks to see if an error was generated, and if so, displays the error and exits.  The remainder of the code is fairly straightforward.  If there s no value in ID, then it makes a sound and exits.  If ID has a value, it saves it in a TempVar, closes the current form, opens a report with the ID filtered, prints it, then opens another form filtered.  Finally, it  clears the TempVar and exits.  The cmdPrint_Click_Err code isn t referenced anywhere, so it never executes.

John Viescas, Author
Effective SQL
SQL Queries for Mere Mortals 
Microsoft Access 2010 Inside Out
Microsoft Access 2007 Inside Out
Microsoft Access 2003 Inside Out
Building Microsoft Access Applications 
(Paris, France)




On Apr 5, 2017, at 7:47 PM, david.pratt@... [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:



For my education, I am trying to convert some MS templates from macros to code.  Can someone explain why the various MS template subs have two different error handling statements, one after the other, as below?  What error would the first error statement handle?  I assume the first error statement gets turned off as soon as the code reaches the second error statement?

 Private Sub cmdPrint_Click()
 On Error GoTo cmdPrint_Click_Err
 
 
     On Error Resume Next
     If (Form.Dirty) Then
         DoCmd.RunCommand acCmdSaveNow
     End If
     If (MacroError.Number <> 0) Then
         DoCmd.Beep
         MsgBox MacroError.Description, vbOKOnly, ""
         Exit Sub
     End If
     On Error GoTo 0
     If (IsNull(ID)) Then
         DoCmd.Beep
         Exit Sub
     End If
     TempVars.Add "FIlterID", ID.Value
     DoCmd.Close , ""
     DoCmd.OpenReport "Task Details", acViewReport, "", "[TaskID]=" & TempVars!FilterID, acNormal
     DoCmd.RunCommand acCmdPrint
     DoCmd.OpenForm "frmTaskDetails", acNormal, "", "[TaskID]=" & TempVars!FilterID, , acNormal
     TempVars.Remove "FIlterID"

 cmdPrint_Click_Exit:
     Exit Sub
 
 cmdPrint_Click_Err:
     MsgBox Error$
     Resume cmdPrint_Click_Exit
 
 End Sub








__._,_.___

Posted by: david.pratt@outlook.com
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (5)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.


.

__,_._,___

Tidak ada komentar:

Posting Komentar