Maintain same length of string in text file

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Maintain same length of string in text file

Post by sal21 »

I print in a text file with a for next loop this string:

Code: Select all

        Print #1, DATAOP1 & " " & ORAOP1 & " " & TIPASS1 & " " & NRASS1 & " " & IMPORTOASS1 & " " & ABI1 & " " & CAB1 & " " & EMISS1 & " " & NT1 & " " & SPORTOP1
but have a big prob!!!

Many variable dont have the same lenght!!!!

For example:

IMPORTOASS1 it can assume 1.1200,14 or 15,14 ....

naturally when i write the line the orizonthal position are different:

......1200,14....
......15,14....

i need:
......1200,14....
.........15,14....

User avatar
HansV
Administrator
Posts: 78643
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Maintain same length of string in text file

Post by HansV »

Let's say you want 10 positions for the first field, 6 for the second field, 12 for the third field, etc. You can do something like this:

Code: Select all

    Dim strLine As String
    strLine = Space(10 - Len(DATAOP1)) & DATAOP1
    strLine = strLine & Space(6 - Len(ORAOP1)) & ORAOP1
    strLine = strLine & Space(12 - Len(TIPASS1)) & TIPASS1
    strLine = strLine & Space(9 - Len(NRASS1)) & NRASS1
    strLine = strLine & Space(13 - Len(IMPORTASS1)) & IMPORTASS1
    ...
    Print #1, strLine
Adjust the lengths 10, 6, 12, 9, 13, ... as needed.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Re: Maintain same length of string in text file

Post by sal21 »

HansV wrote:Let's say you want 10 positions for the first field, 6 for the second field, 12 for the third field, etc. You can do something like this:

Code: Select all

    Dim strLine As String
    strLine = Space(10 - Len(DATAOP1)) & DATAOP1
    strLine = strLine & Space(6 - Len(ORAOP1)) & ORAOP1
    strLine = strLine & Space(12 - Len(TIPASS1)) & TIPASS1
    strLine = strLine & Space(9 - Len(NRASS1)) & NRASS1
    strLine = strLine & Space(13 - Len(IMPORTASS1)) & IMPORTASS1
    ...
    Print #1, strLine
Adjust the lengths 10, 6, 12, 9, 13, ... as needed.
IF i have undesrtand...
Before to set the space, i need to consider the max possible lenght for each Variable, or not?

User avatar
HansV
Administrator
Posts: 78643
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Maintain same length of string in text file

Post by HansV »

Yes, you have to know the maximum number of characters you'll need for each variable.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Re: Maintain same length of string in text file

Post by sal21 »

HansV wrote:Yes, you have to know the maximum number of characters you'll need for each variable.
tks :thankyou: :clapping:

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Re: Maintain same length of string in text file

Post by sal21 »

HansV wrote:Let's say you want 10 positions for the first field, 6 for the second field, 12 for the third field, etc. You can do something like this:

Code: Select all

    Dim strLine As String
    strLine = Space(10 - Len(DATAOP1)) & DATAOP1
    strLine = strLine & Space(6 - Len(ORAOP1)) & ORAOP1
    strLine = strLine & Space(12 - Len(TIPASS1)) & TIPASS1
    strLine = strLine & Space(9 - Len(NRASS1)) & NRASS1
    strLine = strLine & Space(13 - Len(IMPORTASS1)) & IMPORTASS1
    ...
    Print #1, strLine
Adjust the lengths 10, 6, 12, 9, 13, ... as needed.
ops! prob with this code to align the first var:

Code: Select all

.....
Dim strLine As String, NR As String
    
    SQL = "SELECT Count(ASS_MF_REPORT.RAPPORTO) AS ConteggioDiRAPPORTO, ASS_MF_REPORT.SPORT_CLIENTE, ASS_MF_REPORT.DESCR_AGENZIA FROM ASS_MF_REPORT GROUP BY ASS_MF_REPORT.SPORT_CLIENTE, ASS_MF_REPORT.DESCR_AGENZIA"
    Set RS = CONN.Execute(SQL)
    
    Y = RS.RecordCount
    Erase strDBRows()
    strDBRows = RS.GetRows(Y)
    RS.Close
    Set RS = Nothing

    With Me.SPORT
        .Clear
        For X = 0 To UBound(strDBRows, 2)
        NR = Trim$(Format$(strDBRows(0, X), "#,##0 "))
        strLine = Space(3 - Len(NR)) & NR
            .AddItem Format$(strLine, "#,##0 ") & " - " & Format$(strDBRows(1, X), "#0000") & " - " & strDBRows(2, X)
        Next X
    End With
.....
see image ...
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78643
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Maintain same length of string in text file

Post by HansV »

That's silly - first, you create a fixed length string:

NR = Trim$(Format$(strDBRows(0, X), "#,##0 "))
strLine = Space(3 - Len(NR)) & NR

and then you apply the format again so that the length becomes variable:

.AddItem Format$(strLine, "#,##0 ") & ...
Best wishes,
Hans