Null Value Not Recognized (1 Viewer)

TheSearcher

Registered User.
Local time
Today, 07:29
Joined
Jul 21, 2011
Messages
304
The code below returns a null string (as it should) in query design.
However, in my VBA code the vartype for rs("Number_UnitsBilled") is 9 which indicates that it is returning an object. Therefore, the program crashes with a "No current Record" error in the "Else" statement. How can this be?

Code:
sql2 = "SELECT tbl_Billing.Number_UnitsBilled, tbl_Billing.ID "
sql2 = sql2 & "From tbl_Billing "
sql2 = sql2 & "WHERE (tbl_Billing.Client_Id = '" & Client & "') "
sql2 = sql2 & "AND (tbl_Billing.Date_Of_Service = #" & DOS & "#) "
sql2 = sql2 & "AND (tbl_Billing.Payer = 'MLTSS') "
sql2 = sql2 & "AND (tbl_Billing.Therapy = 'Structured Day');"
Set rs2 = db.OpenRecordset(sql2)
    
MsgBox "Vartype: " & VarType(rs2("Number_UnitsBilled"))
'Vartype 9 = Object
    
If IsNull(rs2("Number_UnitsBilled")) = True Then
    SDBilled = 0
Else
    SDBilled = rs2("Number_UnitsBilled")
End If
 

Josef P.

Well-known member
Local time
Today, 13:29
Joined
Feb 2, 2023
Messages
827
IsNull(FieldReference) => false

Code:
If IsNull(rs2("Number_UnitsBilled")) = True Then
vs
If IsNull(rs2("Number_UnitsBilled").Value) = True Then

=>
Code:
if rs2.eof then
     SDBilled = 0
else
     SDBilled = Nz(rs2("Number_UnitsBilled").Value, 0)
end if
 

TheSearcher

Registered User.
Local time
Today, 07:29
Joined
Jul 21, 2011
Messages
304
Nevermind. I just replaced "If IsNull(rs2("Number_UnitsBilled")) = True Then" with "If rs2.recordcount = 0 then"
Now all is good. Dumb mistake.
@Josef P - Thanks for responding.
 

MarkK

bit cruncher
Local time
Today, 04:29
Joined
Mar 17, 2004
Messages
8,181
In boolean math, comparing a value to True has no effect. It's like multiplying by one. This is more succinct code...
Code:
If IsNull(rs2("Number_UnitsBilled").Value) Then
Compare with Josef P.'s test for EOF...
Code:
if rs2.eof then
Recordset.EOF is a boolean property, so no need to compare with True.

Also, to determine the type of a variable, consider using Typename() rather than VarType(). Typename() return the name of the type, so ...
Code:
MsgBox "Type is: " & Typename(rs2("Number_UnitsBilled"))
...would have returned "Field2", and you would have noticed right away that you were testing not the Recordset.Field.Value, but the Recordset.Field object itself.

hth : )
 

Users who are viewing this thread

Top Bottom