Results 1 to 9 of 9

Thread: Passing nulls to a function...How?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Passing nulls to a function...How?

    I have an app that pulls data from a database. The data is then passed to a function that manipulates it and returns it as string. The returns from the database could be just about any date type and Nulls are possible. The problem is the code that calls the function can’t be changed to check for nulls. I have set up the function to accept a string as a parameter. This works for everything except nulls. Short of using a variant as the parameter, is there any other way to deal with these nulls?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Why not check for Null before calling the function, then replace it with ""? If you can't alter the function, I don't see what other choice you have. Of course, you could replace it with something other than an empty string, but you pretty much have to replace it with something.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    The data that is being pulled is coming from a compiled dll. I can make it work passing it to a variant and then deal with nulls from there. I was just hoping for something better.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Sorry, I realize that I misread your post. You were clear, and I was confused.

    I expect that you're hosed on this. Since a Null is a typeless thing, I doubt that you can use anything but a variant. If it was a number, that would be easier, but I believe it is defined as typeless.

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    str = rec.Fields("whatever")
    If IsNull(str) Then str = ""

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Yeah, that was my original idea. However, if you re-read his posts, you will realize that he never gets that option.

  7. #7
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    StrPtr would return 0 on a null string:

    Private Sub Command1_Click()
    SomeSub vbNullString
    End Sub
    Sub SomeSub (b As String)
    MsgBox StrPtr(b) 'returns 0
    End Sub
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Originally posted by BuggyProgrammer
    StrPtr would return 0 on a null string:

    Private Sub Command1_Click()
    SomeSub vbNullString
    End Sub
    Sub SomeSub (b As String)
    MsgBox StrPtr(b) 'returns 0
    End Sub
    Unfortunately the db returns Null instead of vbNullString. With the code below you see that you never get out of the call procedure.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     MsgBox SomeSub(Null)
    5. End Sub
    6.  
    7. Private Function SomeSub(b As String) As String
    8.     MsgBox StrPtr(b) 'returns 0
    9. End Function

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Why avoid the variant? It makes it much simpler when having to deal with a source you can't change...
    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox Func(Null)
    3. End Sub
    4.  
    5. Private Function Func(ByVal v As Variant) As String
    6.     Func = v & ""
    7. End Function

    Tada, no more Null.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

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