Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

Formatting a detail line while printing with FileSystemObject?

I am creating a .txt file. I need to have each column fixed length. I am using the FileSystemObject where my final detail line is produced by the following line of code:

   strLine = strLocID  & "   " & strVendID & "   " & strDateTime & "   " & strProdID & "   " & strPrice
   ts.WriteLine strDetailLine

The file is created with no problem, however, the columns, row for row, do not line up exactly...meaning each column does not start in the exact column each time.

How can I fix this?

1 Solution
I guess you need a function to pad your strings with spaces.
b0lsc0ttIT ManagerCommented:

If this is a plain text file then that must mean that the information in the "columns" are not the same length.  Either that or the text reader you are using is using a variable width font.  Does the length of the content differ (i.e. sometimes 8 characters other times 12 characters)?  Will the file be read by a person or are you making it for something else?  You seem to be using ASP/vbscript but please confirm that.

If this has to be a text file and be formatted visually (i.e. csv file won't work) then you may have to fix this in the text reader.  Your answers to my questions will help me verify this though.

Let me know if you have any questions or need more information.

I use 'space' when i'm building a flat file, for text items, and 'format' for numerical and date .  I plug the maximum number of characters for 'x'.

Space(x - Len(variable))

strLine = space(x - len(strLocID))  & "   " & space(x - len(strVendID)) & "   " & format(strDateTime,"yyyy/mm/dd") & "   " & space(x-len(strProdID)) & "   " & format(strPrice,"0000000000.00")

For the price, determine a max number of digits and decimals required.

Hope this helps.

BlakeMcKennaAuthor Commented:
Ok, now things have changed. I am putting each data element into a Type array (which is defined in a .bas module) and I'm not getting a "Subscript out of range" error. I also need to sort this collection by the strLocID and I have no idea how to do this. Any ideas? Here is my code:

.bas Module Code:

Public Type DetailLine
    tLocID As String * 10
    tVendID As String * 16
    tDateTime As String * 13
    tProdID As String * 15
    tPrice As String * 8
End Type
Public typPNECDetail() As DetailLine

Form Module Code:

    idx = 0
    For Each objBlock In ReqColl
        For Each objProd In objBlock.Products
            Dim i As Integer
            For x = 0 To UBound(typAreas)
                If objBlock.AreaID = typAreas(x).AreaID Then
                    For i = 0 To UBound(typLocation)
                        If objBlock.Location = typLocation(i).advLocName Then
                            typPNECDetail(idx).tLocID = typLocation(i).pdsLocID             <<<<< subscript error >>>>>
                            Exit For
                        End If
                    Next i
                    For i = 0 To UBound(typVendor)
                        If objBlock.Vendor = typVendor(i).advVendorName Then
                            typPNECDetail(idx).tVendID = typVendor(i).pdsVendorID
                            Exit For
                        End If
                    Next i
                    For i = 0 To UBound(typProducts)
                        If objProd.Product = typProducts(i).advProdName Then
                            typPNECDetail(idx).tProdID = typProducts(i).pdsProdCode
                            Exit For
                        End If
                    Next i
                    typPNECDetail(idx).tDateTime = Format(objProd.EffOnDateTime, "mmddyy hh:mm")
                    typPNECDetail(idx).tPrice = objProd.Price
                    ts.WriteLine typPNECDetail(idx).tLocID & typPNECDetail(idx).tVendID & typPNECDetail(idx).tDateTime & 
                                      typPNECDetail(idx).tProdID & typPNECDetail(idx).tPrice
                    idx = idx + 1
                    Exit For
                End If
            Next x

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now