Question

I'm trying to connect to a SQL server(localhost) and extract the following:

SELECT INVOICENO, SUPPLIERID, AMOUNT, DOCID
FROM ES_TRANS_HEADER
WHERE READYTOINVOICE = 1

I then need to get this information into a .txt file, seperated by commas

This is what I got so far:

function accounting()
  Dim myStream, connection, myCommand, recValue
  Set myStream = CreateObject("ADODB.Stream")
  Set connection = CreateObject("ADODB.Connection")
  Set myCommand = CreateObject("ADODB.Command")
  connection.Open "Provider=SQLNCLI10;" & _
    "Data Source=localhost;" & _
    "Integrated Security=SSPI;" & _
    "Initial Catalog=SQLDatabase;" & _
    "User ID=;Password="
  myCommand.ActiveConnection=connection
  myCommand.CommandText="SELECT INVOICENO,SUPPLIERID, AMOUNT, DOCID FROM ES_TRANS_HEADER WHERE READYTOINVOICE = 1"
  SET recValue = myCommand.Execute()

  If Not recValue.EOF then
     MsgBox "INVOICENO = " & recValue(0) & vbcrlf & "SUPPLIERID=" & recValue(1) _
     & "AMOUNT="  &  recValue(2) & "DOCID=" & recValue(3)
  End If

  While Not recValue.EOF
    INVOICENO = recValue(0)
    SUPPLIERID = recValue(1)
    AMOUNT = recValue(2)
    DOCCID = recValue(3)
    recValue.MoveNext
  Wend
end function

Am I on the right track here, and if so what should I try to do next?

Thanks in advance

Was it helpful?

Solution

MsgBox didn't have any parentheses missing. Remove them (and read Eric Lippert's most awesome blog post on parentheses in VBScript). What's missing in that line is a line continuation character (_) at the end of the line. In VBScript you cannot wrap lines without that. Change this:

MsgBox ("INVOICENO = " & recValue(0) & vbcrlf & "SUPPLIERID=" & recValue(1)
& "AMOUNT="  &  recValue(2) & "DOCID=" & recValue(3))

into this:

MsgBox "INVOICENO = " & recValue(0) & vbcrlf & "SUPPLIERID=" & recValue(1) _
  & "AMOUNT="  &  recValue(2) & "DOCID=" & recValue(3)

or put the whole statement in a single line:

MsgBox "INVOICENO = " & recValue(0) & vbcrlf & "SUPPLIERID=" & recValue(1) & "AMOUNT="  &  recValue(2) & "DOCID=" & recValue(3)

As for the database connection, your connection string seems odd. According to the information provided here that connection string is for connections to a mirrored database. Try this instead:

db = "..."  'specify database name here
connection.Open "Provider=SQLNCLI10;" _
  & "Server=localhost;" _
  & "Database=" & db & ";" _
  & "Trusted_Connection=yes;"

If that doesn't help: check the value of the connection's State property:

WScript.Echo connection.State

Also, update your question with any error you're getting (error number, error message and line number).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top