Solved

How is this handling a null....and how can I fix it?

Posted on 2004-08-16
3
207 Views
Last Modified: 2010-04-07
I am borrowing this function that was created in VBscript/ASP

Private Function MkPhoneNum(byVal number)
      Dim tmp
      number = CStr( number )
      number = Trim( number )
      number = Replace( number, " ", "" )      
      number = Replace( number, "-", "" )
      number = Replace( number, "(", "" )
      number = Replace( number, ")", "" )
      Select Case Len( number )
            Case 7
                  tmp = tmp & Mid( number, 1, 3 ) & "-"
                  tmp = tmp & Mid( number, 4, 4 )
            Case 10
                  tmp = tmp & "(" & Mid( number, 1, 3 ) & ") "
                  tmp = tmp & Mid( number, 4, 3 ) & "-"
                  tmp = tmp & Mid( number, 7, 4 )
            Case 11
                  tmp = tmp & Mid( number, 1, 1 ) & " "
                  tmp = tmp & "(" & Mid( number, 2, 3 ) & ") "
                  tmp = tmp & Mid( number, 5, 3 ) & "-"
                  tmp = tmp & Mid( number, 8, 4 )
            Case Else
                  MkPhoneNum = Null
                  Exit Function
      End Select
      MkPhoneNum = tmp
End Function


However, when I pass a null field through it, it results in the following error:
      

Microsoft VBScript runtime error '800a005e'

Invalid use of Null: 'CStr'

on line 6...which is the line where it defines
number = CStr( number )

any suggestions for how to fix this to just leave null cases blank????

Thanks!
0
Comment
Question by:htillberg
3 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 11812038
Use IsDbNull to find if the value that is passed is null:

Example:
Dim MyVar As Object
Dim MyCheck As Boolean
MyCheck = IsDBNull(MyVar)   ' Returns False.
MyVar = ""
MyCheck = IsDBNull(MyVar)   ' Returns False.
MyVar = System.DBNull.Value
MyCheck = IsDBNull(MyVar)   ' Returns True.
MsgBox(MyCheck)


---------------------------------------------------------
Private Function MkPhoneNum(byVal number)
    if IsDbNull(number) then
       throw ArgumentNullException("number","The parameter number cannot be null")
    else

     Dim tmp
     number = CStr( number )
     number = Trim( number )
     number = Replace( number, " ", "" )    
     number = Replace( number, "-", "" )
     number = Replace( number, "(", "" )
     number = Replace( number, ")", "" )
     Select Case Len( number )
          Case 7
               tmp = tmp & Mid( number, 1, 3 ) & "-"
               tmp = tmp & Mid( number, 4, 4 )
          Case 10
               tmp = tmp & "(" & Mid( number, 1, 3 ) & ") "
               tmp = tmp & Mid( number, 4, 3 ) & "-"
               tmp = tmp & Mid( number, 7, 4 )
          Case 11
               tmp = tmp & Mid( number, 1, 1 ) & " "
               tmp = tmp & "(" & Mid( number, 2, 3 ) & ") "
               tmp = tmp & Mid( number, 5, 3 ) & "-"
               tmp = tmp & Mid( number, 8, 4 )
          Case Else
               MkPhoneNum = Null
               Exit Function
     End Select
     MkPhoneNum = tmp
 end if
End Function

HTH, nauman
0
 

Accepted Solution

by:
tdkh earned 250 total points
ID: 11815543
I would just change the syntax of line 6 to the following...

number = "" & number

The result of a concatination operation is a string.  This should work if number has a value or is null.
0
 

Author Comment

by:htillberg
ID: 11821414
Nice suggestions. The new code by nauman still kept returning the same error though. However, modifying the number line did the trick. Thanks so much!@!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now