Writing to a text file in VB6

habib -  
 lepasprodutous -
Bonjour, how to write to a text file using Visual Basic 6.0 code.

7 answers

  1. bel84
     
    I've found out how to write to a file!
    Below is the code:

    __
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim file As System.IO.StreamWriter
    Dim mywriteline, T(3) As String
    Dim i As Integer
    Dim A, B, C As String
    ' variables to write
    A = "1"
    B = "2"
    C = "3"
    'We put the variables into an array
    T(0) = A
    T(1) = B
    T(2) = C

    'Opening the file for writing, erasing existing lines.
    'To append data, use True instead of False at the end of the line.
    file = My.Computer.FileSystem.OpenTextFileWriter(My.Computer.FileSystem.CurrentDirectory & "\tarif.txt", False)
    'writing line by line
    mywriteline = ""
    Do Until i > 3
    mywriteline = T(i)
    file.WriteLine(mywriteline)
    i = i + 1
    Loop
    file.Close()
    End Sub

    __
    5
    1. Epica84
       
      You are programming in VB.net and not in VB6, if I'm not mistaken...
      0
    2. lepasprodutous
       
      how do we do it in vb 2010
      0
  2. Spider
     
    To write to a file, there are several options:

    1. open "C:\essai.txt" for random as #1
    put 1,,"Hello" --> to write
    get 1,,myVariable --> to read

    2. open "C:\essai.txt" for output as #1
    write #1, "Hello" --> to write

    2a. open "C:\essai.txt" for input as #1
    input #1, myVariable --> to read

    3. the method with "Append" mentioned by Kalamit

    Note: after using a file, you must use the instruction
    " close" to close the file.
    3
    1. pouic
       
      there is also a binary for
      0
    2. kikoooo
       
      thank you so much for this thing
      0
    3. l'essayeur
       
      Me, it doesn't work.
      0
    4. ddd
       
      It's crazy the number of people who talk to say anything, without knowledge.

      We don't use 'for binary' to write to a file. (Try it and tell me.)

      Similarly for the first answer, it's vb.net and not vb6, .net is ugly.
      Otherwise, nothing to say about the code above.
      0
  3. bel84
     
    Indeed, it's very difficult to find information on text file management under VB Express.
    For my part, I finally managed to open and read a structured file line by line. I am currently working on writing to the file. Below is the code:
    __
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim mystreamreader As IO.StreamReader
    Dim myreadline(), T(3) As String
    Dim i As Integer
    Dim A, B, C As String
    ' The file is named tarif.txt, contains 3 lines for testing, and is located in the same folder as the executable.
    'Opening in read mode
    mystreamreader = My.Computer.FileSystem.OpenTextFileReader(My.Computer.FileSystem.CurrentDirectory & "\tarif.txt")
    'Reading line by line
    Do Until mystreamreader.EndOfStream
    myreadline = Split(mystreamreader.ReadLine(), "")
    T(i) = myreadline(0)
    i = i + 1
    Loop
    'Inputting data into variables
    A = T(0)
    B = T(1)
    C = T(2)
    End Sub
    __

    Below is the file "Tarif.txt" created with Notepad:

    123
    456
    789
    2
  4. teebo Posted messages 33585 Registration date   Status Contributor Last intervention   1 797
     
    I'm going back instead of asking the same question, but if I find it, I'll reply to you... Unless you found it?

    .
    \_/
    0
    1. habib
       
      hi
      this small code allows you to write and display the content of the text file
      Private Sub Command1_Click()
      Dim Response1 As Long
      Open "c:\TEST\Rapport.txt" For Append As 1
      Print #1, "******************************************"
      Print #1, "****************************************"
      Print #1, "************************************"
      Close
      'display the text file
      Response = Shell("NotePad.exe c:\TEST\Rapport.txt", vbNormalFocus)
      End Sub
      0
  5. teebo Posted messages 33585 Registration date   Status Contributor Last intervention   1 797
     
    And here’s an interesting link for all those who have the problem... It works like a charm for me (also read the comments from others on this code...)
    .
    \_/
    0
    1. pas_20
       
      According to me, your link didn't work!
      0
  6. kalamit
     
    Hello,

    Open "c:\TEST\error.log" For Append As 1
    Print #1, "Error in the PDB To txt procedure"
    Print #1, PdbConvert1.GetErrorText
    Print #1, ocxerror
    Print #1, " "
    Print #1, "***************************************"
    Print #1, " "
    Close

    This is a sample code. But check the file opening options on VBFrance, there are several.
    a+

    Kalamit,
    I sand down, therefore I wipe.
    0
  7. Eusebe
     
    Hello,

    I am currently using Visual Basic Express to do the same thing (the goal is to create a text file containing numerical values in a certain order, and I would like to know if you know a way to open the text file under Express. The open function does not seem to exist under Express.
    0