Behind on posts a bit.
Will try to catch up.
Banana, yes I'm familiar with the distinction and implementations of Properties.
I agree there are uses for Properties (not just at custom class level where they are, naturally, hugely important).
But not needing to
set the connection object - a returning function is just fine for returning the object. If it
were required, I agree a property would likely make for neater coding than a distinctly named sub for assigning it.
It's primarily the use of the static object that I'd rather avoid.
(Not that I
never use them, though generally it's for convenient, transportable example code - e.g. for forum posting
Otherwise no, your code doesn't particularly differ in concept.
It's all about that self healing and single location.
Fundamentally, the example I suggested is air code based on real concepts (as I'm sure yours is) - not based on actual full procedures of mine (attempting to demonstrate the core concept only rather than full live procedures).
None the less it was written with reason to my apparent madness.
The Resume Next error handling was to allow for failing due to checking
If mcnn Is Nothing Or mcnn.Status = 0 Then
If the connection object is indeed nothing - then mcnn.Status will raise a runtime (object variable) error.
We could handle that of course and resume
into the procedure to carry out the opening (but then this is aircode... :-s)
That explains the Resume Next use hopefully.
Equally we could handle this by splitting out into separate checks including
If mcnn Is Not Nothing Then
If mcnn.Status = 0 Then
...
Again - I didn't go down this route, wanting to keep this demonstration as direct as possible for readers.
And on to reasons for failure.
Dropping of the connection object - that's got to be comparatively unlikely.
Obviously stopped code (i.e. ended, as in after an unhandled error) will kill the object be it module level or in procedure static - but that's hopefully a very unlikely event in an application (if not impossible, for example in a runtime install).
Other factors could come in to play, but for me - far more likely is the potential that the connection simply closes.
The Connection object itself will remain a valid connection object until some VBA issue causes otherwise (or we issue a
Set mcnn = Nothing).
However server or network problems could easily see the
open connection dropped - in which case a check such as
If cnn Is Nothing Then
will happily pass the test as the object variable is typed as a connection and has been instantiated, but which just happens to be closed - exactly as it is after
Set mcnn = New ADODB.Connection
but before
mcnn.Open ....
Hence my check for the State of the connection to determine if it's still open.
Is this foolproof either? Gawd no. Is anything?
Is it more comprehensive? How can it not be.
But what's offered is deliberately conceptual.
Anyway - I suspect Mike wants some more walking through the concept.
Since he seems to be following "I can keep the connection all contained within one function" then I'll leave you to it.