Go Premium for a chance to win a PS4. Enter to Win

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

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
0
daky
Asked:
daky
  • 3
  • 3
  • 2
  • +3
1 Solution
 
xSinbadCommented:
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
0
 
ArkCommented:
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
0
 
BlackSharkCommented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
dakyAuthor Commented:
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)
0
 
dakyAuthor Commented:
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.
0
 
ArkCommented:
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
0
 
glass_cookieCommented:
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 : )
0
 
xSinbadCommented:
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

0
 
BlackSharkCommented:
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")
0
 
vikleleCommented:
Your Q is not related to API functions, VB has built-in functions to provide the functionality that you are looking for. Here we go :

On a form, put a textbox (Text1) where you can key-in your stuff, and put a command button (Command1).

Use the following code to store textbox contents to a file c:\temp\demo.txt.

--------------------------------------------------------
Option Explicit

'save contents of text1 to a file
Private Sub Command1_Click()
Dim hFile%

   hFile = FreeFile
   Open "c:\temp\demo.txt" For Output As #hFile
   Print #hFile, Text1.Text
   Close #hFile
   
End Sub

'reload contents at start up
Private Sub Form_Load()
Dim hFile%
Dim sData$

   Text1.Text = ""
   If(Dir$("c:\temp\demo.txt", vbNormal) <> "") then
      hFile = FreeFile
      Open "c:\temp\demo.txt" For Input As #hFile
      Do While Not EOF(hFile)    ' Loop until end of file.
         Input #1, sData
         If (Len(Text1.Text) = 0) Then
            Text1.Text = sData
         Else
            Text1.Text = Text1.Text & vbCrLf & sData
         End If
      Loop
      Close #hFile
   End If

End Sub
--------------------------------------------------------

This should do the trick.
0
 
dakyAuthor Commented:
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!
0
 
xSinbadCommented:
Gee thanks viklele
0

Featured Post

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.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now