Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code works, but error when no records in DB

Status
Not open for further replies.

sysr

Technical User
Aug 17, 2001
17
CA
The following code works great when there are records in the database, but when there are NO records I get an Exception error that I can't seem to fix. Any help would be greatly appreciated.

10 sqlStringa = "SELECT OrganizationID FROM RegionalContracts WHERE Region=" & "'" & strSelectedRegion & "'"

20 Set ReportRS = Con.Execute (sqlStringa)
(I am retrieving OrganizationID's)


30 WHILE NOT ReportRS.EOF
(For each OrganizationID from line 20, I am retrieving ActiveFiles and ClosedFiles with line 40)


40 sqlStringb = "SELECT * FROM Reporting WHERE OrganizationID =" & ReportRS("OrganizationID")& " AND Year=" & strYear & " AND Quarter=" & strQuarter

50 Set Rst = Con.Execute (sqlStringb)

60 strActiveFiles = strActiveFiles + Rst("ActiveFiles")

70 strClosedFiles = strClosedFiles + Rst("ClosedFiles")
(In 60 and 70, I am getting a sum of ActiveFiles and ClosedFiles)

80 ReportRS.MoveNext

90 WEND

Works great when there are records, but with no records it crashes.
 
Did you try trapping the error? If you do a thing like:

On Error Goto Err_Handler

< Write your Code >

exit sub <or function>
Err-Handler:
msgbox &quot;There were no records&quot;
end sub <or function>

The program won't cash but you won't have any data either.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top