Results 1 to 6 of 6

Thread: Write to a file in VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    8
    Does anyone know how to write to a text file using VB only?

    I need to query an access database. Then gather the queried information and then populate that information into the text file.

    Can someone help a new comer to VB coding.

    Thanks

    Rey

  2. #2
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    Like this:

    Open (C:\MyFile.txt) for output as #1'doesnt have to exist
    For intCount = 1 to intNumberofRecords
    Write #1, strData1(intCount),strData2(intCount)'as many different fields as there are
    Next intCount
    Close #1

    If that isn't what you meant let me know

  3. #3
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    Code:
    Dim myDB as Database
    Dim myRS as Recordset
    Dim FileNum as Integer
    
    FileNum = FreeFile
    
    Set myDB = DBEngine.Workspaces(0).OpenDatabase("DB.mdb")
    Set myRS = myDB.OpenRecordset("DaRecordset")
    
    Open "DB.txt" for Output as #FileNum
    Do Until myRS.EOF
    Print #FileNum, myRS!Feild1; "," ; myRS!Field2 ;vbCrLf 'and so on(End with vbCrLf or vbNewLine)
    myRs.MoveNext
    Loop
    
    Close FileNum
    Set myRS= Nothing
    Set myDB = Nothing
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    8
    Originally posted by wengang
    Like this:

    Open (C:\MyFile.txt) for output as #1'doesnt have to exist
    For intCount = 1 to intNumberofRecords
    Write #1, strData1(intCount),strData2(intCount)'as many different fields as there are
    Next intCount
    Close #1

    If that isn't what you meant let me know
    I am confused by the #1? What does that stand for? Also, where would I place my query into the database.

    Example. I need to find all the last names in the Peoples database.

    Select lastname from Peoples

    Is my query going to be placed within the For Next?
    Also, do I have to place all the information returned from the query into some placeholder, a recordset or where does the information reside until I want to place into the text file?

    Let me know if this confused you.

    Thanks

    Rey


  5. #5
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    In my code:
    put your SQL statement in openRecordset:
    Code:
    set myRS = myDB.OpenRecordset("SELECT lastname FROM Peoples")
    the 1 is a filenumber, a refrance for what file to open. Use the FreeFile function to get a freeone. 1 is almost always free, unless multiple files are open at same time. Just to be sure, use FreeFile.

    SQL will return a "Recordset" based on your Query into the myRS object. Use the methods in my code to loop through.

    Hope this helps
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  6. #6
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    Okay. I guess you better go with GWDash's code, since you are working with a database. For the general purpose of writing to a text file in VB, use the code I gave you.

    The #1 (where the # is optional these days) represents that it is the first file you have open. Whenever you use an OPEN statement, VB holds that file open until it receives a CLOSE statement. Therefore if you wanted to open several files, you would use #1,#2,#3.... to reference them. So when you say Write#1, that tells VB to write to the file that you designated as #1.

    Example
    Open "C:\text1.txt" for output as #1
    Open "C:\text2.txt" for output as #2
    Write #1, "This is the first text file."
    Write #2,"This is the first text file."
    Close #1
    Close #2


    Then you should have 2 new files on your c: with the corresponding texts.

    Sorry for the confusion.

Posting Permissions

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



Click Here to Expand Forum to Full Width