Solved

Excel VBA How to detect that SQL query returned no data?

Posted on 2011-09-02
1
323 Views
Last Modified: 2012-05-12
I have Excel VBA code that calls a SQL Server 2008 stored proc, and all is fine when the user supplies a valid parameter. How can I tell in VBA when the user supplies a bad account number and there is no data returned from SQL Server to Excel?

I know how to check for EOF on the returned data with ADO, but this project is using OLEDB.

 'Execute stored procedure and return to a query table
    Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("B1"), Sql:=sSql)

    oQt.Refresh     ' it stops right here when there's no data, otherwise it runs fine...

I would like to be able to trap the fact that there's no data, send the user a msg, and exit the sub.

Thanks!
0
Comment
Question by:notawahoo2
1 Comment
 

Accepted Solution

by:
notawahoo2 earned 0 total points
ID: 36474207
Never mind. I just added a teeny query to check the SQL database for the account number, and returned a '0' if it was not found. Since the teeny query will ALWAYS return something, I'm able to handle it on the VBA side.

Thanks anyway!
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

810 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