各位大神幫忙看看,好奇怪,我用VBA操作sqlserver資料庫其他都沒有變化,兩個陳述句用sql都是能查的,但是放到vba中,一個正常,一個報錯
報錯
Public Function Create_Report101()
Dim strSql01 As String, strSql02 As String, sql As String, strSql03 As String, strSql04 As String, strSql05 As String, strSql06 As String, strSql07 As String, strSql08 As String, strSql09 As String, strSq201 As String
Set cn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
Workbooks("奮戰120天中金所成交量.xlsx").Activate
Worksheets("主界面").Activate
StartDate = Range("i2").Value
EndDate = Range("j2").Value
strSql01 = "INSERT INTO t_66666_registers" & vbNewLine & _
"SELECT '總權益',SUM(t_flex_cusfund.[期末權益])/10000000"
strSql02 = "FROM t_flex_cusfund" & vbNewLine & _
"WHERE t_flex_cusfund.[交易日] = '20200915'" & vbNewLine & _
"select * from t_66666_registers"
' strSql01 = "SELECT '總計',SUM(t_flex_cusfund.[期末權益])/10000000"
' strSql02 = "FROM t_flex_cusfund" & vbNewLine & _
"WHERE t_flex_cusfund.[交易日] = '20200916'"
sql = strSql01 & strSql02
cn.Open ServerConn
cn.CommandTimeout = 600
rs.Open sql, cn
Workbooks("奮戰120天中金所成交量.xlsx").Activate
Worksheets("每日").Activate
Range("A:aa").Clear
Range("a1").CopyFromRecordset rs
報錯:vba 運行時錯誤3704 物件關閉時,不允許操作
正常
Public Function Create_Report101()
Dim strSql01 As String, strSql02 As String, sql As String, strSql03 As String, strSql04 As String, strSql05 As String, strSql06 As String, strSql07 As String, strSql08 As String, strSql09 As String, strSq201 As String
Set cn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
Workbooks("奮戰120天中金所成交量.xlsx").Activate
Worksheets("主界面").Activate
StartDate = Range("i2").Value
EndDate = Range("j2").Value
' strSql01 = "INSERT INTO t_66666_registers" & vbNewLine & _
"SELECT '總權益',SUM(t_flex_cusfund.[期末權益])/10000000"
' strSql02 = "FROM t_flex_cusfund" & vbNewLine & _
"WHERE t_flex_cusfund.[交易日] = '20200915'" & vbNewLine & _
"select * from t_66666_registers"
strSql01 = "SELECT '總計',SUM(t_flex_cusfund.[期末權益])/10000000"
strSql02 = "FROM t_flex_cusfund" & vbNewLine & _
"WHERE t_flex_cusfund.[交易日] = '20200916'"
sql = strSql01 & strSql02
cn.Open ServerConn
cn.CommandTimeout = 600
rs.Open sql, cn
Workbooks("奮戰120天中金所成交量.xlsx").Activate
Worksheets("每日").Activate
Range("A:aa").Clear
Range("a1").CopyFromRecordset rs
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/65012.html
標籤:VBA
下一篇:求資源
