在VBScript中从Excel工作表运行多个查询

时间:2017-02-02 15:00:40

标签: sql excel vbscript odbc sybase-ase

Hello Scripters来自全国各地,我正在开发一个VBScript来从Excel工作表中提取Query参数,并在excel工作表中的每一行的SQL服务器(Sybase ASE)上运行查询

目前我有成功提取参数并运行第一行的代码,但是当我开始下一行时,查询因“缺少对象”而失败,但我已经尝试保持ADO连接打开并重新运行查询记录集,以及为Excel工作表中的每一行重新创建连接并运行记录集。我的代码目前如下:

Dim MyConnection, MyRecordSet, MyExcel, MyExcelBook, MyExcelSheet
Dim ConnectionString, Parameter1, Parameter2, SQL, I
ConnectionString = "Driver=ODBC Driver;UserID=Admin;Password=Pass;"

Set MyExcel = CreateObject("Excel.Application")
Set MyExcelBook = MyExcel.Workbooks.Open("C:/Test.xls")
Set MyExcelSheet = MyExcelBook.WorkSheets(2)
Set MyConnection = CreateObject("ADODB.Connection")
Set MyRecordSet = CreateObject("ADODB.RecordSet")
MyConnection.Open ConnectionString
I = 2

Do while MyExcelSheet.Cells(I,2).Value <> ""
    Parameter1 = MyExcelSheet.Cells(I,6).Value
    Parameter2 = MyExcelSheet.Cells(I,7).Value
    SQL = "Select * From Table Where Type=" & Parameter1 & " AND Color=" & Parameter2 & ";"
    MyRecordSet.Open SQL, MyConnection
    'Save RecordSet to another Excel sheet'
    MyRecordSet.Close
    I = I + 1
loop

MyConnection.Close
Set MyConnection = Nothing

有没有办法在与数据库相同的连接上运行另一个查询而不进入

  

需要对象

错误?

suggested changes之后的修改,仍有相同的错误。

Dim MyConnection, MyExcel, MyExcelBook, MyExcelSheet
Dim ConnectionString, Parameter1, Parameter2, SQL, I
ConnectionString = "Driver=ODBC Driver;UserID=Admin;Password=Pass;"

Set MyExcel = CreateObject("Excel.Application")
Set MyExcelBook = MyExcel.Workbooks.Open("C:/Test.xls")
Set MyExcelSheet = MyExcelBook.WorkSheets(2)
Set MyConnection = CreateObject("ADODB.Connection")
MyConnection.Open ConnectionString
I = 2

Do while MyExcelSheet.Cells(I,2).Value <> ""
    Parameter1 = MyExcelSheet.Cells(I,6).Value
    Parameter2 = MyExcelSheet.Cells(I,7).Value
    SQL = "Select * From Table Where Type=" & Parameter1 & " AND Color=" & Parameter2 & ";"
    Dim MyRecordSet
    Set MyRecordSet = CreateObject("ADODB.RecordSet")
    MyRecordSet.Open SQL, MyConnection
    'Save RecordSet to another Excel sheet'
    MyRecordSet.Close
    I = I + 1
loop

MyConnection.Close
Set MyConnection = Nothing

2 个答案:

答案 0 :(得分:1)

因此,经过对不同想法的大量测试,我终于得到了代码。当MyRecordSet被关闭时,问题就出现了。状态返回到0,当您尝试重新打开MyRecordSet时,它将因对象损坏而失败。重新连接然后重新打开MyRecordSet导致相同的错误。所以这是完成的代码编辑工作。

Dim MyConnection, MyRecordSet, MyExcel, MyExcelBook, MyExcelSheet
Dim ConnectionString, Parameter1, Parameter2, SQL, I
ConnectionString = "Driver=Adaptive Server Enterprise;UserID=Admin;Password=Pass;Pooling=False"

Set MyExcel = CreateObject("Excel.Application")
Set MyExcelBook = MyExcel.Workbooks.Open("C:/Test.xls")
Set MyExcelSheet = MyExcelBook.WorkSheets(2)
I = 2

Do while MyExcelSheet.Cells(I,2).Value <> ""
    Parameter1 = MyExcelSheet.Cells(I,6).Value
    Parameter2 = MyExcelSheet.Cells(I,7).Value
    SQL = "Select * From Table Where Type=" & Parameter1 & " AND Color=" & Parameter2 & ";"
    Set MyConnection = CreateObject("ADODB.Connection")
    MyConnection.Open ConnectionString
    Set MyRecordSet = CreateObject("ADODB.RecordSet")
    MyRecordSet.Open SQL, MyConnection
    'Save RecordSet to another Excel sheet'
    Set MyRecordSet = Nothing
    Set MyConnection = Nothing
    I = I + 1
loop

现在,如果您问我的数据库是否具体,我不知道。但是这段代码适用于使用Adaptive Server Enterprise驱动程序,我确实使用了LankyMart的建议来关闭连接池。我理解只设置&#39; object = nothing&#39;没有关闭连接首先违反标准编码实践,但它的工作原理。感谢那些帮助过的人。

答案 1 :(得分:0)

对于每个查询,都不需要打开和关闭连接对象。试试这种方式:

Dim MyConnection, MyExcel, MyExcelBook, MyExcelSheet
Dim ConnectionString, Parameter1, Parameter2, SQL, I
ConnectionString = "Driver=ODBC Driver;UserID=Admin;Password=Pass;"

Set MyExcel = CreateObject("Excel.Application")
Set MyExcelBook = MyExcel.Workbooks.Open("C:/Test.xls")
Set MyExcelSheet = MyExcelBook.WorkSheets(2)
I = 2

Set MyConnection = CreateObject("ADODB.Connection")
MyConnection.Open ConnectionString
Do while MyExcelSheet.Cells(I,2) <> ""
    Parameter1 = MyExcelSheet.Cells(I,6).Value
    Parameter2 = MyExcelSheet.Cells(I,7).Value
    SQL = "Select * From Table Where Type=" & Parameter1 & " AND Color=" & Parameter2 & ";"
    Dim MyRecordSet
    Set MyRecordSet = CreateObject("ADODB.RecordSet")
    MyRecordSet.Open SQL, MyConnection
    'Save RecordSet to another Excel sheet'
    MyRecordSet.Close
    Set MyRecordSet = Nothing
    I = I + 1
Loop
MyConnection.Close
Set MyConnection = Nothing