Advertisement

03.06.2008 at 06:13PM PST, ID: 23221861
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

MS Access 2003:  Find character in Text File and Replace with another character

I need to find (2 spaces) and replace with (1 space) in a text file before using a TransferText on it through code or queries.  How would I do this?
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: deer777
Solution Provided By: aesmike
Participating Experts: 4
Solution Grade: A
Views: 76
Translate:
Loading Advertisement...
03.06.2008 at 07:04PM PST, ID: 21066996

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.06.2008 at 07:10PM PST, ID: 21067035

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.06.2008 at 07:25PM PST, ID: 21067092

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.06.2008 at 08:18PM PST, ID: 21067312

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.06.2008 at 08:21PM PST, ID: 21067324

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 04:38AM PST, ID: 21069373

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 05:27AM PST, ID: 21069716

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 07:46AM PST, ID: 21071244

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 07:52AM PST, ID: 21071319

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 08:14AM PST, ID: 21071589

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.06.2008 at 07:04PM PST, ID: 21066996
Sounds like a familiar situation.  Do you have situations where there are exactly two spaces and you want to strip it down to one?  Or are you trying to shrink down multiple spaces to only one space?  
If it's the former, you can create a query that uses the REPLACE function for each field you ware ultimately exporting.
e.g. SELECT CustomerID, REPLACE([CustomerName],"  "," ") as CName INTO <NEW TABLE> etc, etc, etc.

Run this query, create the table, then do the transfertext command that you need to do.
 
03.06.2008 at 07:10PM PST, ID: 21067035
you can use the getobject function open word with a macro and do a search and replace then save the file back out as a text file
here's a snipit of code for the getobject function
' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName as String, _
                    ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd as Long,ByVal wMsg as Long, _
                    ByVal wParam as Long, _
                    ByVal lParam As Long) As Long

Sub GetWord()
    Dim Mydoc As Object    ' Variable to hold reference
                                ' to Microsoft Word.
    Dim WordWasNotRunning As Boolean    ' Flag for final release.

' Test to see if there is a copy of Microsoft Word already running.
    On Error Resume Next    ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
    Set Mydoc = Getobject(, "Word.Application")
    If Err.Number <> 0 Then WordWasNotRunning = True
    Err.Clear    ' Clear Err object in case error occurred.

' Check for Microsoft Word. If Microsoft Word is running,
' enter it into the Running Object table.
    DetectWord

' Set the object variable to reference the file you want to see.
    Set Mydoc = Getobject("c:\vb4\MYTEST.doc")

' Show Microsoft Word through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the Mydoc object reference.
    Mydoc.Application.Visible = True
    Mydoc.Parent.Windows(1).Visible = True
     Do manipulations of your  file here.
    ' ...
' If this copy of Microsoft Word was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Word, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
    If WordWasNotRunning = True Then
        Mydoc.Application.Quit
    End IF

    Set Mydoc = Nothing    ' Release reference to the
                                ' application and spreadsheet.
End Sub

Sub DetectWord()
' Procedure dectects a running Word and registers it.
    Const WM_USER = 1024
    Dim hWnd As Long
' If Word is running this API call returns its handle.
    hWnd = FindWindow("docMAIN", 0)
    If hWnd = 0 Then    ' 0 means Word not running.
        Exit Sub
    Else                
    ' Word is running so use the SendMessage API
    ' function to enter it in the Running Object Table.
        SendMessage hWnd, WM_USER + 18, 0, 0
    End If
End Sub


Build a series of loops that walk through the text file character by character comparing the previous to characters to two spaces, if the condition is true, then replace with one space, and advance to the next character until EOL or EOF condition is true.
 
03.06.2008 at 07:25PM PST, ID: 21067092
doh.  you want to READ in the text file.
This will read in the text file line-by-line, replace two spaces with one space (recursively), and write the contents out to a new text file.

