Trailing commas (1 Viewer)

aziz rasul

Active member
Local time
Today, 12:29
Joined
Jun 26, 2000
Messages
1,935
Using

Code:
DoCmd.TransferText acExportDelim, "tbl001 MRHDATA Export Specification", "qryTemp", strFolder & strFilename, False

I have the following header record.

H,PRODMASTFULL,4,2017-04-19 15:16:00,PRODUCT MASTER FULL,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

How do I get rid of the trailing commas using code?
 

RuralGuy

AWF VIP
Local time
Today, 05:29
Joined
Jul 2, 2005
Messages
13,825
I would use a function like:
Code:
Public Function DelTrailing(InString As String) As String

DelTrailing = Left(InString, InStr(InString, ",,") - 1)

End Function
 

aziz rasul

Active member
Local time
Today, 12:29
Joined
Jun 26, 2000
Messages
1,935
I wanted to know how to do this in the text file itself using code in Ms Access. After the changes save the file.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:29
Joined
Feb 28, 2001
Messages
27,332
aziz, that would depend on what you are using for the file. If it is a WORD file, for example, the code might appear one way, while if it is NOTEPAD it would look another way. What is your context for the text file?

By the way, Allan's suggest is well-founded. If the problem occurs because of something you do in Access, you should try to fix it in Access. I.e. where you can do so, fix something where it occurs, not after-the-fact. The farther away you get from the error, the harder it becomes to fix.
 

aziz rasul

Active member
Local time
Today, 12:29
Joined
Jun 26, 2000
Messages
1,935
I create a header record on the first row in the first field in a table. There follows about 100,000 records followed by a trailing record agin in the first field field. When I use TransferText I get trailing commas in the resulting txt file in the header and trailing rows. I'm trying to get rid of the trailing commas.

If there is a way doing this at source that would be good but can't how. I was hoping to point to the first and last record in the txt file and resolve it that way.
 

isladogs

MVP / VIP
Local time
Today, 12:29
Joined
Jan 14, 2017
Messages
18,258
I would use a function like:
Code:
Public Function DelTrailing(InString As String) As String

DelTrailing = Left(InString, InStr(InString, ",,") - 1)

End Function

That's rather neat though it will trip up where there was a space in the string of commas
 

static

Registered User.
Local time
Today, 12:29
Joined
Nov 2, 2015
Messages
823
Code:
    Const filepath As String = "C:\test.txt"
    
    s = CreateObject("scripting.filesystemobject").opentextfile(filepath, 1).readall
    s = Split(s, vbNewLine)
    
    i = InStr(s(0), ",,")
    If i Then
        s(0) = Left(s(0), i-1)
        s = Join(s, vbNewLine)
        CreateObject("scripting.filesystemobject").opentextfile(filepath, 2).write s
    End If
 

aziz rasul

Active member
Local time
Today, 12:29
Joined
Jun 26, 2000
Messages
1,935
static, your code works great for the header record. How would I modify the code for the last record? There are about 100,000 records between the first and last records.
 

static

Registered User.
Local time
Today, 12:29
Joined
Nov 2, 2015
Messages
823
The code reads the file contents into a string and splits it into an array.
Each line of the array is a line in the file.
Arrays usually start at index 0, so s(0) is the first line.
They don't always start at 0 and if you aren't sure you can check using LBound(array) (lower bound).

To find and change the last line you need to know how many items are in the array. For that you get the upper bound, UBound(array).

Code:
Const filepath As String = "C:\test.txt"

s = CreateObject("scripting.filesystemobject").opentextfile(filepath, 1).readall
s = Split(s, vbNewLine)

i = InStr(s(0), ",,")
If i Then s(0) = Left(s(0), i-1)

j = InStr(s(ubound(s)), ",,")
If j Then s(ubound(s)) = Left(s(ubound(s)), j-1)

if i or j then
    s = Join(s, vbNewLine)
    CreateObject("scripting.filesystemobject").opentextfile(filepath, 2).write s
end if
 

Users who are viewing this thread

Top Bottom