Solved

read and write a string into a txt file.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create a duplicate finder Application 9 115
passing parameter in sql procedure 9 56
Spell Check in VB6 13 98
VB6 ListBox Question 4 33
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

920 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