You then read in the new text file using TransferText
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
Dim handle As Integer
    Dim outhandle As Integer
    Dim infilename As String
    Dim outfilename As String
    Dim buffer As String
    
    infilename = "MyOriginalTextFile.txt"
    outfilename = "ConvertedTextFile.txt"
 
    handle = FreeFile
    Open infilename For Input As handle
    
    outhandle = FreeFile
    Open (outfilename) For Output As outhandle
    
    Do While EOF(handle) = False
        Line Input #handle, buffer
        Do While InStr(buffer, "  ") > 0
            buffer = Replace(buffer, "  ", " ")
        Loop
        Print #outhandle, buffer
        
    Loop
    
    Close (handle)
    Close (outhandle)
Open in New Window
Accepted Solution
 
03.06.2008 at 08:18PM PST, ID: 21067312

Rank: Guru

Another way, using Regular Expressions:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
Option Explicit 
Sub CleanUpFile() 
    Dim fso As Object, ts As Object, WholeFile As String
    
    Const filepath As String = "c:\folder\subfolder\file.txt"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(filepath)
    WholeFile = ts.ReadAll
    ts.Close
    
    Set ts = fso.CreateTextFile(filepath, True)
    ts.Write RegExpReplace(WholeFile, " {2,}")
    ts.Close
    
    Set ts = Nothing
    Set fso = Nothing
    
    MsgBox "Done"
    
End Sub 
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True) 
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll controls
    ' whether all instances of the matched string are replaced (True) or just the first instance (False)
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    Dim RegX As Object
    
    Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
    Set RegX = Nothing
    
End Function
Open in New Window
 
03.06.2008 at 08:21PM PST, ID: 21067324

Rank: Guru

Or, if you prefer line by line:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
Option Explicit 
Sub CleanUpFile() 
    Dim fso As Object, ts1 As Object, ts2 As Object
    
    Const InputFile As String = "c:\folder\subfolder\file.txt"
    Const OutputFile As String = "c:\folder\subfolder\clean_file.txt"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts1 = fso.OpenTextFile(InputFile)
    Set ts2 = fso.CreateTextFile(OutputFile)
    
    Do Until ts1.AtEndOfStream
        ts2.WriteLine RegExpReplace(ts1.ReadLine, " {2,")
    Loop
    
    ts1.Close
    ts2.Close
    
    Set ts1 = Nothing
    Set ts2 = Nothing
    Set fso = Nothing
    
    MsgBox "Done"
    
End Sub 
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True) 
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll controls
    ' whether all instances of the matched string are replaced (True) or just the first instance (False)
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    Dim RegX As Object
    
    Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
    Set RegX = Nothing
    
End Function
Open in New Window
 
03.07.2008 at 04:38AM PST, ID: 21069373
MatthewsPatrick:

I am getting a "file not found" error message on

     set ts1 = fso.OpenTextFile(Inputfile)
 
03.07.2008 at 05:27AM PST, ID: 21069716

Rank: Guru

deer777,

You need to edit the constant values to show the original file path and name (InputFile), and where
you want the results saved (OutputFile).  Did you do that?

Regards,

Patrick
 
03.07.2008 at 07:46AM PST, ID: 21071244
I did that and now I am getting the error:

Application-defined or object-defined error:

RegExpReplace = RegX.Replace(Lookin, ReplaceWith)
 
03.07.2008 at 07:52AM PST, ID: 21071319
I've had problems with getting the Regular Expression thing to work with FSO.
my solution loops through and simply replaces two spaces for one.  Simple, but effective.
 
03.07.2008 at 08:14AM PST, ID: 21071589
AESMIKE:

I tried your sample earlier and received this error:

Compile error:
Variable not defined

Handle
 
 
05.19.2008 at 01:27PM PDT, ID: 21601264
Another way to deal with this problem is very simple.  (First, as always, copy your table!)  Open the table in which the field you are referencing is housed.  Put your cursor in the beginning of the first record of the field you need cleaned up.  Go to Edit, Find/Replace.  Click on Replace.  In the "Find" field, type two spaces with the space bar.  In the "Replace with" field, type one space.  Make sure that you change the Match field to "Any part of field".  Try it out with the Search and Replace keys or go ahead and hit Replace All.  You will get a count of how many replacements were made at the end.  This is the fastest and simplest way to take care of this problem.
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628