?
Solved

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

Posted on 2004-08-16
3
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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