MS Access continue execution of loop when no return from SQL

Posted on 2012-09-15
Medium Priority
Last Modified: 2012-09-27

I have this bit of code:
Set rst3 = dbs.OpenRecordset("SELECT Customer.[ListID], Customer.[Name], Customer.[FullName] " _
& "FROM Customer " _
& "WHERE (((Customer.[Name])='" & student_name & "'));")
sCustomerListID = rst3!ListID

Sometimes though one some records nothing is returned and the program stops execution. How do I continue execution eventhough nothing is returned from the code above?

Question by:Victor Kimura
1 Comment
LVL 61

Accepted Solution

mbizup earned 2000 total points
ID: 38401554
Check the recordcount prior to doing anything that needs values from your recordset:

Set rst3 = dbs.OpenRecordset("SELECT Customer.[ListID], Customer.[Name], Customer.[FullName] " _
& "FROM Customer " _
& "WHERE (((Customer.[Name])='" & student_name & "'));")

if rs.Recordcount = 0 then
     msgbox "No records"
     Exit sub                ' or do whatever else is needed
       sCustomerListID = NZ( rst3!ListID )     '<----- Also change this line as shown to handle nulls
End if 

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

840 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