Link to home
Start Free TrialLog in
Avatar of REA_ANDREW
REA_ANDREWFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of alorentz
alorentz
Flag of United States of America image

WHat happens id you change to this:

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

If IncludeFieldNames =true Then
Avatar of REA_ANDREW

ASKER

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))
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))
And, is this supposed to be WriteLine:

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

Nevermind..I see it's a stream.
Seems like you RS object is not passing in appropriately, or is not opened properly.
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.
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)

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
This is the write function for what you need to do.  Have you tried what I said?
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?
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
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.
>>transactadmin.asp, line 22

With current code...what line is 22?
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.
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/.
>>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?
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
Now it sounds like IUSR account doesn't even have permissions to create a file...

Do you know how to check ISS/NT permissions?
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.

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().
And of course "FileName" is a varaible that you need to assign.
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
ASKER CERTIFIED SOLUTION
Avatar of alorentz
alorentz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent.  That is a lot more efficient that the other one i was trying to use.

Cheers

Andrew