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
Solved

Error 94 invalid use of null

Posted on 2001-07-26
7
677 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

790 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