Integrate Export Function

Hi,

I have this function which exports to CSV

Function RecordsetToCSV(ByRef RS, ByVal CSVFilePath, ByVal IncludeFieldNames)
      Set objCSVFile = CreateObject("ADODB.Stream")
      Call objCSVFile.Open

If IncludeFieldNames Then
'string concatenation issues aren't a problem for a small string of field names
For Each Field In RS.Fields
If FieldNames = doublequote Then
FieldNames = Field.Name
Else
FieldNames = FieldNames & "," & Field.Name
End If
Next

FieldNames = FieldNames & vbCRLF
Call objCSVFile.WriteText(FieldNames, 1)
End If

Call objCSVFile.WriteText(FieldNames & RS.GetString(adClipString, , ",", vbCRLF, doublequote))
Call objCSVFile.SaveToFile(CSVFilePath, 2)
Set objCSVFile = Nothing
End Function

But when i enter the values for the third argument i get an error.

Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'IncludeFieldNames'
/transactadmin.asp, line 8

I am obviously doing something wrong. Can someone show me how they think this function should be called.
LVL 20
REA_ANDREWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alorentzCommented:
WHat happens id you change to this:

Function RecordsetToCSV(RS,CSVFilePath,IncludeFieldNames)
alorentzCommented:
Or, change to this:

If IncludeFieldNames =true Then
REA_ANDREWAuthor Commented:
alorentz, i did that, changed the function part to If IncludeFieldNames =true Then and i called it using this

if request.querystring("export")=1 then
call RecordsetToCSV(Objrs,"transactions.csv",true)
end if

the error i now get is this

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
/transactadmin.asp, line 22

LINE 22./ is

Call objCSVFile.WriteText(FieldNames & RS.GetString(adClipString, , ",", vbCRLF, doublequote))
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

alorentzCommented:
Use response.write to see what your values are.  My previous answer got you past the initial problem, and now this is new one....so need to debug a little.

What are in these values, and what are you trying to do:

FieldNames & RS.GetString(adClipString, , ",", vbCRLF, doublequote))
alorentzCommented:
And, is this supposed to be WriteLine:

Call objCSVFile.WriteLine(FieldNames & RS.GetString(adClipString, , ",", vbCRLF, doublequote))
alorentzCommented:
>>And, is this supposed to be WriteLine:

Nevermind..I see it's a stream.
alorentzCommented:
Seems like you RS object is not passing in appropriately, or is not opened properly.
REA_ANDREWAuthor Commented:
alorentz

FieldNames & RS.GetString(adClipString, , ",", vbCRLF, doublequote))

i think that line is asking me what to separate the values with in the csv file.  I have tried calling it like this

call RecordsetToCSV(Objrs,"transactions.csv","doublequote") <---i am thinking cause i want to enclose all values in double quotes

but i get this error

Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'IncludeFieldNames'
/transactadmin.asp, line 8

line 8./ is

If IncludeFieldNames Then

I have taken the true out, i will try again with this call method with true in.
alorentzCommented:
Ummm...why would you take out the TRUE part...we just go that to work!

And, shouldn't it be like this:  (remove FieldNames, because you already have them above this line...and put in variable)

val = RS.GetString(adClipString, , ",", vbCRLF, chr(34))
Call objCSVFile.WriteText(val)

REA_ANDREWAuthor Commented:
tell you what, instead of this one, can you advise me of a script with which you are familiar and i will use that one?

regards

andrew
alorentzCommented:
This is the write function for what you need to do.  Have you tried what I said?
REA_ANDREWAuthor Commented:
ok I have amended that, now how d'you reckon i call it, as the third argument is confusing me. I will put true back in, and is it expecting simply true?
REA_ANDREWAuthor Commented:
Here is my amended function

Function RecordsetToCSV(RS,CSVFilePath,IncludeFieldNames)
      Set objCSVFile = CreateObject("ADODB.Stream")
      Call objCSVFile.Open

If IncludeFieldNames = true Then
'string concatenation issues aren't a problem for a small string of field names
For Each Field In RS.Fields
If FieldNames = doublequote Then
FieldNames = Field.Name
Else
FieldNames = FieldNames & "," & Field.Name
End If
Next

FieldNames = FieldNames & vbCRLF
Call objCSVFile.WriteText(FieldNames, 1)
End If

val = RS.GetString(adClipString, , ",", vbCRLF, chr(34))
Call objCSVFile.WriteText(val)

'Call objCSVFile.WriteText(FieldNames & RS.GetString(adClipString, , ",", vbCRLF, doublequote))
Call objCSVFile.SaveToFile(CSVFilePath, 2)
Set objCSVFile = Nothing
End Function

Here is how i called it

call RecordsetToCSV(Objrs,"transactions.csv",true)

and here is the error i now get

ADODB.Recordset (0x800A0BB9)
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
/transactadmin.asp, line 22

thanks so far for your help
alorentzCommented:
First thing is you should take it OUT of a function until you get it to work properly....then put it in function.

And just use RS.GetString() to start, just to see how it works.
alorentzCommented:
>>transactadmin.asp, line 22

With current code...what line is 22?
REA_ANDREWAuthor Commented:
ok yeh that worked, although i would like to restrict the method to 4 fieldnames, but i will tackle that later.  It does as you wanted, it prints out all values.
REA_ANDREWAuthor Commented:
line 22.

val = RS.GetString(adClipString, , ",", vbCRLF, chr(34))

i had taken out the function, so to correctly find line 22 i put it back in/.
alorentzCommented:
>>val = RS.GetString(adClipString, , ",", vbCRLF, chr(34))

Correct, because the RS object is not passed into the function properly, so you get that error.

What happens is you use RS.GetString() in the function instead?  Probably same error?
REA_ANDREWAuthor Commented:
Alorentz,

I used just the val = RS.GetString()

and i get this error

ADODB.Stream (0x800A0BBC)
Write to file failed.
/transactadmin.asp, line 26

any ideas? or a more straight forward script lol

Cheers

Andrew
alorentzCommented:
Now it sounds like IUSR account doesn't even have permissions to create a file...

Do you know how to check ISS/NT permissions?
REA_ANDREWAuthor Commented:
no, but you have just said something that has been doing my head in the past couple of days.  I am inline to get priveledges at work but until that happens a number of tasks cannot be completed. haha ain't that the truth, well at least i can now add this to the list of things i cannot do until i have rights.

alorentzCommented:
Here, this should be straight forward enough:

Response.Clear
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "filename=" & FileName & ";"
set objItem = DBConn.execute("SELECT * FROM YourTable")
Response.Write objitem.GetString(,,"|",vbcrlf)
dbconn.close

Just a note, all this can be figured out by simply reading what the functions do online...like GetString().
alorentzCommented:
And of course "FileName" is a varaible that you need to assign.
REA_ANDREWAuthor Commented:
willl the filename component enable it so when they click on the link

"EXPORT TO CSV"

this will bring up the download box.  The points are yours at this point ofcourse. Thank you for your time

Andrew
alorentzCommented:
Usage:

page1.asp
<a href="download.asp?filename=test.csv">EXPORT TO CSV</a>


download.asp
<%

Response.Clear
Response.ContentType = "text/csv"

FileName=request("filename")
Response.AddHeader "Content-Disposition", "filename=" & FileName & ";"
set objItem = DBConn.execute("SELECT * FROM YourTable")
Response.Write objitem.GetString(,,"|",vbcrlf)
dbconn.close
%>


Try it out...you'll see how it works.  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
REA_ANDREWAuthor Commented:
Excellent.  That is a lot more efficient that the other one i was trying to use.

Cheers

Andrew
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.