|
楼主 |
发表于 2016-6-14 19:09
|
显示全部楼层
我的解决问题之后的代码在下面,也就是把Set Conn = CreateObject("ADODB.connection"),Set rs = CreateObject("ADODB.Recordset") 把这两句放在For Each……循环里面。我认为是这样,代码开始执行时候前面两句Set rs、Set Con执行了,当第一个SQL执行完后,Conn.Quit ,Set Conn = Nothing 这两句关闭了数据库,第二次循环执行的时候从FOR each……开始执行,没有执行Set rs、Set Con,相当于数据没有再次连接上。 不知道这么理解对不对!
Private Sub 更新数据_Click()
Dim Conn As Object, rs As Object
Dim ws As Worksheet
Dim gzbm As String
Dim strConn As String
Dim sql As String
On Error Resume Next
strConn = "Provider=MSDAORA.1; user id="
strConn = strConn & txt用户名.Value
strConn = strConn & "; password=" & txt密码.Value
strConn = strConn & "; data source = " & txt数据库.Value
strConn = strConn & "; Persist Security Info=True"
For Each ws In Worksheets
If ws.Name <> "增加井号" Then
gzbm = ws.Name
Set Conn = CreateObject("ADODB.connection")
Set rs = CreateObject("ADODB.Recordset")
Conn.Open strConn
sql = "SELECT RQ,SCSJ,FROM SD.DBA01 DBA01 WHERE (MZ='" & gzbm & "') ORDER BY RQ"
rs.Open sql, Conn
ThisWorkbook.Sheets(gzbm).[A3].CopyFromRecordset rs
Conn.Quit
Set Conn = Nothing
End If
Next
End Sub
|
|