|
本帖最后由 xyxcc177 于 2018-9-24 14:45 编辑
你的代码有点问题,查询结束后及时关闭连接
Sub CC1() '任意分数段查询
Set Cn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
PathStr = ThisWorkbook.FullName
Select Case Application.Version * 1 '设置连接字符串,根据版本创建连接
Case Is <= 11
strconn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
Case Is >= 12
strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
End Select
bj = 任意分数段查询.ComboBox1.Value
km = 任意分数段查询.ComboBox2.Value
fs1 = 任意分数段查询.TextBox1.Value
fs2 = 任意分数段查询.TextBox2.Value
If fs1 = "" Then fs1 = 100
If fs2 = "" Then fs2 = 95
Sql = "select Sum (IIf(" & km & " >= " & fs1 & ", 1, 0)), Sum (IIf(" & km & " <= " & fs2 & ", 1, 0)),Sum (IIf(" & km & " > " & fs2 & "and " & km & "<" & fs1 & ", 1, 0)) from [成绩表$a1:p] where 班级=" & bj & " "
Cn.Open strconn
Set rs = Cn.Execute(Sql)
'rs.Open Sql, Cn, 1, 3
任意分数段查询.Label7.Caption = rs.Fields(0).Value
任意分数段查询.Label6.Caption = rs.Fields(1).Value
任意分数段查询.Label11.Caption = rs.Fields(2).Value
rs.Close
Cn.Close
' Sheets("统计").[b2].CopyFromRecordset rs
'任意分数段查询.Label7.Caption = Sheets("统计").[b2]
'任意分数段查询.Label6.Caption = Sheets("统计").[c2]
End Sub
|
|