?
Solved

data formatting problem

Posted on 2005-04-12
9
Medium Priority
?
153 Views
Last Modified: 2010-05-18
The following code produces the output shown below. My problem is that everything is not aligned properly. I want all the numbers to be presented in standard columns like excel. However I just can't do that because of the difference in number widths like 1,75,751. Can somebody look at the code below and suggest something. I can't use flexigrid etc because the number of rows in the output might go to 1000.
___________________________________________________________________________________
Dim fso As New FileSystemObject
    Dim fsoStream As TextStream
    ' Create a text file, and return a reference to a TextStream
    Set fsoStream = fso.CreateTextFile("c:\ppcout.txt", True)
    ' Write to the file
   
   
    For i = 1 To period
    fsoStream.WriteLine _
    Str(demand(i)) + "     " + Str(a1(i)) _
  + "     " + Str(a2(i)) + "     " + Str(a3(i)) _
  + "     " + Str(a4(i)) + "     " + Str(a5(i))
 
   
    fsoStream.WriteBlankLines 2
    Next
   
   
    fsoStream.WriteLine ("-----------------------------")
    fsoStream.WriteLine ("Cost Matrix")
    fsoStream.WriteLine ("-----------------------------")
    fsoStream.WriteBlankLines 2
   
    For i1 = 1 To 5
    fsoStream.WriteLine Str(Module1.totalcost(i1))
    fsoStream.WriteBlankLines 1
    Next
   
     fsoStream.Close
    Set fsoStream = Nothing
    Set fso = Nothing

_________________________________________

 75      75      75      108      75      75


 0      0      0      0      0      0


 33      71      61      0      70      71


 28      0      0      38      0      0


 0      0      0      0      0      0


 10      0      10      0      70      0


-----------------------------
Cost Matrix
-----------------------------


 258

 328

 286

 424

 258
0
Comment
Question by:anshuma
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13768592
Hi anshuma,

try

String.Format ("This is a test {0} and another {1}",Arg1,arg2)

Cheers!
0
 
LVL 27

Expert Comment

by:Dabas
ID: 13768630
From VB6 help on User-Defined String Formats (Format Function)

------------
You can use any of the following characters to create a format expression for strings:

Character Description
@ Character placeholder. Display a character or a space. If the string has a character in the position where the at symbol (@) appears in the format string, display it; otherwise, display a space in that position. Placeholders are filled from right to left unless there is an exclamation point character (!) in the format string.
....
------
In your case, I would suggest using something like:
fsoStream.WriteLine(Format(Str(demand(i)),"@@@@@@@@@@@@") + Format(Str(a1(i),"@@@@@@@@@@@") + .... etc

Dabas
0
 
LVL 1

Expert Comment

by:aryaomni
ID: 13769688
instead of using any fixed characters use vbtab

    fsoStream.WriteLine _
    Str(demand(i)) + vbTab + Str(a1(i)) _
  +  vbTab + Str(a2(i)) +  vbTab + Str(a3(i)) _
  +  vbTab + Str(a4(i)) +  vbTab + Str(a5(i))

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13769721
Tabs are no good..

because you'll get the following

   100
   20

But if you use Format str you'l get
   100
    20

which looks better
0
 
LVL 1

Assisted Solution

by:aryaomni
aryaomni earned 400 total points
ID: 13769765
But
the TextMetric gives diffterent character width for different characters . i.e width of "i" is less than width of "W" . charcter stuffinf will not give you proper results. also You can not predict which type of font the viewer of your text file . so its better to use tab .

in this case you can use this way
  fsoStream.WriteLine _
    Str(demand(i)) + vbTab + space( constFixedLength - len(Str(a1(i)) ) + Str(a1(i))

space(constFixedLength ) or space$(constFixedLength ) returns
constFixedLength sized string containing spaces


0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13769794
Well to be honest its a personal preference more than a must do it this way!!

Tabs or no Tabs the characters will still end up screwing the tab side of things anyway.

Unless you can force fixed width fonts...

     WWW     EEE
     III    JJJ

Will display differently and unaligned, with tabs or without tabs...
0
 
LVL 14

Accepted Solution

by:
Shiju Sasidharan earned 1600 total points
ID: 13769835
try this

'===============
Private Sub Command1_Click()
Dim fso As New FileSystemObject
Dim fsoStream As TextStream
Dim sAlign As String
Dim sTab As String

    Set fsoStream = fso.CreateTextFile("c:\ppcout.txt", True)
    sTab = LAlign(" ", 5)
    For i = 1 To period
       sAlign = LAlign(demand(i), 6) & sTab & LAlign(a1(i), 6) & sTab & LAlign(a2(i), 6) & sTab & LAlign(a3(i), 6) & sTab & LAlign(a4(i), 6) & sTab & LAlign(a5(i), 6)
       fsoStream.WriteLine sAlign
       fsoStream.WriteBlankLines 2
    Next
   
    fsoStream.WriteLine ("-----------------------------")
    fsoStream.WriteLine ("Cost Matrix")
    fsoStream.WriteLine ("-----------------------------")
    fsoStream.WriteBlankLines 2
   
    For i1 = 1 To 5
        fsoStream.WriteLine Str(Module1.totalcost(i1))
        fsoStream.WriteBlankLines 1
    Next
    fsoStream.Close
    Set fsoStream = Nothing
    Set fso = Nothing
End Sub
Public Function LAlign(ByVal StringToAlign As String, ByVal MaxSpace As Integer) As String
Dim iCount As String
    StringToAlign = Left(StringToAlign, MaxSpace)
    If MaxSpace - Len(StringToAlign) > 0 Then _
        StringToAlign = StringToAlign & Space(MaxSpace - Len(StringToAlign))
    LAlign = StringToAlign
End Function
'============================

hope this will help u

;-)
Shiju
0
 

Author Comment

by:anshuma
ID: 13781669
Thank you so much. It was very nice of you guys
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13787977
thank u anshuma
for the points
;-)
Shiju
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question