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
Solved

How to run find and replace command in a text file from VBA?

Posted on 2015-01-04
7
406 Views
Last Modified: 2015-01-05
Hi Experts,
I am interesting in a way to open from my Access app a text file and replace all commas, then save the document.
0
Comment
Question by:bfuchs
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 400 total points
ID: 40530615
you can create  a new text file "myFile2.txt" without the commas

dim s as string
open "c:\folderName\myFile.txt" for input as #1
open "c:\folderName\myFile2.txt" for output as #2
do until eof(1)
line input #1, s
s=replace(s,",",":")           'change ":" with the replacement you want

print #2, s
loop
close #1
close #2
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40531906
Hi Ben,
I don't think you worded your question quite right.
You have a fixed width column text file that has some data that has commas in it.
This prevents you from importing the file directly as a csv file.

Rey has provided you with a solution that will replace all commas with colons and save the file with a new name.
Which is what you asked for in this Q

If you still remain interested in opening a fixed width column text file and processing the results line-by-line into fields and records, let me know.

Nick67
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40531978
...yes, please clarify:
"Replace" all commas with ...?
...or "Delete" all commas...
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 4

Author Comment

by:bfuchs
ID: 40532175
@Ray,
Your solution works, just wonder if there is really a need to create a new document or this can be done in same document?

@Nick67,
For that I am happy with the solution provided in other thread, just wonder if what you had posted there could work for this as well?

@Jeff,
For what I am concerned now, it wouldn't matter, either way will work.

Thanks,
Ben
0
 
LVL 26

Assisted Solution

by:Nick67
Nick67 earned 100 total points
ID: 40532201
just wonder if what you had posted there could work for this as well
Yes, the principle is pretty much universal.
You open the file, and can then proceed to read it line by line, and do whatever you would like with each line in VBA
Rey's solution here does the exact same things, but uses a different, less verbose, syntax than what I posted in your earlier question.

is really a need to create a new document
Both Rey's idea and mine require saving the read data to a new location.
But there is really very little involved with killing the source file and replacing it with the new file so in the end only the edited results exist
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 400 total points
ID: 40532210
<Your solution works, just wonder if there is really a need to create a new document or this can be done in same document?>

if you want to retain the same document..

after running my codes
kill/delete the original document with

kill  "c:\folderName\myFile.txt"

'then name the new file with the name of the old file

name  "c:\folderName\myFile2.txt" as  "c:\folderName\myFile.txt"
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 40532241
Thanks Experts!
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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

861 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