vb中執行查詢后,一般要判斷是否為空,只要執行的查詢執行了select,都可以用rs.eof 或者 rs.recordcount來判斷,
但是,如果執行的sql中加了邏輯判斷,導致沒有執行任何select語句,則用rs.eof 或者rs.crcordcount來判斷,系統就會提示
對象關閉時無法操作。
eg1:
delare @a int set @a=1 if @a=0 select @a
說明:如果是執行該腳本,用rs.eof進行判斷時,系統就會報對象關閉錯誤,因為沒有執行任何select查詢,沒有返回任何內容。
eg2:
select * into #itemno3 from seorder a inner join seorderentry b on a.finterid = b.finterid where fbillno = @fbillno --……其它處理的 select * from #itemno3
說明:上面這段如果是執行該腳本,用rs.eof進行判斷時,系統也會報對象關閉錯誤,因為開始沒有執行任何select查詢,沒有返回任何內容。
解決辦法:
1、存儲過程里將最終的結果寫入表中
if exists (select 1 from sysobjects where xtype = 'U' and name = 'itemno3') drop table itemno3 select * into itemno3 from seorder a inner join seorderentry b on a.finterid = b.finterid where fbillno = @fbillno --……其它處理的 --將數據寫入結果表,存儲過程中不做查詢
2、VBA中執行完存儲過程后,再單獨執行查詢結果表
sql = "exec rk_sp_huizong " & FItemID Set rs = ExecSql(sql) sql="select * from [結果表]" set rs= ExecSql(sql) '下面再做判斷就不會提示對象關閉時不允許操作了 If rs.RecordCount > 0 Then end if
