Record not found

ilanray

Member
Local time
Today, 05:14
Joined
Jan 3, 2023
Messages
126
Hi
I have a code that search on a table a record,
Diff:
set rs=db.OpenRecordset("select fname from employee where id = 5" )
and i would like to use the if statement:
if rs("fname") = "cccc" then....else....

the problem is since my table doesn't have id=5, I get an error message "invalid use of null"
how can I solve it?
 
You need to check the recordset for EOF

Code:
If rs.EOF Then
    ' no record found
Else
    ' record found'
End If
 
Test whether recordset has records before referencing field.
If Not rs.EOF Then

Why is the ID static?

If you just want to return a single value, why not use DLookup()?
 
The id isn't static' this is just an eample
I tried to use the DLOOKUP and I got the same error since the record has not been found
 
Whenever you filter, you can expect that the filtering will be thorough and will not produce any results, meaning you will get an empty recordset.
Therefore, you have to check as a precaution.
Code:
If Not rs.EOF
   if rs("fname") = "cccc" then....else....
End If
'---------------------------------------------------
Dim x As Variant
x = Dlookup(...)
If Not IsNull(x) Then   ' record exists
Addition to the recordset. When a recordset is new opened, the record pointer is automatically set to the first record or to EOF.
With an existing recordset, you don't know this and you have to check for BOF in addition to EOF.
 
OK, so now THREE posts in a row have told you that you need to check for EOF BEFORE using a recordset.
 
Dim x As String
x = Nz(DLookup("fname", "employee", "ID = 5"), "No Name")
 
ilanray,

First, I agree with the others that checking that there are records in the recordset (eg Not EOF) is fundamental.
Second, do you have any evidence that the recordset has any values? And what those values may be?

You could do a recordcount. It just seems that you may be assuming something and you just need to confirm your data/recordset values.
 
if rs("fname") = "cccc" then....else....

the problem is since my table doesn't have id=5, I get an error message "invalid use of null"

It appears that the others are telling you what to check. I'll take a different approach for the explanation.

If the ID in question doesn't exist in your table - and you say it doesn't, the "current record" for your recordset is null. Therefore, rs("fname") is returned as null, which usually puts a crimp in further activity because of how Access treats nulls. However, this is actually correct behavior for Access and OpenRecordset in the absence of methods to prevent returning those nulls.

Now... what the others are telling you (and they ARE right) is that your recordset's RS.EOF property will be true if if your stated operation fails. If you test for that EOF condition BEFORE you look at the fname field then you will know when you cannot make the comparison.
 
That makes 5 people telling the OP that before you can use the recordset, you need to know if a record exists. I wonder when he will try it.
 
That makes 5 people telling the OP that before you can use the recordset, you need to know if a record exists. I wonder when he will try it.
You can lead a horse to water...... :)
 

Users who are viewing this thread

Back
Top Bottom