Results 1 to 5 of 5
  1. #1
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413

    A Variant Canard?

    It seems to be gospel that an untyped variable will be defaulted to type variant. I decided to try and verify that. Here's the result.
    It seems that a variable without an explicit Type assignment when declared, will take on the Type of its later assigned value.
    Interestingly, a declared variant without a value assignment will have Type 'Empty'

    Code:
    Public Sub DT()
        Dim s1, s2 As String: s1 = "Hello World"
        Dim b1, b2 As Boolean: b1 = "Me Too"
        Dim n1, n2 As Long: n1 = False
        Dim v1, v2 As Variant: v1 = #12/1/2020#
        
        Debug.Print "s1 Type result:"; TypeName(s1)
        Debug.Print "s2 Type result:"; TypeName(s2)
        
        Debug.Print "b1 Type result:"; TypeName(b1)
        Debug.Print "b2 Type result:"; TypeName(b2)
        
        Debug.Print "n1 Type result:"; TypeName(n1)
        Debug.Print "n2 Type result:"; TypeName(n2)
        
        Debug.Print "v1 Type result:"; TypeName(v1)
        Debug.Print "v2 Type result:"; TypeName(v2)
    End Sub
    Immediate:


    call dt
    s1 Type result:String
    s2 Type result:String
    b1 Type result:String
    b2 Type result:Boolean
    n1 Type result:Boolean
    n2 Type result:Long
    v1 Type resultate
    v2 Type result:Empty

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You probably have never seen this, then

    http://allenbrowne.com/vba-NothingEmpty.html

    In the case you mention, it is not of type Empty. The variable IS empty, or at least that's how I understand it. About as confounding as Null, I suppose.

    EDIT - I just reviewed TypeName function. Documentation states that it returns information about a variable. It doesn't explicitly state that it returns the variable type.
    Last edited by Micron; 01-06-2023 at 04:42 PM. Reason: added comment
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Seeing Allen Brown's article and expanding his example to include the untyped var1, it does indeed appear to be a variant.
    An apparent assumption here is that declaring a variable as string or variant makes little difference.

    Code:
    Public Sub variantDeclare()
        Dim var1, var2 As String
        If var1 = 0 Then
          Debug.Print "The uninitialized variant behaves as zero."
        End If
        If var1 = "" Then
          Debug.Print "The uninitialized variant behaves as a ZLS."
        End If
    End Sub
    Immediate
    call variantdeclare
    The uninitialized variant behaves as zero.
    The uninitialized variant behaves as a ZLS.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I'd say it will depend entirely upon what you try to pass to it. Pretty sure you cannot pass Null to a string variable but can for variant, so I can't really agree with your conclusion.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    If you look in the Locals window when you step through the code, you'll see
    var1-----variant/empty
    var2-----string

    Don't know if that adds to the confusion, but I accept the type from the Locals window.

    Also, based on Post #1, these are the values in locals window

    Click image for larger version. 

Name:	Post1Locals.PNG 
Views:	3 
Size:	5.6 KB 
ID:	49461
    Last edited by orange; 01-07-2023 at 09:51 AM.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Current variant of the bookmark bug
    By JezPerth in forum Modules
    Replies: 1
    Last Post: 02-24-2016, 03:31 AM
  2. Add an additional Variant to the iif statment
    By Ian Frost in forum Modules
    Replies: 3
    Last Post: 04-14-2015, 08:25 AM
  3. Replies: 6
    Last Post: 11-16-2013, 06:06 PM
  4. Converting Variant Listbox Results
    By dreamnauta in forum Programming
    Replies: 4
    Last Post: 01-03-2012, 12:24 PM
  5. Replies: 2
    Last Post: 09-27-2010, 02:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums