Results 1 to 3 of 3

Thread: How to write to a text file?

  1. #1
    Join Date
    Apr 2007
    Posts
    1,259
    Rep Power
    0

    Default How to write to a text file?



    here is the thing i want when i click store information it stores the data in a path i already created
    i have this so far

    Private Sub Command3_Click()
    End
    End Sub

    Private Sub Form_Load()
    If Dir("C:\Program Files\V-Phone Book", vbDirectory) = vbNullString Then
    MkDir "C:\Program Files\V-Phone Book"
    End If
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.CreateTextFile("C:\Program Files\V-Phone Book\Adress Book.txt")

    End Sub

    So what i need now is to link the text box so that when i click store it saves it in the path above.
    www.carhuntja.com - Buy and Sell Cars in Jamaica! - Coming Soon
    www.blackspade-ent.com - Jamaicas #1 Online Community
    www.mybbunlock.com - Instant Blackberry Unlocks Only 9.99 USD
    www.starallianceja.com - Negril's #1 In Entertainment

  2. #2
    Join Date
    Aug 2002
    Posts
    6,223
    Rep Power
    0

    Default

    Open the txt file for output and then write the data to it.

    Open "C:\Program Files\V-Phone Book\Adress Book.txt" For Output As #1

    Print #1, strLine

    Close #1
    .
    PC - Ubuntu 15.04 64bit Desktop
    HP Pav G60-236US 3GB RAM Laptop, Ubuntu 15.04 64bit and Win7 Home

    "So Daddy, how come you telling me stealing not right when YOU copying DVDs? How come? How Come?"


    RIP Ramesh ...

  3. #3
    Join Date
    Feb 2005
    Posts
    85
    Rep Power
    0

    Default

    I think a more structured approach would be:

    Code:
    Dim myPath as string ' make this a form global variable 
    Dim myFile as string ' make this a form global variable 
    
    Private Sub Form_Load()
    
     myPath="C:\Program Files\V-Phone Book"
     If Dir(myPath, vbDirectory) = vbNullString Then
    	MkDir myPath
     End If
     myFile=myPath & "\Adress Book.txt"
    
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFile = objFSO.CreateTextFile(myFile)
    
    End Sub
    
    
    'on the button click event
    
     Dim F As Integer
     F = FreeFile
     Open myFile For Output As #F 
     Print #F, strLine ' where strLine is any text you wish to write.
     Close #F
    You might want to check if the file already exists an take some appropriate action also
    Last edited by ToxXxic; Dec 12, 2008 at 04:13 PM.
    Code King aka Code WizZzard: Motivated By The Challenge, Driven By The Will To Succeed.

    In The Land Of Programmers, The Code WizZzard Is KING. Sen on anything VB

Posting Permissions

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