Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Save content from a text box in a text file

Hello all,

I need your help about my issue.

I need to be able, by clicking on a botton, To create and save in a text file the text1.text and the text2.text.

The file will have the name config.txt under "c:/config.txt"

On line 1 of the txt file, i will have the text1.text and in line 2, the text2.text.


That is the first thing.


The second thing, it will be that when i will open my vb6 tool, text1 and text2 will be populated by the txt file.

How can i do this please?

Thanks again for your help
0
Wilder1626
Asked:
Wilder1626
  • 5
  • 4
1 Solution
 
GrahamSkanRetiredCommented:
Command1 to save the text.
Command2 to read it back
Option Explicit

Private Sub Command1_Click()
    Dim f As Integer

    f = FreeFile
    Open "C:\MyFolder\MyFile.txt" For Output As #f
    Print #f, Text1.Text
    Print #f, Text2.Text
    Close #f
End Sub

Private Sub Command2_Click()
    Dim f As Integer
    Dim strLIne As String
    
    f = FreeFile
    Open "C:\MyFolder\MyFile.txt" For Input As #f
    Line Input #f, strLIne
    Text1.Text = strLIne
    Line Input #f, strLIne
    Text2.Text = strLIne
    Close #f
End Sub

Open in new window

0
 
GrahamSkanRetiredCommented:
Sorry missed the bit about opening with the boxes pre-populated. You will need to use the Form_Load event for that.
Option Explicit

Private Sub Command1_Click()
    Dim f As Integer

    f = FreeFile
    Open "C:\MyFolder\MyFile.txt" For Output As #f
    Print #f, Text1.Text
    Print #f, Text2.Text
    Close #f
End Sub

Private Sub Form_Load()
    Dim f As Integer
    Dim strLIne As String
    
    f = FreeFile
    Open "C:\MyFolder\MyFile.txt" For Input As #f
    Line Input #f, strLIne
    Text1.Text = strLIne
    Line Input #f, strLIne
    Text2.Text = strLIne
    Close #f
End Sub

Open in new window

0
 
roeibCommented:
in order to read the data from the TXT file use this:

Public Sub FileToArray(ByVal FileName As String, _
    ByRef TheArray As Variant)
'PURPOSE:    Puts all lines of file into a string array
'PARAMETERS: FileName = FullPath of File
'            TheArray = StringArray to which contents
'                       Of File will be added.
'Example
'  Dim sArray() as String
'  FileToArray "C:\MyTextFile.txt", sArray
'  For lCtr = 0 to Ubound(sArray)
'  Debug.Print sArray(lCtr)
'  Next

'NOTES:
'  --  Requires a reference to Microsoft Scripting Runtime
'      Library
'  --  You can write this method in a number of different ways
'      For instance, you can take advantage of VB 6's ability to
'      return an array.
' --   You can also read all the contents of the file and use the
'      Split function with vbCrlf as the delimiter, but I
'      wanted to illustrate use of the ReadLine
'      and AtEndOfStream methods.
'**********************************************************

  Dim oFSO As New FileSystemObject
  Dim oFSTR As Scripting.TextStream
  Dim ret As Long
  Dim lCtr As Long

  If Dir(FileName) = "" Then Exit Sub

'Check if string array was passed
'If you want to permit other type of arrays (e.g.,
'variant) remove or modify this line
If VarType(TheArray) <> vbArray + vbString Then Exit Sub
 
  On Error GoTo ErrorHandler
     Set oFSTR = oFSO.OpenTextFile(FileName)
     
     Do While Not oFSTR.AtEndOfStream
            ReDim Preserve TheArray(lCtr) As String
            TheArray(lCtr) = oFSTR.ReadLine
            lCtr = lCtr + 1
            DoEvents 'optional but with large file
                     'program will appear to hang
                     'without it
    Loop
     oFSTR.Close
     
ErrorHandler:
     Set oFSTR = Nothing
End Sub


then append the text in your textBox with the returned Array.

in order to write to the file :


  'declare and initiate required objects

   Dim fs As FileSystemObject
   Dim ts As TextStream
     
    Set fs = New FileSystemObject
       'To write
  If fs.FileExists("C:\mytestfile.txt") Then
     fs.Delete
    Set ts = fs.OpenTextFile("C:\mytestfile.txt", ForWriting, True)
    ts.WriteLine "Text1"
    ts.WriteLine "Text2"
    ts.Close
     
         'clear memory used by FSO objects
    Set ts = Nothing
    Set fs = Nothing
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
GrahamSkanRetiredCommented:
Note that you could consider using SaveSetting a GetSetting() instead.
0
 
GrahamSkanRetiredCommented:
This shows how to use SaveSetting and GetSetting
Option Explicit

Private Sub Command1_Click()
    SaveSetting "MyApp", "Settings", "TextOne", Text1.Text
    SaveSetting "MyApp", "Settings", "TextTwo", Text2.Text
End Sub

Private Sub Form_Load()
    Text1.Text = GetSetting("MyApp", "Settings", "TextOne")
    Text2.Text = GetSetting("MyApp", "Settings", "TextTwo")
End Sub

Open in new window

0
 
Wilder1626Author Commented:
What can i do if before openning the vb6 tool, i what him to see if the text file exist. If yes, it will apply the code:
 Dim f As Integer
    Dim strLIne As String
   
    f = FreeFile
    Open "C:\Parametre test.txt" For Input As #f
    Line Input #f, strLIne
    Text1.Text = strLIne
    Line Input #f, strLIne
    Text2.Text = strLIne
    Close #f


If No, it will create the file:
Private Sub cmdSauvegarder_Click()
 Dim f As Integer

    f = FreeFile
    Open "C:\Parametre test.txt" For Output As #f
    Print #f, Text1.Text
    Print #f, Text2.Text
    Close #f


So that the next time, when i will open the vb6 tool, it will import the parametres:

Private Sub Form_Load()
'Load parameters
Paramètres.Show
 Dim f As Integer
    Dim strLIne As String
   
    f = FreeFile
    Open "C:\Parametre test.txt" For Input As #f
    Line Input #f, strLIne
    Text1.Text = strLIne
    Line Input #f, strLIne
    Text2.Text = strLIne
    Close #f
Paramètres.Hide
End Sub
0
 
Wilder1626Author Commented:
Hello GrahamSkan,

Where does it save?

Option Explicit

Private Sub Command1_Click()
    SaveSetting "MyApp", "Settings", "TextOne", Text1.Text
    SaveSetting "MyApp", "Settings", "TextTwo", Text2.Text
End Sub

Private Sub Form_Load()
    Text1.Text = GetSetting("MyApp", "Settings", "TextOne")
    Text2.Text = GetSetting("MyApp", "Settings", "TextTwo")
End Sub

Open in new window

0
 
Wilder1626Author Commented:
I dont know where it save but it work.

Thanks a lot for your help.

And thanks to all the others.
0
 
GrahamSkanRetiredCommented:
To test if a file exists, you can use Dir

If Dir$("C:\MyFolder\MyFile.txt") = "" then
   'File does not exist
Else
   'FileExists
endif

SaveSetting saves in the registry.
0
 
Wilder1626Author Commented:
Perfect, thanks again
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now