记录集将writing写入CSV文件而不是查询结果

时间:2017-08-21 17:10:08

标签: ms-access vbscript

我有一个查询访问数据库的VBS文件,并将结果写入.CSV文件。写入.CSV文件的唯一内容是ÿþ

我使用相同的VBS文件和更复杂的查询,它运行正常。两个查询都返回2个相同类型的字段。这两个文件之间唯一的变化是SQL查询。此外,如果我将我尝试使用的查询粘贴到Access中,它会按预期运行查询。

这是VBS文件:

Dim connStr, objConn, getNames, objFSO, rs
Const ForWriting = 2

'Make and open progress window
On Error Resume Next

Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 200
objExplorer.Visible = 1
objExplorer.Silent = 1
objExplorer.Document.Title = "Script in progress"
objExplorer.Document.Body.InnerHTML = "Your Script is being processed. " & "This might take several minutes to complete.  Closing this window will stop the script. So don't close this window ^_^"

'Create csv file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("\\192.168.100.4\data\IT\Scripts\testData.csv", ForWriting, True)

'Define Db String
connStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\192.168.100.16\Sage\2016\Connex\Sage100Shadow_CHRIS.mdb"

'Define object type
Set objConn = CreateObject("ADODB.Connection")

'Open Connection
objConn.Open connStr

'Define recordset and SQL query
Set rs = objConn.Execute("SELECT [BillNo],[InStock] FROM qryKitInvNums;")

MsgBox(rs.Fields(0).Type & ", " & rs.Fields(1).Type)

'While loop, loops through all available results
Do While Not rs.EOF
    'add values seperated by comma to getNames
    getNames = rs.Fields(0) & "," & rs.Fields(1)
    'Write current record to LogFile
    objLogFile.Write getNames
    'Line return for next record in LogFile
    objLogFile.Writeline

    'move to next result before looping again
    rs.MoveNext
    'continue loop
Loop

MsgBox(getNames)

'Close connection and release objects
objLogFile.Close
objConn.Close
Set rs = Nothing
Set objConn = Nothing

'Closes progress window
objExplorer.Document.Body.InnerHTML = "Your Script is now complete."

WScript.Sleep 2000

objExplorer.Quit

这是工作VBS文件的副本。唯一的区别是SQL查询仍然返回与第一个VBS文件相同类型的2个字段。

Dim connStr, objConn, getNames, objFSO
Const ForWriting = 2

'Make and open progress window
On Error Resume Next

Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 200
objExplorer.Visible = 1
objExplorer.Silent = 1
objExplorer.Document.Title = "Script in progress"
objExplorer.Document.Body.InnerHTML = "Your Script is being processed. " & "This might take several minutes to complete.  Closing this window will stop the script. So don't close this window ^_^"

'Create csv file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("\\192.168.100.4\data\IT\Scripts\testData.csv", ForWriting, True)

'Define Db String
connStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\192.168.100.16\Sage\2016\Connex\Sage100Shadow_CHRIS.mdb"

'Define object type
Set objConn = CreateObject("ADODB.Connection")

'Open Connection
objConn.Open connStr

'Define recordset and SQL query
Set rs = objConn.Execute("SELECT IM_ItemWarehouse.ItemCode, IIf([InStock] Is Null Or [InStock]="""",[IM_ItemWarehouse].[QuantityOnHand],[InStock]) AS EvalInstock FROM IM_ItemWarehouse LEFT JOIN qryInStockNum4ItemsWPO ON IM_ItemWarehouse.ItemCode = qryInStockNum4ItemsWPO.ItemCode WHERE (((IM_ItemWarehouse.ItemCode) Is Not Null) AND ((IM_ItemWarehouse.WarehouseCode)=""000""));")

'While loop, loops through all available results
Do While Not rs.EOF
    'add values seperated by comma to getNames
    getNames = rs.Fields(0) & "," & rs.Fields(1)
    'Write current record to LogFile
    objLogFile.Write getNames
    'Line return for next record in LogFile
    objLogFile.Writeline

    'move to next result before looping again
    rs.MoveNext
    'continue loop
Loop

'Close connection and release objects
objLogFile.Close
objConn.Close
Set rs = Nothing
Set objConn = Nothing

'Closes progress window
objExplorer.Document.Body.InnerHTML = "Your Script is now complete."

WScript.Sleep 2000

objExplorer.Quit

0 个答案:

没有答案