[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Error 94 invalid use of null

Posted on 2001-07-26
7
Medium Priority
?
687 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
[X]
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
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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

649 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