I have a VBS file that queries an access database and writes the results to a .CSV file. The only thing being written to the .CSV file is ÿþ
.
I am using the same VBS file with a more complex query and it runs fine. Both queries return 2 fields of the same type. The only change between the 2 files is the SQL query. Also if I paste the query I'm trying to use into Access, it runs the query as expected.
Here is the VBS file:
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
Here is a copy of the working VBS file. The only difference is the SQL query which still returns 2 fields of same type as the first VBS file.
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