Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

There is any function to replace spaces to tabs

Status
Not open for further replies.

Gti

Programmer
Jul 23, 2001
99
PT
I have a Txt file whith 4 coluns and between there are spaces.
(e.g
Txt File
--------------------------------------------------------
XXxxxxx Yyyyyyyy Oooooooo Sssssssss
Sssssss Oooooooo Yyyyyyyy XXxxxxxxx
--------------------------------------------------------

What i wont is replace de spaces between the coluns to tabs. How can i do it?

Any help

tkx ;-)
 
The following code will do what you want.

I have used an Access table containing two fields, one for your data, and the second for the replaced data (group of spces replaced with a Tab) data to achieve the result.

The table details I have used are :

TableName = YourTable

Fields YourFieldName (Text, 255)
ReplacedField (Text, 255)

You can change the above to what you want and make the appropriate changes in the code I have forwarded to you.


IMPORT your text file data into the field "YourFieldName" and then run the following. The results you want will end up in the field "ReplacedField"


Public Function Replace_Spaces_With_Tabs()

On Error GoTo Error_in_Replacement

Dim db As Database
Dim rst As Recordset

Dim FieldLength As Long
Dim StartPoint As Long
Dim CharacterToReplace As Long

Dim NewField As String
Dim SingleCharacter As String * 1

Set db = CurrentDb
Set rst = db.OpenRecordset("YourTable")

rst.MoveFirst

Do Until rst.EOF = True
FieldLength = Len(rst!Yourfieldname)
NewField = " "
StartPoint = 1

' Replace each group of spaces with a single Tab character

Do Until StartPoint > FieldLength
CharacterToReplace = InStr(StartPoint, rst!Yourfieldname, " ")
If CharacterToReplace = 0 Then
' Nothing more to replace - so build last input group and exit

NewField = NewField & Mid(rst!Yourfieldname, StartPoint, (FieldLength - StartPoint) + 1)
StartPoint = FieldLength + 1
Else

' Build current group of characater ready for output with a TAB replacing the group of spaces

NewField = NewField & Mid(rst!Yourfieldname, StartPoint, (CharacterToReplace - StartPoint))
NewField = NewField & vbTab

' Now look for the next input character that is NOT a space

SingleCharacter = " "
StartPoint = CharacterToReplace
Do Until SingleCharacter <> &quot; &quot; Or StartPoint > FieldLength
StartPoint = StartPoint + 1
SingleCharacter = Mid(rst!Yourfieldname, StartPoint, 1)
Loop

End If
Loop

' Update second field within current record with the replaced details

rst.Edit
rst!ReplacedField = LTrim(NewField)
rst.Update

' Get next input record

rst.MoveNext
Loop

rst.Close
db.Close

Exit_This_Function:

Exit Function

Error_in_Replacement:

MsgBox &quot;Error detected&quot;

Resume Exit_This_Function

End Function
 


Hi Gti

If you want a simple answer - then you could import the file and then export it as tab delimited file.

Stew &quot;Even a stopped clock tells the right time twice a day.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top