print data in txt file in tabular format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kkshansid
    New Member
    • Oct 2008
    • 232

    print data in txt file in tabular format

    Code:
    Dim rs As DAO.Recordset
    Dim sPath As String
    Function ITAX()
    sPath = "C:\temp\" ' just have the directory here
    Set rs = CurrentDb.OpenRecordset("SAL")
    Open sPath & "TAX.txt" For Output As #1
    Do Until rs.EOF
        EName = rs("name")
        Edesg = rs("desg")
        ecode = rs("ecode")
        Print #1, ecode & Space(5) & EName & Space(5) & Edesg
        rs.MoveNext
    Loop
    Close #1
    rs.Close
    Set rs = Nothing
    End Function
    this code displays same in below format which looks ugly.I want to display same in tabular format.such that name in one column designation in one columns in synchronization kindly advise how to print data in txt file in tabular format?
    Code:
    0189    SMT BIMLA             HEAD ASSTT
    0190    SH PARTAP SINGH BIST  HEAD ASSTT
    018     SMT BIM               HEAD ASSTT
    019     SH TAP SINGH BIST     HEAD ASSTT
    Last edited by NeoPa; Dec 12 '12, 04:16 PM. Reason: Reformatted display of data
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    You can Output to Print Zones and pad the middle String Variable with a fixed number of Spaces that you define, as in:
    Code:
    Dim rs As DAO.Recordset
    Dim sPath As String
    
    'User Defined CONSTANT
    Const conLENGTH As Byte = 20
    
    sPath = "C:\temp\" ' just have the directory here
    Set rs = CurrentDb.OpenRecordset("SAL")
    
    Open sPath & "TAX.txt" For Output As #1
    
    Do Until rs.EOF
      EName = rs("name")
      Edesg = rs("desg")
      ecode = rs("ecode")
        Print #1, Format$(ecode, "0000"), EName & Space$(conLENGTH - Len(EName)), Edesg
          rs.MoveNext
    Loop
    
    Close #1
    
    rs.Close
    Set rs = Nothing
    OUTPUT:
    Code:
    0189          SMT BIMLA                   HEAD ASSTT
    0190          SH PARTAP SINGH BIST        HEAD ASSTT
    0018          SMT BIM                     HEAD ASSTT
    0000          SH TAP SINGH BIST           HEAD ASSTT

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32645

      #3
      While I have no doubt that ADezii's solution is perfectly correct and very useful, it may help to know that the Print# command, by default, tabulates your data for you anyway. You are simply bypassing this functionality by joining all your data into a single string before sending it.

      If you use the comma (,) character to separate the values for each column it will tabulate automatically to the default column widths. If you want to tabulate to a specific column then this is also supported by using Tab(n) in your output stream, where n is the column number desired. You can also set the charpos for the next item to print and print a set number of spaces using Spc(n). Look in the help page for all this information.

      Fundamentally though, Print# was designed to support these features.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Print #1, ecode, EName, Edesg
        will produce:
        Code:
        189          SMT BIMLA     HEAD ASSTT
        190          SH PARTAP SINGH BIST        HEAD ASSTT
        18           SMT BIM       HEAD ASSTT
        0            SH TAP SINGH BIST           HEAD ASSTT
        Print #1, ecode; Tab(10); EName; Tab(35); Edesg
        although a little more confusing, will produce:
        Code:
        189     SMT BIMLA                HEAD ASSTT
        190     SH PARTAP SINGH BIST     HEAD ASSTT
        18      SMT BIM                  HEAD ASSTT
        0       SH TAP SINGH BIST        HEAD ASSTT
        Last edited by NeoPa; Dec 12 '12, 04:44 PM. Reason: Extraneous space had got into your first block.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32645

          #5
          Indeed. Clearly the second option is appropriate for this question, where the first isn't.

          Although, with a limited length (<8 chars) number value at the start you could get away with :
          Code:
          Print #1, ecode, EName, Tab(35); Edesg
          Only very variable fields need be handled by Tab(n).

          Comment

          Working...