[dba-VB] Passing null values to a function

John W. Colby jwcolby at colbyconsulting.com
Fri Aug 26 08:15:02 CDT 2005

I have designed a class, with the New widget (constructor) having a set of
parameters, with the parameters type declared.

New(ByVal lintPKID As Int32, ByVal lstrForeignName As String, ByVal
lstrLocalName As String, _
                    ByVal lintLen As Int16, ByVal lintPosStart As Int16,
ByVal lintPosStop As Int16, _
                    ByVal lblnActive As Boolean, ByVal lblnCritical As
Boolean, _
                    ByVal lstrForeignDescr As String, ByVal lstrLocalDescr
As String, _
                    ByVal lstrFmt As String, ByVal lblnRightJustified As
Boolean, ByVal lstrNotes As String)

The problem I am running in to is that I am loading this stuff from a table.
I have a class that I initialize, which returns a data reader, and I take
the fields from the reader and pass the values in as follows:

        Dim lclsData As clsData
        Dim lDr As OleDbDataReader
        lclsData = New clsData(lstrDataProvider, lstrDataSrc)   'Get an
instance of clsData
        lclsData.pSQL = "SELECT * FROM " & lstrTblName & " WHERE ACTIVE =
True;"    'Set the SQL statement
        If lclsData.mDrOpen() Then  'Open the reader
            Dim lclsFld As clsFld   'dim a variable to hold clsFld instances
            lDr = lclsData.pDR      'Get the data reader
            While lDr.Read()        'Cycle through the field format records
                'Loading the data from each field format record into the
clsFld instance
                lclsFld = New clsFld(lDr("PKID"), lDr("ForeignFldName"),
lDr("LocalFldName"), _
                                        lDr("Length"), lDr("PosStart"),
lDr("PosStop"), _
                                        lDr("Active"), lDr("Critical"), _
lDr("LocalDescr"), _
lDr("RightJustified"), lDr("Notes"))

The last line is pulling null values out of the "RightJustified" and "Notes"
fields and trying to pass them in.  The program is throwing a runtime error
- "cast from type dbNull to type String not valid"

In this specific case I can go in and "fix" the source data but how do I
handle this if nulls are a valid possibility and I want them to be allowed
to be passed in?  Do I need to change my parameter type to object (or

John W. Colby

Contribute your unused CPU cycles to a good cause:

More information about the dba-VB mailing list