Solved

read and write a string into a txt file.

Posted on 2006-11-20
4
2,932 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

733 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