打开Connection和Recordset对象以使用SQL进行工作表到工作表的数据移动

时间:2015-06-16 18:57:18

标签: excel-vba connection adodb recordset vba

我正在尝试打开连接 Recordset ,以使用SQL在打开的工作簿中的工作表之间移动数据。要清楚所有数据源表都在当前工作簿中打开,我试图从同一个打开的工作簿中的数据中复制数据。

我使用不同输入的代码将封闭工作簿中的数据复制到当前工作簿中而不会出错。

我得到的错误是

  

“[Microsoft] [ODBC设备管理器]未找到数据源名称且没有   默认驱动程序指定“

此处发生错误:

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"

环境 Windows 7 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$3] & ;"

 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

1 个答案:

答案 0 :(得分:0)

JToggleButton#isSelected中故意注释掉“strConnect”参数取消注释该参数和事情将会有效吗

编辑:import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JToggleButton; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class JavaApplication63 { public static void main(String[] args) { new JavaApplication63(); } public JavaApplication63() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private JTextField height; private JToggleButton cmBtn; private JToggleButton feetBtn; public TestPane() { setLayout(new GridBagLayout()); height = new JTextField(10); cmBtn = new JToggleButton("CM"); feetBtn = new JToggleButton("Ft"); ButtonGroup bg = new ButtonGroup(); bg.add(cmBtn); bg.add(feetBtn); feetBtn.setSelected(true); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.insets = new Insets(2, 2, 2, 2); add(height, gbc); gbc.gridx++; add(cmBtn, gbc); gbc.gridx++; add(feetBtn, gbc); gbc.gridx = 0; gbc.gridy++; gbc.gridwidth = GridBagConstraints.REMAINDER; JButton calc = new JButton("Calculate"); calc.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { double valueInCms = 0; if (cmBtn.isSelected()) { valueInCms = Double.parseDouble(height.getText()); } else if (feetBtn.isSelected()) { String text = height.getText().replace("'", ".").replace("\"", ""); double feet = Double.parseDouble(text); valueInCms = feet * 30.48; } System.out.println("valueInCms = " + valueInCms); } }); add(calc, gbc); } } } 也是错误的。它应该是objConnection.Open 'strConnect($符号在错误的位置,并且那里也有一个迷路和符号)