Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating CSV - filesystemobject 2

Status
Not open for further replies.

doorbreaker

Programmer
Nov 19, 2002
91
GB
Hi,

have some code in a vbs file that should drag all records out of a db and then spit them out into a csv file.

I run the file and no errors are thrown up but no csv file is made. Any ideas - code below....

I have checked the permissions on the folder.

Any help appreciated.

Chris

Code:
on error resume next
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.cursorlocation=3
oconn.Open "driver={sql server};server=xxx.xxx.xxx.xxx;database=mydb;uid=myuid;pwd=mypwd"



strSQL = "select * from productmaster"
			
				
Set oRS = objconn.Execute(strSQL)

dim oFSO, oFile,oField
dim strOutput


set oFSO = server.createobject ("scripting.filesystemobject")
set oFile = oFSO.createtextfile("c:\csv\kelkoo.csv",true)
do until oRS.eof
    for each oField in oRS.fields
        stroutput = stroutput & ofield.value & ","
    next
    
    stroutput = left(stroutput,len(stroutput)-1)
    ofile.writeline stroutput
    ors.movenext
loop

ofile.close
set oFile = nothing
set oFSO = nothing
 
first try this
Code:
on error resume next
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.cursorlocation=3
oconn.Open "driver={sql server};server=xxx.xxx.xxx.xxx;database=mydb;uid=myuid;pwd=mypwd"



strSQL = "select * from productmaster"
            
                
Set oRS = objconn.Execute(strSQL)

dim oFSO, oFile,oField
dim strOutput


'set oFSO = server.createobject ("scripting.filesystemobject")
'set oFile = oFSO.createtextfile("c:\csv\kelkoo.csv",true)
do until oRS.eof
    for each oField in oRS.fields
        stroutput = stroutput & "'" & ofield.value & "',"
    next
    
    stroutput = left(stroutput,len(stroutput)-1)
    'ofile.writeline stroutput
    ors.movenext
loop
response.write stroutput

'ofile.close
'set oFile = nothing
'set oFSO = nothing'
see if it displays anything first
 
you say this is a vbs file but it looks like asp form

server.creatobject

vs

createobject

and when your ready to create this csv, i would do it like this
Code:
do until oRS.eof
    for each oField in oRS.fields
        stroutput = stroutput & chr(34) & ofield.value & chr(34)& ","
    next
    
    stroutput = left(stroutput,len(stroutput)-1)
    stroutput = stroutput & vbCrLf
    ofile.writeline stroutput
    ors.movenext
loop

each row has to be separated by a vbCrLf
 
I am stil learning the ropes here.....

I renamed it .asp and now it works. Will I still be able to call this from a scheduled task?
 
I run the file and no errors are thrown up but no csv file is made



In order to see the errors you need to remove this line:
on error resume next
 
thanks people

just gotta work out how to run the page now from a scheduled task...but i think thats possibly a different forum.
 
I bet if you look under Scheduled Tasks in the Control Panel then you can figure it out... it is fairly straightforward.
 
I found a solution:

Apparently save the following code as whatevername.vbs and just set the URL to whatever page you want. Then in scheduled tasks just point to the vbs file.

Code:
 Sub MyASPJob()
  
    Dim oXMLHttp
    Dim sURL
                                
       on error resume next

       Set oXMLHttp = CreateObject("MSXML2.XMLHTTP.3.0")
       sURL = "[URL unfurl="true"]http://usr:pwd@www.mysite.com/job.asp[/URL]
              
       oXMLHttp.open "GET", sURL, false
       oXMLHttp.send()                  

          if oXMLHttp.status = 200 Then  
             ' Retrieve enter HTML response
             MsgBox oXMLHttp.ResponseText
          else
     ' Failed
  end if
    
Set oXMLHttp = nothing

End Sub

Call MyASPJob()

Cheers

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top