Solved

read and write a string into a txt file.

Posted on 2006-11-20
4
2,928 Views
Last Modified: 2013-12-26
I want a vb command that saves values from text box or a string into a txt file and also reads that line back into vb code.

Thank you.
0
Comment
Question by:Aiysha
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
Are storing only ONE value in the text file?...and then reading that one line back into a TextBox?

If so, then here is an example that persists a Textboxes value across runs:

Option Explicit

Private fileName As String

Private Sub Form_Load()
    fileName = "c:\someFile.txt"
    Call cmdRead_Click
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call cmdSave_Click
End Sub

Private Sub cmdRead_Click()
    If Dir(fileName) <> "" Then
        Dim ff As Integer
        Dim value As String
       
        ff = FreeFile
        Open fileName For Input As #ff
        Line Input #ff, value
        Close #ff
       
        Text1.Text = value
    End If
End Sub

Private Sub cmdSave_Click()
    Dim ff As Integer
       
    ff = FreeFile
    Open fileName For Output As #ff
    Print #ff, Text1.Text
    Close #ff
End Sub
0
 
LVL 16

Expert Comment

by:JohnBPrice
Comment Utility
From your question, it sounds like you might just want to save settings.  Idle_Mind's way is one, and you get to specify the file name.  Another way is to save it in the registry, using something like

Sub GetSettings()
    txtMyTextField.Text = GetSetting(App.EXEName, "Settings", "MyTextField", "")
End Sub

Sub SaveSettings()
   Call SaveSetting(App.EXEName, "Settings", "MyTextField", txtMyTextField.Text)
End Sub

and then call GetSettings from your form load or wherever, and SaveSettings from the change event or the form close event or wherever.

If you want to specify the file name, you could use the windows API calls GetPrivateProfileString and WritePrivateProfileString, which will create and read a separate .INI file that you specify.
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
Instead using a text file for storing some data you can
use the registry very easily to store short informations :


This sample creates the following key in the registry :
HKCU\Software\VB and VBA Program Settings\Project1\Settings

Under "HKCU\...\Project\" it creates the additional key "Settings" with the value
"Hey,I love Microsoft"




Private Sub Form_Load()
zzz = "Hey,I love Microsoft"

'S A V I N G :
'= = = = = = = =
'Set some data to registry
SaveSetting App.Title, "Settings", "firmName", zzz '  "App.Title" contains the variable  "Project1"


'R E A D I N G :
'= = = = = = = =
'read some data from registry
qqq = GetSetting(App.Title, "Settings", "firmName", "sorry, but the registry entry 'firmName' doesn't exist")
MsgBox qqq
' RESULT : a message box pops up with the text - - -> Hey,I love Microsoft
End Sub




'Annotation:
'The last parameter of the GetSetting() function allows to supply a DEFAULT value
'if the requested registry entry is not found...

'Counter = GetSetting(App.Title, "Settings", "Counter", "-1")
'If Counter = "-1" Then        ' there was no value found in registry
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
This sample stores the textbox content
(Text1.text) in the registry.

-Everytimes when you restart the vb program, you will see
the text1.text from your  'last session' .



Needs:
======
1 Textbox
1 Form


Private Sub Form_Load()
Text1.Width = 3000

If zz1 = "-1" Then
    Text1 = "Please enter some text"
Else
    Text1 = zz1
End If


End Sub



Private Sub Text1_Change()
zz9 'store text1 content
End Sub



Function zz1() As String   ' read
    zz1 = GetSetting(App.Title, "Settings", "textbox1", "-1")
End Function



Sub zz9()    ' save
      SaveSetting App.Title, "Settings", "textbox1", Text1.Text
End Sub
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now