Solved How does this work?

Gasman

Enthusiastic Amateur
Local time
Today, 17:51
Joined
Sep 21, 2011
Messages
15,661
OK, internet was down at my house for some of the morning, so I decided just to browse Northwind 2007.
They have an error logging routine
Code:
Option Compare Database
Option Explicit

Private m_rs As DAO.Recordset2

Public Function OpenRecordset(Domain As String, _
                              Optional Criteria As String = "1=1", _
                              Optional OrderBy As String, _
                              Optional RecordsetType As DAO.RecordsetTypeEnum = dbOpenDynaset, _
                              Optional RecordsetOptions As DAO.RecordsetOptionEnum _
                              ) As Boolean
    
    
    If Not m_rs Is Nothing Then
        ' Close the recordset so it can be re-used
        CloseRecordset
    End If
    
    Dim strSQL As String
    strSQL = "SELECT * FROM [" & Domain & "] WHERE " & Criteria
    
    If OrderBy <> "" Then
        strSQL = strSQL & " ORDER BY " & OrderBy
    End If
    
    On Error GoTo ErrorHandler
    Set m_rs = CurrentDb.OpenRecordset(strSQL, RecordsetType, RecordsetOptions)
    OpenRecordset = True

Done:
    Exit Function
ErrorHandler:
    ' verify the private Recordset object was not set
    Debug.Assert m_rs Is Nothing
    
    ' Resume statement will be hit when debugging
    If eh.LogError("RecordsetWrapper.OpenRecordset", "strSQL = " & Chr(34) & strSQL & Chr(34)) Then Resume
End Function
Nothing wrong with that you might say, but the name of the module LogError is in is actually called ErrorHandling ?
Code:
Public Function LogError(strLocation As String, ParamArray State()) As Boolean
    Dim strMsg As String
    Dim strState As String
    
    ' Build the error message to display
    strMsg = Err.Description & " (" & Err.Number & ")" & vbCrLf & vbCrLf & strLocation
    
    strState = Join(State, vbCrLf)
    
    If strState <> "" Then
        strMsg = strMsg & vbCrLf & vbCrLf & strState
    End If
    
    ' Display the error
    MsgBox strMsg, vbCritical
    
    ' If debugging is supported, break using Debug.Assert.
    If DebuggingSupported() Then
        Debug.Assert False  ' Stop code so that you can debug
        LogError = True     ' Step over this line if you don't want to resume
    End If
End Function
Include pic of left window

So my question is, how does Access recognise eh. as Errorhandling?, as you can see in the first code block they actually use the whole name for RecordsetWrapper.
 
is the errorhandling module a class module?

If so is eh a global variable set to errorhandling when the app opens?
 
What happens if you put the cursor in "errorhandler" and press shift-F2? That should jump to the actual code block.

I don't see that it can be going to LogError because there is no "return" in that.
 
is the errorhandling module a class module?

If so is eh a global variable set to errorhandling when the app opens?
Yes, they are both class modules.

Yes spot on, in Declarations
Code:
Public eh As New ErrorHandling

Thank you both.
 
That's a neat way of doing it.
Would you have to check whether errorhandling was not nothing before you trued to use it?
 
What happens if you put the cursor in "errorhandler" and press shift-F2? That should jump to the actual code block.

I don't see that it can be going to LogError because there is no "return" in that.
If I put the cursor on the line with eh.logerror, it jumps to that function in the errorhandler module.
I never knew you could do that either. :)
That's a neat way of doing it.
Would you have to check whether errorhandling was not nothing before you trued to use it?
No idea, I was just browsing to kill time.
It is defined in a module Utilities. That is the only place I can find eh and a space
1661512205569.png
 
That's a neat way of doing it.
Would you have to check whether errorhandling was not nothing before you trued to use it?
I don't think in VBA this is an issue.
As shown in the picture, ErrorHandling is a class and the
Code:
Public eh As New ErrorHandling
will always run.
Even if that line failed, it is save to say that you cannot open the file.
If for some odd reason, the instance got destroyed, it is still not really an issue because it only resume if in debug mode.
in any other case, the error will end the procedure.
 
I have never included the module name in calling a function/sub in a module?
Why would they feel the need, unless there were two by the same name perhaps? Is that allowed even?
 
^^^
@Grumm

My bad, really.
I didn't bother to post again, but when I first looked at the original post, I didn't realise there was a scroll bar, and I thought the goto errorhandler was managing to go to another procedure, which of course it couldn't. And then I thought the errorhandler point must be inside the class module, again not possible.
 
I have never included the module name in calling a function/sub in a module?
Why would they feel the need, unless there were two by the same name perhaps? Is that allowed even?
The function is also public so, good question.
It can be useful when using private functions.

Other than that, to show some 'best practices' in a demo project ?
 
Paul,
I watched a few videos by Jack Panyakone. He uses that technique quite often. (module.proc)
Here's an example. If you look at the code you'll see things like

Code:
....Then Call EmployeeDBAccess.Logout(Me.txtEmployeeID)

Taken from 7:45 time on this youtube video
 
Paul,
I watched a few videos by Jack Panyakone. He uses that technique quite often. (module.proc)
Here's an example. If you look at the code you'll see things like

Code:
....Then Call EmployeeDBAccess.Logout(Me.txtEmployeeID)

Taken from 7:45 time on this youtube video
Thanks Jack,
So I am assuming you could have multiple same named functions/sub as long as they exist in different modules?
I suppose it doesalso document where a function is as well, as I often had to go looking for which Module I might have saved something in. :)
 
the function could be modified to update a error log table for someone to review later. Certainly on the basis of reporting unhandled errors in the production environment
 
Hmm? Haven't tested but I don't think you can have >1 sub/function with a specific name.
I also have to search around for a function or sub or create anew. I have a poor system for storing code.
I saw a post by Colin/isladogs recently where he mentioned his way of storing and retrieving code.
I'm going to go find it and read it a few times.
I can't find that reference at the moment.

Isladogs may see this and point us to the post on how he manages code.
 
I don't think you can have >1 sub/function with a specific name
you can if they are private and in different modules, but not if public

Classic example would be common bit of code used in multiple forms - but with slight variations in the code to tailor it to the specific form
 
Thanks Jack,
I do not use Access much at all now, and I only dabbled with it when I did. :)
 
I agree Dave.
Paul, I am 15 yrs retired so trying to find things/modules is a challenge for sure.
Still it might be interesting to attempt some housecleaning/code management.
I do have library database as reference for things but a mish-mash at best.
 

Users who are viewing this thread

Back
Top Bottom