LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
tod tod is offline
external usenet poster
 
Posts: 114
Default Better way to test for empty Recordset

I have this code:

'code..............
With cm
.ActiveConnection = cn
.CommandText = DesktopReportQuery
.CommandType = adCmdStoredProc
Set pm1 = .CreateParameter(InputField, adInteger,
adParamInput)
.Parameters.Append pm1
.Parameters(InputField) = ReportMonth
Set pm2 = .CreateParameter(InputField, adInteger,
adParamInput)
.Parameters.Append pm2
.Parameters(InputField) = ReportYear
End With

rs.Open cm

Set PC = ThisWorkbook.PivotCaches.Add
(SourceType:=xlExternal)
Set PC.Recordset = rs
Set PT = PTSheet.PivotTables.Add(PivotCache:=PC,
TableDestination:=PTSheet.Range("A9"))

'code...............

It's always worked fine. However, I eventually ran into a
situation where the Recordset is empty. So the pivot table
creation fails. So I decided simply to use an If structure
to first test if the Recordset was empty, like this:

With cm
.ActiveConnection = cn
.CommandText = DesktopReportQuery
.CommandType = adCmdStoredProc
Set pm1 = .CreateParameter(InputField, adInteger,
adParamInput)
.Parameters.Append pm1
.Parameters(InputField) = ReportMonth
Set pm2 = .CreateParameter(InputField, adInteger,
adParamInput)
.Parameters.Append pm2
.Parameters(InputField) = ReportYear
End With

if rs.EOF Then
'code to skip the creation of the pivot table
Else
Set PC = ThisWorkbook.PivotCaches.Add
(SourceType:=xlExternal)
Set PC.Recordset = rs
Set PT = PTSheet.PivotTables.Add(PivotCache:=PC,
TableDestination:=PTSheet.Range("A9"))
End if

It works if rs.EOF is True. But if there IS data in the
Recordset, when it gets to the statement to put the pivot
table in Cell A9, I get this error:

'One or More Accessor Flags were Invalid'

Huh? The only difference is that it has to first test if
rs.EOF is True. How can that cause this error?

Better yet, is there a better way to run my query and then
abort the creation of the pivot table if the recordset is
empty?

tod




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any way to test whether rows are empty or contain the word Pass ? rCube Excel Worksheet Functions 1 December 22nd 09 04:48 PM
IF function to test whether the cell value is equal to empty nest terb Excel Worksheet Functions 1 April 30th 08 06:05 AM
How can I test if a cell is empty? Victor Delta Excel Discussion (Misc queries) 10 August 8th 07 11:22 PM
test expression for empty cell in =SUMIF() fgrose Excel Worksheet Functions 12 July 16th 07 04:52 PM
Test if the range is empty dan Excel Discussion (Misc queries) 6 December 21st 06 03:59 PM


All times are GMT +1. The time now is 02:30 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"