Forums
This topic is locked
CreateTextFile - within a loop
Posted 31 Oct 2006 18:58:14
1
has voted
31 Oct 2006 18:58:14 David Tregoning posted:
<font color=green>I have built a page that opens a recordset used this information to populate a string that is turned into a html via the asp code CreateTextFile. This is then attached to an email and sent - note I have not included the email part on the example below: </font id=green><pre id=code><font face=courier size=2 id=code> Dim rsRecordset
Dim rsRecordset_numRows
Set rsRecordset = Server.CreateObject("ADODB.Recordset"
rsRecordset.ActiveConnection = MM_Test_STRING
rsRecordset.Source = "SELECT * FROM tblTest"
rsRecordset.CursorType = 0
rsRecordset.CursorLocation = 2
rsRecordset.LockType = 1
rsRecordset.Open()
rsRecordset_numRows = 0
Dim Repeat1__numRows
Dim Repeat1__index
Repeat1__numRows = -1
Repeat1__index = 0
rsRecordset_numRows = rsRecordset_numRows + Repeat1__numRows
Dim strHTML
'loop through records
While ((Repeat1__numRows <> 0) AND (NOT rsRecordset.EOF))
strHTML = "This is the content of the Text file " & rsRecordSet("NumberID" & " - which is an HTML file"
strPath = Server.MapPath("Test.html"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject"
If objFSO.FileExists(strPath)= true Then
objFSO.DeleteFile strPath,true
End If
Set objOrderFile = objFSO.CreateTextFile(strPath,True)
objOrderFile.Write(strHTML)
objOrderFile.Close
Set objOrderFile = Nothing
'end of loop
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
rsRecordset.MoveNext()
Wend
rsRecordset.Close()
Set rsRecordset = Nothing
</font id=code></pre id=code>
<font color=green>I have run some tests using response.write and response.flush and the code runs through fine once but when it loops back through again I get the following error </font id=green><BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>Microsoft VBScript runtime error '800a0046'
Permission denied <hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote> <font color=green>with the error being flagged as </font id=green><pre id=code><font face=courier size=2 id=code>Set objOrderFile = objFSO.CreateTextFile(strPath,True) </font id=code></pre id=code><font color=green>upon the second loop.
Does anybody know why I can create attach send and delete a file once but not a second time when the script is looped again.
Many thanks</font id=green>