• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:

Error 94 invalid use of null

Hi,

I'm new to VB, so this is probably easy. I'm trying out ADO and have a recordset that simply returns a table. I have to access the fields individually, assigning them to a variable or some component's caption/text. Every time I try to access a field value that happens to be null, I bump into error 94: invalid use of null. How can I work around this ?

Note: I'm not allowed to use any data-bound components, everything has to be done hardcoded.

Bart
0
DonBartholomew
Asked:
DonBartholomew
1 Solution
 
nahumdCommented:
Try to combine an empty string to the returned result.

e.g.

rs.fields("xx") & ""
0
 
nigelroweCommented:
There is not much you can do about this if you have Dim'ed the variables where you store the data in accordance with the types of DB table fields. If the field type is string or date, for example...

Dim a_date As Date
Dim a_string As Date
 a_date = Rs.Fields("a_date").Value
 a_string = Rs.Fields("a_string").Value

and any of the DB fields are NULL, then you will get this error.

The only thing you can do is use Variant type variables, except on the id column of the table (this is usually the only table column where you can be certain of its type and that it is not NULL)

I use this method and a couple of functions to decode the data into the variables when I get them from the DB...

Public Function SQLDecode(ByVal x As Variant) As Variant
On Error GoTo ErrorHandler
    Dim vntPosition As Variant
    Dim strx As String
    If IsNull(x) Then
        SQLDecode = ""
    Else
        Select Case TypeName(x)
            Case "String"
                SQLDecode = Trim(x)
            'Case "Date"
            '    SQLDecode = CStr(x)
            Case Else
                SQLDecode = x
        End Select
    End If
    Exit Function
ErrorHandler:
    SQLDecode = ""
    Err.Raise Err.Number, Err.Source, RTErr.Description(TypeName(Me), "SQLDecode", Err.Description)
End Function

I do this each time...

 a_date = SQLDecode(Rs.Fields("a_date").Value)
 a_string = SQLDecode(Rs.Fields("a_string").Value)

Thus, if there is a NULL, the variable would contain "", an empty string.

Inversely, to be independant of the type of fields in an update or insert situation this function automatically formats the data for the sql clause...

Public Function SQLEncode(ByVal x As Variant) As String
On Error GoTo ErrorHandler
Dim vntPosition As Variant
Dim strx As String
    If IsNull(x) Then
        SQLEncode = "Null"
    Else
        Select Case TypeName(x)
            Case "String"
                If x = "" Then
                    SQLEncode = "Null"
                Else
                    strx = x
                    vntPosition = InStr(strx, "'")
                    Do While vntPosition <> 0
                        strx = Mid(strx, 1, vntPosition - 1) & "''" & Mid(strx, vntPosition + 1)
                        vntPosition = InStr(vntPosition + 2, strx, "'")
                    Loop
                    SQLEncode = "'" & strx & "'"
                End If
            Case "Single"
                SQLEncode = Trim(Str(x))
            Case "Double"
                SQLEncode = Trim(Str(x))
            Case "Decimal"
                SQLEncode = Trim(Str(x))
            Case "Date"
                SQLEncode = "'" & x & "'"
            Case "Boolean"
                If x = True Then
                    SQLEncode = "True"
                Else
                    SQLEncode = "False"
                End If
            Case "Integer"
                SQLEncode = Trim(Str(x))
            Case "Long"
                SQLEncode = Trim(Str(x))
        End Select
    End If
    Exit Function
ErrorHandler:
    SQLEncode = ""
    Err.Raise Err.Number, Err.Source, RTErr.Description(TypeName(Me), "SQLEncode", Err.Description)
End Function

then

 sqlclause = "UPDATE a_table SET a_date = " & SQLEncode(a_date) & " WHERE idfield = " & SQLEncode(idfield)

If this is not clear, don't hesitate to post
0
 
kamallCommented:
The correct way to handle nulls is:

If Not IsNull(MyRs("xxx")) Then Lable1=MyRs("xxx")

Regards
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DonBartholomewAuthor Commented:
Thank u guyz, that will help for sure. One more question for Nigel: when updating a table, does that SQLencode func guarantee that a NULL field remains NULL, or is it replaced by a string "Null" or an empty field ?
0
 
DonBartholomewAuthor Commented:
Thank u guyz, that will help for sure. One more question for Nigel: when updating a table, does that SQLencode func guarantee that a NULL field remains NULL, or is it replaced by a string "Null" or an empty field ?
0
 
nigelroweCommented:
it would do something like...
Dim a_date As Variant
 a_date = Null
sqlclause = "UPDATE a_table SET a_date = " & SQLEncode(a_date) & " WHERE id = ...."

would give...

"UPDATE a_table SET a_date = Null WHERE id = ...."

You could use kamall's thing of course, but you will have to test every field, adding a lot of coding.

Using my method, you are independant of the field types.
0
 
nigelroweCommented:
Cheers Don
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now