|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
从资源和效率方面,应当把
Set Conn = CreateObject("ADODB.connection")
Set rs = CreateObject("ADODB.Recordset")
Conn.Open
放在For..next 外面
从数据连接方面,我们是先连接到数据库源,再去打开我们需要的数据集,这循环中,原则上我们只要连接数据库源一次就行了,所以不必要在For 。。。next里如下进行个更改:
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"
Set Conn = CreateObject("ADODB.connection")
Set rs = CreateObject("ADODB.Recordset")
Conn.Open strConn
For Each ws In Worksheets
If ws.Name <> "增加井号" Then
gzbm = ws.Name
sql = "SELECT RQ,SCSJ,FROM SD.DBA01 DBA01 WHERE (MZ='" & gzbm & "') ORDER BY RQ"
rs.Open sql, Conn
ThisWorkbook.Sheets(gzbm).[A3].CopyFromRecordset rs
rs.close '记得用完了,要关闭
End If
Next
Conn.close 'ADO的Connection,没有Quit的用法,而是用Close来关闭,后面用 Set 对象=Nothing 来释放资源
Set rs = Nothing
Set Conn = Nothing
End Sub
|
|