Link to home
Start Free TrialLog in
Avatar of daky
dakyFlag for Croatia

asked on

Very easy: write to text file

Hi! This one is pretty easy (although rough english)...

How to make a regular textbox, and when you press button "Save" it saves written text from textbox into pre-defined file? So, I want to make simple program when you write something, and press save, and then quit the program, and when you re enter it, it reads automaticly defined file, and enters textbox contents from that file!?

Is that question related to API functions?
Thanks, Daky
Avatar of xSinbad
xSinbad

To write to a text file you can use this;



Dim varTextBox
Open "Filename" For Output As #1
Write #1, varTextBox
Close #1

To read it again;

Dim varTextBox
Open "Filename" For Input As #1
Input #1, varTextBox
Close #1
Avatar of Ark
First way - using only VB

Open sFileName For Binary As #1
   put #1,,Text1.Text
Close #1

Or you can use FileSystemObject:
'Add referense to Microsoft Scripting Runtime
Private Function WriteTextFile(fName As String, _
  sText As String) As Boolean

  Dim FSO As New FileSystemObject
  Dim FSTR As Scripting.TextStream
  On Error Resume Next
  Set FSTR = FSO.OpenTextFile(fName, ForWriting, _
    Not FSO.FileExists(fName))
  FSTR.Write sText
  WriteTextFile = True
  FSTR.Close
  If Err.Number Then WriteTextFile = False
  On Error GoTo 0
  Set FSTR = Nothing
  Set FSO = Nothing
End Function

' Using
' Private Sub Command2_Click()
'   Dim WriteOK as Boolean
'   WriteOK = WriteTextFile("c:\myfile.txt", Text1.Text)
' End Sub

Cheers
Option Explicit
Dim IsDirty As Boolean
Dim strFileName As String
Dim fsoFile As New FileSystemObject
Dim tstFile As TextStream

Private Sub Form_Load()
 strFileName = "c:\test.txt"
 If fsoFile.FileExists(strFileName) Then
    Set tstFile = fsoFile.OpenTextFile(strFileName, ForReading)
    txtText.Text = tstFile.ReadAll
    tstFile.Close
    Set tstFile = Nothing
 Else
    txtText.Text = ""
    fsoFile.CreateTextFile (strFileName)
 End If
 
End Sub

Private Sub txtText_Change()
    IsDirty = True
End Sub

Private Sub cmdSave_Click()
    If IsDirty Then
        Set tstFile = fsoFile.OpenTextFile(strFileName, ForWriting)
        tstFile.Write txtText
        tstFile.Close
        Set tstFile = Nothing
        IsDirty = False
    End If
End Sub

Avatar of daky

ASKER

I tried your, BlackShark, but I must mention that I am using Visual Basic 5! (not 6) and that following code doesn't work for me:

Dim fsoFile As New FileSystemObject
Dim tstFile As TextStream

If there is other way, I will try it! (Now I am gonna try 2 other ways)
Avatar of daky

ASKER

Xsinbad, and Ark:

I get error message on this:

Dim FSO As New FileSystemObject

Xsinbad, please could you tell me where to write you code? (I'm kinda newbie for Visual Basic). This applys for all of you. Thank you.
Hi
You need to add reference to Scriping.
OK, without references (Late Binding)

Private Function WriteTextFile(fName As String, _
 sText As String) As Boolean

 Dim FSO As Object
 Dim FSTR As Object
 On Error Resume Next
 Set FSO = CreateObject("Scripting.FileSystemObject")
 Set FSTR = FSO.OpenTextFile(fName, ForWriting, _
   Not FSO.FileExists(fName))
 FSTR.Write sText
 WriteTextFile = True
 FSTR.Close
 If Err.Number Then WriteTextFile = False
 On Error GoTo 0
 Set FSTR = Nothing
 Set FSO = Nothing
End Function

' Using
' Private Sub Command2_Click()
'   Dim WriteOK as Boolean
'   WriteOK = WriteTextFile("c:\myfile.txt", Text1.Text)
' End Sub

Cheers
Hi!

Here's a file over the net for you:

View code...
http://www.planetsourcecode.com/xq/ASP/txtCodeId.584/lngWId.1/qx/vb/scripts/ShowCode.htm

Descrition: .INI read/write routines

Well, I soppse it works more than just *.ini files.  Since *.ini files are also text files, I suppose it'll work.

That's it!

glass cookie : )
Place the first bit of code in the Save buttond On_Click event (rename the save button to "cmdSave" and you text box to txtData;

Private Sub cmdSave_Click()
Dim varTextBox
varTextBox = me.txtData
Open "Filename" For Output As #1
Write #1, varTextBox
Close #1

End Sub

The next bit you would place in the forms On_Open event.

Private Sub Form_Open(Cancel As Integer)
Dim varTextBox
Open "Filename" For Input As #1
Input #1, varTextBox
Close #1
me.txtData = varTextBox
End sub

does that explain it a little better?

Cheers
Marcus

As Ark said you must add referense to Microsoft Scripting Runtime (from menu - Project - Referenses.. - in dialog check Microsoft Scripting Runtime) If you don't have it can download it free from http://msdn.microsoft.com/scripting.
If New keyword is not valid for vb5 try:
Dim fsoFile As Object
in Form_Load:
Set fsoFile=CreateObject("Scripting.FileSystemObject")
ASKER CERTIFIED SOLUTION
Avatar of viklele
viklele

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of daky

ASKER

Thank you all! But, Vikele's answer I must accept. It really works excellent! If you have more you can leave for me. Again, Thank you very much!

Daky!
Gee thanks viklele