Test for records found

S

Samantha

I'm querying using code but need the right code to test whether there were
any records found as a result. Anyone has any pointers? The query is based
on a textbox txtPartNumber.
here's my code:

Dim d As Database
Dim rsClass As Recordset
Dim strSQL As String
Dim strInput As String
Dim strClass As String

Set d = CurrentDb
strInput = Me!txtPartNumber
strSQL = "SELECT dbo_PartMaster.PartNumber, dbo_PartMaster.ProductClass FROM
dbo_PartMaster WHERE"
strSQL = strSQL & "[dbo_PartMaster.PartNumber] = '" & strInput & "'"
Set rsClass = d.OpenRecordset(strSQL, DB_OPEN_DYNASET)
strClass = rsClass("ProductClass")
Me!cboClass.Value = strClass
rsClass.Close
d.Close

The error is at the line: strClass=rsClass("ProductClass") when there is no
record found. What's the code to find out when there is no record found?
I tried the following, but it doesn't work:
if isnull( rsClass("ProductClass") ) then
'do nothing
end if

Please help!!!! thank you fo so much.
Samantha
 
S

Samantha

Yes, it is working! Thank you so much Allen! This is a tremendous help.
Again thank you. You've been great!

Samantha

Allen Browne said:
If rsClasss.RecordCount > 0 Then
...

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Samantha said:
I'm querying using code but need the right code to test whether there were
any records found as a result. Anyone has any pointers? The query is
based
on a textbox txtPartNumber.
here's my code:

Dim d As Database
Dim rsClass As Recordset
Dim strSQL As String
Dim strInput As String
Dim strClass As String

Set d = CurrentDb
strInput = Me!txtPartNumber
strSQL = "SELECT dbo_PartMaster.PartNumber, dbo_PartMaster.ProductClass
FROM
dbo_PartMaster WHERE"
strSQL = strSQL & "[dbo_PartMaster.PartNumber] = '" & strInput & "'"
Set rsClass = d.OpenRecordset(strSQL, DB_OPEN_DYNASET)
strClass = rsClass("ProductClass")
Me!cboClass.Value = strClass
rsClass.Close
d.Close

The error is at the line: strClass=rsClass("ProductClass") when there is
no
record found. What's the code to find out when there is no record found?
I tried the following, but it doesn't work:
if isnull( rsClass("ProductClass") ) then
'do nothing
end if

Please help!!!! thank you fo so much.
Samantha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top