Creating a text file from Table records

Results 1 to 4 of 4

Thread: Creating a text file from Table records

  1. #1
    Join Date
    Dec 1969
    Posts
    226

    Default Creating a text file from Table records

    Hello,<BR><BR>I&#039m rather new to ASP, I have accomplished many new techniques in ASP but for some reason (I searched the archives and I&#039m having trouble finding a complete example, I need to be certain of the syntax and page layout) I&#039m having a bit of difficulty getting the how to&#039s file writing down: I visited 4fellows they had an example that used input from a form as well as what I found here, I haven&#039t found a example on reading the records data out of a table.<BR><BR>I want to create a text file by reading a table and writing the data to a text file called new.txt<BR><BR><BR>This the only type of example I have found. This takes the data from a form page... <BR> I know how to take data from a form and create an appenable text file;<BR> Dim fs,a<BR> If Request(&#039Submit&#039) = &#039Submit&#039 then<BR> Set fs = CreateObject(&#039Scripting.FileSystemObject&#039) <BR> Set a = fs.OpenTextFile(&#039C:
    ew.txt&#039, 8, TRUE, FALSE)<BR> a.WriteLine(Request.Form(&#039Name&#039))<BR> a.Close<BR> End If<BR><BR>So this didn&#039t show me how to get the data from a SQL database table unfortunitly. I&#039m not sure how to set the page up to A. read the data recrod out of the table, then write it to the text file, can I do it all on one page, do I need looping <BR><BR><BR><BR>But I need to read data from a table and put that info into a text file, I can&#039t find a complete example of how to do this, I&#039m missing the trees for the forest I think, would the code below work? Once again I need to read certain fields from a record, then jsut write one line per record to a text file that I can later append to it more records. I have found only three references in the archives on 15seconds related to it but they related to writing from a form to the text file. Can someoine please review the code below to see if it is layed out correctly;<BR><BR>Any suggestions on my code below or examples would be most appreciated.<BR><BR>Sincerely,<BR><BR>george<BR><B R><BR>Code example - untested <BR><BR>purpose -read a table then write the record to a text file, one line per record.<BR><BR>&#060;%<BR>&#039 Read Table data<BR>Set cn = Server.CreateObject("ADODB.Connection")<BR> cn.Open Application("Connection_String")<BR> q = "SELECT company,name,address,city,zip,state FROM members WHERE approved=1 AND deleted=0" &_<BR> " GROUP BY company ORDER BY company"<BR> Set rs = cn.Execute(q)<BR> <BR><BR>&#039 Write table data to text file<BR>Do While Not RS.eof<BR> Set fs = CreateObject(&#039Scripting.FileSystemObject&#039) <BR> Set a = fs.OpenTextFile(&#039C:
    ew.txt&#039, 8, TRUE, FALSE)<BR> a.WriteLine(rs.(&#039company&#039))<BR> a.WriteLine(rs.(&#039name&#039))<BR> a.WriteLine(rs.(&#039address&#039))<BR> a.WriteLine(rs.(&#039city&#039))<BR> a.WriteLine(rs.(&#039zip&#039))<BR> a.WriteLine(rs.(&#039state&#039))<BR> a.Close<BR> RS.moveNext<BR><BR> Loop<BR>%&#062;<BR>&#060;html&#062;<BR><BR>&#060;h ead&#062;<BR>&#060;meta name="GENERATOR" content="Microsoft FrontPage 3.0"&#062;<BR>&#060;title&#062;Write to a text file&#060;/title&#062;<BR>&#060;/head&#062;<BR><BR><BR><BR>&#060;/body&#062;<BR>&#060;/html&#062;<BR><BR><BR><BR><BR>

  2. #2
    Join Date
    Dec 1969
    Posts
    915

    Default RE: Creating a text file from Table records

    I think your code would work, but it is going to open the new.txt file for each record. What if you have 1.5 million records? Also, this will create entries in the text file that are all jumbledtogether (&#060;-- eg). A better way may be to create a csv file, and write it like this (BTW: Chr(34) produces ").<BR><BR>Set fs = CreateObject(&#039Scripting.FileSystemObject&#039) <BR>Set a = fs.OpenTextFile(&#039C:
    ew.txt&#039, 8, TRUE, FALSE)<BR><BR>Do while not rs.EOF<BR>&#039 Create a string with the recordset Comma Seperated<BR>&#039 Don&#039t put the comma after the last field<BR>strWriteLine = Chr(34) & rs("company") & Chr(34) & "," & chr(34) & rs("name") & Chr(34) & "," ... <BR><BR>&#039 Write the string to the file, and include a CRLF at the end.<BR>a.WriteLine (strWriteLine) & vbCrLf<BR>&#039 Clear Out the variable to receive the nest record<BR>strWriteLine = ""<BR>&#039 Move to the next record and begin Loop<BR>rs.MoveNext<BR>Loop<BR><BR>&#039 Close the file opened for append purposes<BR>a.Close<BR><BR><BR>You could also write a COM object to do this, I have written one, that does just this, post your e-mail if you are interested.<BR><BR>Jerry <BR>

  3. #3
    Join Date
    Dec 1969
    Posts
    226

    Default RE: Creating a text file from Table records

    Jerry,<BR><BR>Thanks, that was the cleanest solution, I tried a version like this; <BR><BR>&#039 Write table data to text file<BR> Do While Not RS.eof<BR> a.WriteLine rs("company").value & "," & _<BR> rs("address").value & "," & _<BR> rs("city").value & "," & _<BR> rs("state").value & "," & _<BR> rs("zip").value & "," & _<BR> rs("fax").value <BR>RS.moveNext<BR>Loop<BR> <BR>But yours was much better processing/performance wise.<BR><BR>**<BR>I would appreciate very much receiving the com object you mentioned:<BR>RE:&#060;&#060;You could also write a COM object to do this, I have written one, that does just this, post your e-mail if you are interested.&#062;&#062;<BR><BR>I apprciate your sharing something you put work into for my benefit.<BR><BR>My e-mail address is ascent@mindspring.com<BR><BR>Thank you again!<BR><BR>George<BR><BR> <BR> <BR> <BR>

  4. #4
    Join Date
    Dec 1969
    Posts
    716

    Default RE: Creating a text file from Table records

    Sorry to jump in on your exchange, but I&#039d also<BR>be interested in having a look at your COM object<BR>if you&#039re willing to share it.<BR><BR>My email is: justin@epoch.co.jp<BR><BR>Regards,<BR>Justin<BR><B R>&#062; You could also write a COM object to do this, <BR>&#062; I have written one, that does just this, <BR>&#062; post your e-mail if you are interested.<BR>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •