Why does this compile? td != qd (2 Viewers)

tvanstiphout

Active member
Local time
Today, 00:27
Joined
Jan 22, 2016
Messages
632
I expected this to fail compilation on the indicated line.
Decompile did not make a difference.
This is A365-32 MEC. I was using a test module in Northwind 2 Dev Edition.

Code:
Sub test1()
    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Dim qd As DAO.QueryDef
    
    Set db = CurrentDb
    Set td = db.TableDefs("Orders")
    Set qd = db.QueryDefs("qryOrder")
    Debug.Print GetTableDefProperty(td, "Name")
    Debug.Print GetTableDefProperty(qd, "Name")    '<== Why does this compile?
End Sub

Private Function GetTableDefProperty(td As DAO.TableDef, ByVal strProp As String) As Variant
    GetTableDefProperty = td.Properties(strProp)
End Function
 
I suspect that DAO.TableDef and DAO.QueryDef extend the same class.

Of course it would be simpler to access the properties directly.
debug.print tc.Name
debug.print qd.Name

or

debug.print db("Name")
db("Name") qd("Name")
 
> Of course it would be simpler to access the properties directly.
Yes, in this example. The actual code is more complicated, but nevertheless, a querydef object is not a tabledef object. Does not even come close in having the same public interface.
At runtime we get the correct Type Mismatch error, but there is nothing at compile time. Weird.
 
Actually they are very similar. I looks like the VBA compiler is being its helpful self when handling type conversions.

BTW it compiles on my computer with out error.
 
As a test I added a few lines just to show that the level of sameness of the two objects is not a factor:
Code:
    Dim dict As Scripting.Dictionary
    Set dict = StringToDictionary("a=1")
    Debug.Print GetTableDefProperty(dict, "Name")   '<==Why does this compile?
 
Could always report it?

I have a view. If someone does not know something is broken, how can they fix it, unless they have been told?
 
As a test I added a few lines just to show that the level of sameness of the two objects is not a factor:
Code:
    Dim dict As Scripting.Dictionary
    Set dict = StringToDictionary("a=1")
    Debug.Print GetTableDefProperty(dict, "Name")   '<==Why does this compile?
Scripting.Dictionary is nothing like a table or query object. My point was that those 2 objects are derived for the same base class and VBA thoughtfully decided they were interchangeable in this case.
 
The parameters in question all have in common that they are objects. This might give us a little unexpected insight into when data types are actually evaluated in a formal parameter list.

For that call to GetTableDefProperty( X, string ) you have X, an object that is ByRef, and ByRef arguments are evaluated at run-time for exact matches. Object as a datatype is semi-amorphous in that it can be any type of object, and the compiler apparently gives any object a pass to the run-time evaluator. But the run-time evaluator is less forgiving. At least, that is what it looks like to me.
 
apparently
Apparently? You disappoint me Doc. ;-) You're correct, but you usually reference the VBA Language Specification to underpin your statements in such kind of matters.

The Set Coercion section of the specification details that the static semantics (compile time) allow any type of object (and Variant) as source/destination types for the type coercion. Only the runtime semantics require that the source type must implement the destination type for a successful type coercion.
 
Apparently? You disappoint me Doc. ;-) You're correct, but you usually reference the VBA Language Specification to underpin your statements in such kind of matters.

The Set Coercion section of the specification details that the static semantics (compile time) allow any type of object (and Variant) as source/destination types for the type coercion. Only the runtime semantics require that the source type must implement the destination type for a successful type coercion.

Yes, I normally do hit that reference, but I was pressed for time. Wifey was calling for help with a "honey-do" and I'm sorry to have admit to my friends here that she has higher priority.
 

Users who are viewing this thread

Back
Top Bottom