?
Solved

open, read, change a text file

Posted on 2003-03-17
7
Medium Priority
?
202 Views
Last Modified: 2010-04-07
I have a program that I need to open a text file, read the values from it, take those values and do something, change the values and then close it.  Whats the code to open a text file and do those things using VBA?  Thanks a bunch.
0
Comment
Question by:fbyron
[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
  • 2
  • 2
  • +1
7 Comments
 

Expert Comment

by:DynAstY
ID: 8152519
Dim iNUM as integer
iNUM=FreeFile
Open "C:\Something" for input as #iNUM

read then

close #iNUM
0
 

Accepted Solution

by:
vincentregent earned 150 total points
ID: 8152793
Hi fbyron

You could try this:

'Put your file you want to use in a variable

Dim yourfile As string

'Then put the name of your file you want to create en use
'in that variable

yourfile="c:\thisismyfile.dat"

'for example, if you want to store the content of a
'textbox (text1.text)

Dim contentyoustore As string
contentyoustore = Text1.Text

'Finaly save this content in the file
'If the file doesn't exsist, VB creates it automaticly
' for you

Open yourfile For Output As 1
Print #1, contentyoustore
Close #1

'and now it is saved

'To open an use it again do this:

Open yourfile For Input As 1
Line Input #1, contentyoustore
Close #1

'for example you can put it back in a textbox like this:

Text1.Text = contentyoustore



'If you want to store numeric values I would advice to
' store and get them like this:

Open yourfile For Binary As 1
Put 1, 1, contentyoustore
Close 1

'and to open it again:

Open yourfile For Binary As 1
Get 1, 1, contentyoustore
Close 1



I hope this will help you,
Let me know !!

Greetings,

Vincent











0
 

Author Comment

by:fbyron
ID: 8154599
If I want to replace a line in a text file what do I do?  I tried print but it just erases everything in the file.
Its reading the file but I want to replace a line that reads Ben, 21 with Ben, 22.  thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fbyron
ID: 8154615
If I want to replace a line in a text file what do I do?  I tried print but it just erases everything in the file.
Its reading the file but I want to replace a line that reads Ben, 21 with Ben, 22.  thanks
0
 

Expert Comment

by:vincentregent
ID: 8158503
To replace a line in a text file as you use the open,print and close commands, you have 2 options.
1) You save the entire text as one, afterward, you load the entire text in a textbox (with multiple lines) ob better, in a embedded text editor.

But in you case I guess the second option is better,
because its easier to take out the values afterward:

2)
Open yourfile For Binary As 1
Put 1, 1, putyourfirstline
Put 1, 2, putyoursecondline
Put 1, 3, putyourthirdline
' and so on
Close 1

Like this, you should devide your text in different lines,
and store each line in the file your save (1, 2, 3,....)

When you want to change a value or line, you open that file again, get the line you need.  Put it in (for example) a textbox (so the user can change it), ar change the value yourself (in your code)

Good luck
Vincent
0
 

Expert Comment

by:R2DTOEA
ID: 8168207
Dim l1 as long, l2 as long
Dim strLine as string
Dim strReplaceLine as string, strNewLine as string
Dim strOriFile as string, strNewFile as string

'set strreplaceline and strnewline before this

'open the file to read from
l1 = freefile()
open strOriFile for input as l1
'open the file to print 2 - if it doesn't exist, this
'will create it
l2 = freefile()
open strNewFile for output as l2

'loop through the first file reading each line
do until eof(l1)
     line input #l1, strLine
     if strReplaceline = strLine then
          print #l2, strLine
     else
          print #l2, strNewLine
     end if
loop

'close both files
close #l1
close #l2

'Remove the original file and rename the new one to it
kill strorifile
name strnewfile as strorifile

There is another way to do this that is much faster as it reads the entire file all at once into a string.  Then you just use the replace command on the string to replace the line in question and then output the entire string to the file essentially overwriting what is alread there.
0
 

Expert Comment

by:R2DTOEA
ID: 8168239
'With this, you pass in the filename, and it returns the entire file as a string.  See the property get below.

'With the returned string from the property get, just do a replace:
strFileData = replace(strFileData, strReplaceLine, strNewLine)
'Then output it - the open output will clear the entire file:
Dim l1 as long
l1 = freefile()
open strFileName for output as l1
print #l1, strFileData
close #l1

Property Get ReadFileToString(ByVal strFileName As String) As String
    Dim lHandle As long
   
    ' open in binary mode
    lHandle = FreeFile
    Open strFileName$ For Binary As #lHandle
    ' read the string and close the file
    ReadFileToString = Space$(LOF(lHandle))
    Get #lHandle, , ReadFileToString
    Close #lHandle
End Property
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

770 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