打开 Connection 和 Recordset 对象以使用 SQL 进行 sheet 到 sheet 的数据移动
Open Connection and Recordset objects to use SQL for sheet to sheet data movement
我正在尝试打开 Connection 和 Recordset 以使用 SQL 在打开的工作簿中的工作表之间移动数据。需要明确的是,当前工作簿中的所有数据源工作表都处于打开状态,我正在尝试将数据从同一打开的工作簿中的不同工作表复制到其他工作表。
我已将此代码与不同的输入一起使用,将数据从已关闭的工作簿复制到当前工作簿,没有错误。
我得到的错误是
"[Microsoft][ODBC Device Manager] Data source name not found and no
default driver specified"
.
这里出现错误:
objRecordSet.Open strSQL, objConnection, 0, 1, 1
调试器说:
objConnection.Open is <Expression not defined in context>
objConnection.Open : <Expression not defined in context> : Empty : UserForm1.cbPrepareUpload_Click
objConnection : "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=F:\Temp04\Test.xlsm;Mode=Share Deny None;Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OL"
环境是Windows7 64位,Office 2010,32位。
我一整天都在为这件事鼓动灰色的小细胞。我试图将其归结为基础知识,但现在我被困住了。
任何想法都会有所帮助。谢谢,CraigM
Here is the code.
======================================================
Private Sub cbPrepareUpload_Click()
Dim HaveHeader As Boolean
Dim UseHeaderRow As Boolean
Dim i As Long
Dim RowToTest As Long
Dim mySheet As String
Dim shName As String
Dim sh As Worksheet
Dim strConnect As String
Dim strSourceRange As String
Dim strSource As String
Dim strSourceFile As String
Dim strSourceSheet As String
Dim strSQL As String
Dim strTarget As String
Dim objConnection As ADODB.Connection
Dim objRecordSet As ADODB.Recordset
Dim wksName As Worksheet
Set objConnection = New ADODB.Connection
Set objRecordSet = New ADODB.Recordset
strSourceFile = "F:\Temp04\Pricing.xlsx"
strSourceSheet = "Pricing"
strSQL = "SELECT * FROM [Sheet] & ;"
HaveHeader = True
UseHeaderRow = True
strSource = "Pricing"
strTarget = "BF_Upload"
For Each wksName In Sheets
If wksName.Name = strTarget Or wksName.Name Like strTarget & "*" Then i = i + 1
Next
If i = 0 Then
Else
Worksheets(strTarget).Activate
ActiveSheet.Name = strTarget & "-" & (i + 1)
End If
Worksheets.Add(Before:=Worksheets(Worksheets.Count)).Name = strTarget
ActiveSheet.Name = strTarget
Sheets(strTarget).Move Before:=Sheets(1)
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"";"
objConnection.Open strConnect
objRecordSet.Open strSQL, objConnection, 0, 1, 1
Sheets(strSource).Cells(2, 1).CopyFromRecordset objRecordSet
objRecordSet.Close
objConnection.Close
Worksheets(strTarget).Activate
End Sub
"strConnect" 参数是否在 objConnection.Open 'strConnect
中有意注释掉了?取消注释该参数,希望一切正常
编辑:strSQL = "SELECT * FROM [Sheet] & ;"
也是错误的。它应该是 strSQL = "SELECT * FROM [Sheet3$];"
($ 符号在错误的位置并且那里也有一个杂散的 & 符号)
我正在尝试打开 Connection 和 Recordset 以使用 SQL 在打开的工作簿中的工作表之间移动数据。需要明确的是,当前工作簿中的所有数据源工作表都处于打开状态,我正在尝试将数据从同一打开的工作簿中的不同工作表复制到其他工作表。
我已将此代码与不同的输入一起使用,将数据从已关闭的工作簿复制到当前工作簿,没有错误。
我得到的错误是
"[Microsoft][ODBC Device Manager] Data source name not found and no default driver specified"
.
这里出现错误:
objRecordSet.Open strSQL, objConnection, 0, 1, 1
调试器说:
objConnection.Open is <Expression not defined in context>
objConnection.Open : <Expression not defined in context> : Empty : UserForm1.cbPrepareUpload_Click
objConnection : "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=F:\Temp04\Test.xlsm;Mode=Share Deny None;Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OL"
环境是Windows7 64位,Office 2010,32位。
我一整天都在为这件事鼓动灰色的小细胞。我试图将其归结为基础知识,但现在我被困住了。
任何想法都会有所帮助。谢谢,CraigM
Here is the code.
======================================================
Private Sub cbPrepareUpload_Click()
Dim HaveHeader As Boolean
Dim UseHeaderRow As Boolean
Dim i As Long
Dim RowToTest As Long
Dim mySheet As String
Dim shName As String
Dim sh As Worksheet
Dim strConnect As String
Dim strSourceRange As String
Dim strSource As String
Dim strSourceFile As String
Dim strSourceSheet As String
Dim strSQL As String
Dim strTarget As String
Dim objConnection As ADODB.Connection
Dim objRecordSet As ADODB.Recordset
Dim wksName As Worksheet
Set objConnection = New ADODB.Connection
Set objRecordSet = New ADODB.Recordset
strSourceFile = "F:\Temp04\Pricing.xlsx"
strSourceSheet = "Pricing"
strSQL = "SELECT * FROM [Sheet] & ;"
HaveHeader = True
UseHeaderRow = True
strSource = "Pricing"
strTarget = "BF_Upload"
For Each wksName In Sheets
If wksName.Name = strTarget Or wksName.Name Like strTarget & "*" Then i = i + 1
Next
If i = 0 Then
Else
Worksheets(strTarget).Activate
ActiveSheet.Name = strTarget & "-" & (i + 1)
End If
Worksheets.Add(Before:=Worksheets(Worksheets.Count)).Name = strTarget
ActiveSheet.Name = strTarget
Sheets(strTarget).Move Before:=Sheets(1)
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"";"
objConnection.Open strConnect
objRecordSet.Open strSQL, objConnection, 0, 1, 1
Sheets(strSource).Cells(2, 1).CopyFromRecordset objRecordSet
objRecordSet.Close
objConnection.Close
Worksheets(strTarget).Activate
End Sub
"strConnect" 参数是否在 objConnection.Open 'strConnect
中有意注释掉了?取消注释该参数,希望一切正常
编辑:strSQL = "SELECT * FROM [Sheet] & ;"
也是错误的。它应该是 strSQL = "SELECT * FROM [Sheet3$];"
($ 符号在错误的位置并且那里也有一个杂散的 & 符号)