?
Solved

read and write a string into a txt file.

Posted on 2006-11-20
4
Medium Priority
?
2,936 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 17983909
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
ID: 17995692
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
ID: 17996236
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
ID: 17996297
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

649 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