Solved

read and write a string into a txt file.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 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…

813 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

11 Experts available now in Live!

Get 1:1 Help Now