Results 1 to 12 of 12

Thread: [RESOLVED] write a function to make null field to zero

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    313

    Resolved [RESOLVED] write a function to make null field to zero

    I am using Vb6.
    I want to write a function to replace 'null' to '0'.
    I did this:
    Code:
    Public Function sysNulltoZero(ByVal strText As String) As integer
    
      if strText = Null then
         sysNulltoZero = 0 
      end if
    
    End Function
    Then, I call it like this
    Code:
       SmallCurrencyCharge = Format(sysNulltoZero(rcsCOC("Coc_SmallCurrencyRate")), "000.0000")
    But i got an errormsg:invalid use null.

    Can someone help me out? Thanks a lot.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: write a function to make null field to zero

    Try
    Code:
    if strText = vbNullString then
         sysNulltoZero = 0 
      end if

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    313

    Re: write a function to make null field to zero

    Thank you Hack,
    I tried and got the same errormsg.

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: write a function to make null field to zero

    Microsoft Access has a built-in function -- Nz() -- to convert Nulls; you would do well to mirror it. It accepts an optional replacement value for Nulls, which is useful when you want to de-nullify strings. (Setting them to 0 is suboptimal.) Here's code that mimics the Access function:
    Code:
    Public Function Nz(pvar As Variant, Optional pvarReplace As Variant = 0) As Variant
        If IsNull(pvar) Then
            Nz = pvarReplace
        Else
            Nz = pvar
        End If
    End Function
    Quote Originally Posted by Hack
    Try
    Code:
    if strText = vbNullString then
         sysNulltoZero = 0 
      end if
    Null is its own data type; it is not a string. The proper way to test for Null is:

    If IsNull(MyVariant) Then

    ...or...

    If VarType(MyVariant) = vbNull Then

    It is also worth pointing out that Null is a valid VB keyword, and that it is not equal to vbNull.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: write a function to make null field to zero

    I'm trying to duplicate your code for testing. What is:

    rcsCOC and Coc_SmallCurrencyRate?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    313

    Re: write a function to make null field to zero

    Quote Originally Posted by Hack
    I'm trying to duplicate your code for testing. What is:

    rcsCOC and Coc_SmallCurrencyRate?
    rcsCOC is a recordset and Coc_SmallCurrencyRate is a field name.

    Thanks

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: write a function to make null field to zero

    If you take what is in the field, store it in a variable, and run it through you function, would that work?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    313

    Re: write a function to make null field to zero

    I changed the code to this and it worked.
    Code:
    Public Function sysNulltoZero(ByVal strText As variant) As integer
    
      if IsNull(strText) then
         sysNulltoZero = 0 
      end if
    
    End Function
    Thanks again, Hack.

  9. #9
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: write a function to make null field to zero

    The Format function has that capability built right in. The format argument can contain "Zones". The first zone is for positive values, 2nd for negative, 3rd for zero and 4th for Null.

    SmallCurrencyCharge = Format(sysNulltoZero(rcsCOC("Coc_SmallCurrencyRate")), "000.0000;;;0")

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    313

    Re: write a function to make null field to zero

    Quote Originally Posted by brucevde
    The Format function has that capability built right in. The format argument can contain "Zones". The first zone is for positive values, 2nd for negative, 3rd for zero and 4th for Null.

    SmallCurrencyCharge = Format(sysNulltoZero(rcsCOC("Coc_SmallCurrencyRate")), "000.0000;;;0")

    I didn't know that. I tried this and it did give me the 0 but not formatted :
    Code:
    SmallCurrencyCharge = Format(rcsCOC("Coc_SmallCurrencyRate"), "000.0000;;;0")

  11. #11
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [RESOLVED] write a function to make null field to zero

    I tried this and it did give me the 0 but not formatted :
    Change the format as desired

    Format(rcsCOC("Coc_SmallCurrencyRate"), "000.0000;;;000.0000")

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    313

    Re: [RESOLVED] write a function to make null field to zero

    Quote Originally Posted by brucevde
    Change the format as desired

    Format(rcsCOC("Coc_SmallCurrencyRate"), "000.0000;;;000.0000")

    It worked. I have learned a new thing. THanks Bruce.

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