|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 Yaojiaqing 于 2013-1-25 08:26 编辑
使用VBA连接SQL数据库获取数据时,在SQL语句中涉及创建及查询临时表的过程,结果均为:
运行时错误'3704':
对象关闭时,不允许操作。
请问该如何解决?万分感谢!!
代码如下
[code=vb]Private Sub Yao()
Dim i%, strCn$, strSQL$, serIP$, uid$, pwd$, dbName$, mydate, sht As Worksheet
Dim cn As Object
Dim rs As Object
Dim stime As Date, etime As Date
stime = Timer
serIP = "10.1.1.1"
uid = "sa"
pwd = "123"
dbName = "sa"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.RecordSet")
strCn = "Provider=sqloledb;Server=" & serIP & ";Database=" & dbName & ";Uid=" & uid & ";Pwd=" & pwd & "; "
mydate = Date
strSQL = "if object_id('tempdb..#shops') is not null drop table #shops; select b.sno,b.areaid,b.areaname,a.shopid,a.sname into #shops from areashops a,areadept b where"
strSQL = strSQL + "a.arealevel=b.arealevel select * from #shops"
cn.Open strCn
cn.CommandTimeout = 720
rs.Open strSQL, cn
Set sht = ThisWorkbook.Sheets("Sheet1")
sht.[a2:i50000].ClearContents
sht.[a2:i50000].NumberFormatLocal = "@"
sht.[a2].CopyFromRecordset cn.Execute(strSQL)
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
[/code] |
|