Solved

Error 94 invalid use of null

Posted on 2001-07-26
7
678 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 123
Passing a Text Box name to a Sub 6 102
Problem to With line 4 76
fso.FolderExists("\\server\HiddenFolder$") 4 89
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

730 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