Solved

Error 94 invalid use of null

Posted on 2001-07-26
7
667 Views
Last Modified: 2008-03-03
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
Comment
Question by:DonBartholomew
7 Comments
 
LVL 2

Expert Comment

by:nahumd
ID: 6321372
Try to combine an empty string to the returned result.

e.g.

rs.fields("xx") & ""
0
 
LVL 3

Accepted Solution

by:
nigelrowe earned 50 total points
ID: 6321398
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
 
LVL 7

Expert Comment

by:kamall
ID: 6321400
The correct way to handle nulls is:

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

Regards
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:DonBartholomew
ID: 6321459
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
 
LVL 1

Author Comment

by:DonBartholomew
ID: 6321472
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
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6321482
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
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6321558
Cheers Don
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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

15 Experts available now in Live!

Get 1:1 Help Now