Exception when writing to a file

T

Tim Chmielewski

The following code is meant to take records from a database and write
them to a file in a comma delimited CSV format:

Set fso = CreateObject("Scripting.FileSystemObject")
filename1 = FilePath & filename1

Set f1 = fso_OpenTextFile(filename1, ForWriting, True)
f1.WriteLine "Name,School,Address Line 1,Address Line 2,Address Line
3,Address Line 4,Date Requested"

Do While Not rs.EOF

strAddress = rs("PostalAddress")
arrAdd = Split(strAddress, vbcrlf)
For i = LBound(arrAdd) to Ubound(arrAdd)
ArrGetAdd(i) = arrAdd(i)
Next

'The following line generates an exception
f1.WriteLine rs("Name") & "," & rs("School") & "," & ArrGetAdd(0) &
"," & ArrGetAdd(1) & "," & ArrGetAdd(2) & "," & ArrGetAdd(3) & "," & rs
("DateRequested")
rs.MoveNext
Loop
f1.Close
shortpath = "<BR><A HREF=" & QUOT & filename2 & QUOT & ">View CSV
File</A><BR>"

At first I thought it was something to do with the array, but it would
have said it was an 'array out of bounds' error if that was the case.

I have tested the values in the ArrGetAdd array and they seem to have
been stored fine.

Is there anything else I have missed that could be causing the error?

Thanks.
 
R

Ray at

My thoughts on the matter are:

1. What's your actual error?
2. I'm assuming you just didn't include the part where you're creating your
recordset, and you are in fact doing it.
3. For the address, you could skip the whole array process and just do a
Replace(theADdress, vbCrLf, ",")
4. Instead of using rs("name"), it can't hurt to use
rs.Fields.Item("name").Value
5. Make sure that your IUSR account (or the account under which this
process is running) has permissions to create and modicy files.
6. Do you have ForWriting defined in your code?

Ray at home
 
T

Tim Chmielewski

My thoughts on the matter are:

1. What's your actual error?

error '80020009'
Exception occurred.


2. I'm assuming you just didn't include the part where you're
creating your recordset, and you are in fact doing it. Yes.

3. For the address, you could skip the whole array process and just
do a Replace(theADdress, vbCrLf, ",")
I hadn't thought of this, thanks!

4. Instead of using rs("name"), it can't hurt to use
rs.Fields.Item("name").Value
I will have a look at this, thank you.

5. Make sure that your IUSR account (or the account under which this
process is running) has permissions to create and modicy files.
The page was previously working before I tried to modify the address was
written to the file.
6. Do you have ForWriting defined in your code?
Yes (defined further up):
Const ForReading = 1
Const ForWriting = 2

Thanks for your assistance.
 
T

Tim Chmielewski

"Ray at <%=sLocation%>" <myFirstNameATlane34dotKOMM> wrote in

4. Instead of using rs("name"), it can't hurt to use
rs.Fields.Item("name").Value

For some reason the following occured when I tried doing that:
Microsoft OLE DB Provider for ODBC Drivers error '80040e21'

Multiple-step OLE DB operation generated errors. Check each OLE DB status
value, if available. No work was done.


Thanks.
 
R

Ray at

Switch to OLEDB, and if an error still occurs, it'll at least be a little
more descriptive. www.connectionstring.com for your string. Also note that
"name" is a reserved keyword in Access and ODBC, so you should either rename
your column or bracket it in your query. "select [name],...."

Ray at home
 
T

Tim Chmielewski

Switch to OLEDB, and if an error still occurs, it'll at least be a
little more descriptive. www.connectionstring.com for your string.
Also note that "name" is a reserved keyword in Access and ODBC, so you
should either rename your column or bracket it in your query. "select
[name],...."
The error went away as soon as I changed the provider to OLEDB, Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top