Results 1 to 16 of 16

Thread: Null

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Null

    I'm passing ByRef a variant called files to a subroutine

    It's being set there to Null and my next lines are

    Code:
    If files= Null then Stop
    Stop
    It stops on the Stop, not the line with If.. Then, but if I hover the mouse over the variable name it shows it's Null.
    Why is that ?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Null

    Edit: Never mind...

  3. #3

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Null

    Yep, Eduardo nailed it.

    OptionBase1, a Null is a funny animal. Once you get a Null in any expression (boolean, math, whatever), the results is always Null. Therefore, your files = Null returns Null, and not True.

    When treating a Null as a Boolean (which an If... statement does), about the closest they could come was False. Therefore, anytime a Null (or two, which was your case) is in an expression, it will ALWAYS be False.

    Best Of Luck,
    Elroy

    p.s. And that's precisely why we have the IsNull() function.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Null

    Yes I found that worked, but shouldn't condition "Files= Null" work? Maybe nothing can be null, or null is nothing
    but it does allow me to set it to null.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Null

    > Once you get a Null in any expression (boolean, math, whatever), the results is always Null.

    Thanks Elroy.. must remember that!

  7. #7
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Null

    Quote Originally Posted by AlexanderBB View Post
    Yes I found that worked, but shouldn't condition "Files= Null" work? Maybe nothing can be null, or null is nothing
    but it does allow me to set it to null.
    I don't know why that doesn't work.
    May be there is a philosophical thing with that. May be a null is not equal to another null.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Null

    Here, for total grins, here's how to get an IsNull the long way. Focus on the "IsNullMyWay" function. And feel free to trace it out. Just throw it into a BAS module to play with it.

    And again, ALL expressions that contain a Null will evaluate to Null (and not True or False).

    Code:
    
    Option Explicit
    
    Private Type VariantDescriptor
        Type As Integer
        Reserved As String * 6
        Data As String * 8
    End Type
    Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Dest As Any, ByRef Source As Any, ByVal Bytes As Long)
    
    
    Public Function IsNullMyWay(v As Variant) As Boolean
        IsNullMyWay = Left$(sVariantDesc(v), 4) = "Null"
    End Function
    
    
    Public Function sVariantDesc(v As Variant) As String
        ' See https://msdn.microsoft.com/en-us/library/windows/desktop/ms221627(v=vs.85).aspx       for more information.
        '
        ' Or https://msdn.microsoft.com/en-us/library/cc237865.aspx                                 for an Enum with all the variant types.
        '
        'Private Enum VarEnum
        '    VT_EMPTY       = &H0       ' vbEmpty
        '    VT_NULL        = &H1       ' vbNull
        '    VT_I2          = &H2       ' vbInteger
        '    VT_I4          = &H3       ' vbLong
        '    VT_R4          = &H4       ' vbSingle
        '    VT_R8          = &H5       ' vbDouble
        '    VT_CY          = &H6       ' vbCurrency
        '    VT_DATE        = &H7       ' vbDate
        '    VT_BSTR        = &H8       ' vbString
        '    VT_DISPATCH    = &H9       ' vbObject
        '    VT_ERROR       = &HA       ' vbError
        '    VT_BOOL        = &HB       ' vbBoolean
        '    VT_VARIANT     = &HC       ' vbVariant
        '    VT_UNKNOWN     = &HD       ' vbDataObject (IUnknown)
        '    VT_DECIMAL     = &HE       ' vbDecimal
        '    VT_I1          = &H10
        '    VT_UI1         = &H11      ' vbByte
        '    VT_UI2         = &H12
        '    VT_UI4         = &H13
        '    VT_I8          = &H14      '       LongLong (signed), if ByVal works in Variant.
        '    VT_UI8         = &H15
        '    VT_INT         = &H16
        '    VT_UINT        = &H17
        '    VT_VOID        = &H18
        '    VT_HRESULT     = &H19
        '    VT_PTR         = &H1A
        '    VT_SAFEARRAY   = &H1B
        '    VT_CARRAY      = &H1C
        '    VT_USERDEFINED = &H1D
        '    VT_LPSTR       = &H1E
        '    VT_LPWSTR      = &H1F
        '    VT_RECORD      = &H24      ' vbUserDefinedType
        '    VT_INT_PTR     = &H25
        '    VT_UINT_PTR    = &H26
        '    VT_FILETIME    = &H40
        '    VT_BLOB        = &H41
        '    VT_STREAM      = &H42
        '    VT_STORAGE     = &H43
        '    VT_STREAMED_OBJECT   = &H44
        '    VT_STORED_OBJECT     = &H45
        '    VT_BLOB_OBJECT = &H46
        '    VT_CF          = &H47
        '    VT_CLSID       = &H48
        '    VT_VERSIONED_STREAM  = &H49
        '    VT_BSTR_BLOB   = &H0FFF
        '    VT_VECTOR      = &H1000
        '    VT_ARRAY       = &H2000    ' vbArray
        '    VT_BYREF       = &H4000
        '    VT_RESERVED    = &H8000
        '    VT_ILLEGAL     = &HFFFF
        'End Enum
        '
        Dim iTypeCore As Integer
        Dim u As VariantDescriptor
        '
        CopyMemory u, v, 16             ' Get the variant descriptor structure.
        iTypeCore = u.Type And &HFF     ' Strip off any array bit.
        Select Case iTypeCore
        '
        ' Intrinsic VB6 types.
        Case vbInteger:     sVariantDesc = "Integer"
        Case vbLong:        sVariantDesc = "Long"
        Case vbSingle:      sVariantDesc = "Single"
        Case vbDouble:      sVariantDesc = "Double"
        Case vbCurrency:    sVariantDesc = "Currency"
        Case vbDate:        sVariantDesc = "Date"
        Case vbString:      sVariantDesc = "String"
        Case vbBoolean:     sVariantDesc = "Boolean"
        Case vbByte:        sVariantDesc = "Byte"
        Case vbDecimal:     sVariantDesc = "Decimal"            ' Not an intrinsic type.
        Case &H14:          sVariantDesc = "LongLong"           ' Not an intrinsic type; works only when ByVal in Variant.
        '
        ' Special to Variants.
        Case vbEmpty:       sVariantDesc = "Empty"              ' Uninitialized.
        Case vbNull:        sVariantDesc = "Null"               ' Unique to variants (and fields).
        Case vbVariant:     sVariantDesc = "Variant"            ' Used only with arrays of variants.
        '
        ' UDTs (structures).
        ' This isn't allowed in a standard EXE program.
        ' A UDT to be placed into a variant must be declared in a "public object module",
        ' and these aren't allowed in standard EXE programs.
        Case vbUserDefinedType: sVariantDesc = "User Defined Type: " & TypeName(v) ' The TypeName will return the name of the UDT type (not the variable name).
        '
        ' Objects.
        Case vbObject:      sVariantDesc = "Object: " & sVariantObjectDesc(v)
        Case vbError:       sVariantDesc = "Err Object"         ' Doesn't seem to be used.  The Err object is treated just like any other object.
        Case vbDataObject:  sVariantDesc = "IUnknown"
        '
        ' Other non-VB6 types.
        Case Else:  sVariantDesc = "Not VB6 defined: Type #" & Format$(u.Type)
        End Select
        '
        '
        ' Check to see if it's a variant with array (not an array of variants).
        If u.Type And vbArray Then sVariantDesc = sVariantDesc & " (Array)"
        '
        ' Note whether ByRef or ByVal in the Variant.
        If u.Type And &H4000 Then
            sVariantDesc = sVariantDesc & " (ByRef)"
        Else
            sVariantDesc = sVariantDesc & " (ByVal)"
        End If
        '
    End Function
    
    Private Function sVariantObjectDesc(v As Variant) As String
        Dim iTypeCore As Integer
        Dim u As VariantDescriptor
        Dim o As Object
        '
        CopyMemory u, v, 16                     ' Get the variant descriptor structure.
        iTypeCore = u.Type And &HFF             ' Strip off any array bit.
        '
        If iTypeCore <> 9 Then Exit Function    ' We only process objects herein.
        '
        Set o = v                               ' Alias the variant's object into a pure object variable.
        sVariantObjectDesc = TypeName(o)
        ' The following works on IDE added controls, run-time controls added to control arrays, and correctly added API controls (such as Krool's).
        If Not o Is Nothing Then If TypeOf o Is Control Then sVariantObjectDesc = sVariantObjectDesc & " (Control)"
        '
    End Function
    
    
    
    Enjoy,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Null

    Maybe nothing can be null, or null is nothing
    no, nothing is basically the content of an empty object (any object type variable with nothing assigned to it)

    May be a null is not equal to another null.
    Code:
    ?null = null
    Null
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Null

    Unlike other languages Null exists in VB to handle the special case of database nulls. Naturally it behaves similar in SQL expressions. (depending on the DBMS WHEN Field=Null always evaluates to Null)

    Other languages use Null more like a 0 valued pointer. The equivalent in VB would be vbNullString and Nothing.

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Null

    Maybe a good analogy is to think of Null as a stellar black hole. Once something falls into it, it never comes out again. Even a comparison to Empty results in a Null. In the Immediate Windows, when you type "? TypeName(Null = Empty)", you still get "Null".

    EDIT1: Ahhh yes, and vbNullString has nothing to do with a Null. They'd have done better to name the constant vbEmptyString, but we have what we have.

    EDIT2: Truth be told, even though I understand them, I'm not crazy about nulls. I have functions like the following I wrap all my database fields in just to protect myself from them in case they get into my database:

    Code:
    
    Public Function TextVal(fld As DAO.Field) As String
        ' Returns the data from a text field, or vbNullString if the field is Null.
        ' This function also trims any spaces.
        If IsNull(fld) Then
            TextVal = vbNullString
        Else
            TextVal = Trim$(fld)
        End If
    End Function
    
    
    I know that they come in handy for some folks. I'm not sure why, but I've just never liked them.
    Last edited by Elroy; Feb 9th, 2018 at 08:30 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  12. #12
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Null

    Quote Originally Posted by Elroy View Post
    Maybe a good analogy is to think of Null as a stellar black hole. Once something falls into it, it never comes out again. Even a comparison to Empty results in a Null. In the Immediate Windows, when you type "? TypeName(Null = Empty)", you still get "Null".

    EDIT1: Ahhh yes, and vbNullString has nothing to do with a Null. They'd have done better to name the constant vbEmptyString, but we have what we have.
    or maybe vbNothingBurger

  13. #13
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,253

    Re: Null

    Quote Originally Posted by Elroy View Post
    EDIT2: Truth be told, even though I understand them, I'm not crazy about nulls. I have functions like the following I wrap all my database fields in just to protect myself from them in case they get into my database:

    I know that they come in handy for some folks. I'm not sure why, but I've just never liked them.
    It's not really about whether they 'get into your database'. For non-mandatory relationships between tables, there will always be NULLS returned when you outer join those tables. So, for most database designs, there's really no avoiding them - nothing to do with them being 'handy'...

    NULL is the absence of a value and, therefore, nothing can be equal or inequal to it.
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  14. #14
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,253

    Re: Null

    btw, Elroy, surely it would be better (and more performant) to use the DBMS's ISNULL (or equivalent) function than handling the NULLS after you've fetched the data?
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  15. #15
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Null

    Hi Colin,

    Hmmm, well, I prefer to let MSAccess (which is what this database is) just be as dumb as possible, handling any/all validations in VB6. It's just the philosophy I've used to develop my primary project. I won't argue that there are other "philosophies" but that's just been mine. And, truth be told, I've never really noticed any performance issues regarding that stuff. My biggest performance hits always come from Word and Excel automation, but that's another topic. It's as if the entire remainder of my project is ... erm ... instantaneous (at least from the user's perspective). And I'm not one to fret over a process taking 200 microseconds versus 50 microseconds.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  16. #16
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,253

    Re: Null

    Quote Originally Posted by Elroy View Post
    Hi Colin,

    Hmmm, well, I prefer to let MSAccess...just be as dumb as possible
    Well, it certainly won't struggle with that
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width