Print Format in vb6.0

Hello Experts
i want following print format, How can i do that,

ID No     Pcs     Product       ID No    Pcs    Product         ID No     Pcs        Product
2971     4         SWITCH       2972     13     SOCKET        2973        7        MCB
2974     6         MCB             2977      2      SWITCH         2981      12       SOCKET

My print code is as follow

This following code is working fine but its giving me single row detail per ID No,
and i want format as i mention above.

How can i do this ,

Thanks in advance..


CTemp = App.Path & "\ctp.tmp"
CTempLen = Len(CTemp)
Port = "LPT3:"

Open Port For Output As #1 Len = CTempLen

With RS1
.MoveFirst
'I = .RecordCount
While Not .EOF


Print #1, "" & NumAlign(7, MSHFlexGrid1.TextMatrix(i, 1)) & " " & _
                NumAlign(7, MSHFlexGrid1.TextMatrix(i, 2)) & "    " & _
                StrAlign(15, MSHFlexGrid1.TextMatrix(i, 3))""
                
Print #1, ""
    i = i + 1
.MoveNext
Wend
End With

Open in new window

LVL 1
crystalsoftAsked:
Who is Participating?
 
DDBConnect With a Mentor Commented:
it's just an idea. You can make it more safisticated. here is code.

'Set a reference to "Microsoft Scripting Runtime"

'declare and initiate required objects
Dim fs As FileSystemObject
Dim ts_w As TextStream
Dim ts_r As TextStream
Dim i As Integer
Dim sTemp1 As String
Dim sTemp2 As String
Dim sTitle1_line1 As String
Dim sTitle2_lin2 As String

sTitle_line1 = "ID No     Pcs        Product     ID No     Pcs        Product     ID No     Pcs        Product"
sTitle_line2 = "---------------------------------------------------------------------------------------------"
 
Set fs = New FileSystemObject

'To Read
If fs.FileExists("C:\temp\input.txt") Then
    Set ts_r = fs.OpenTextFile("C:\temp\input.txt")

    Do While Not ts_r.AtEndOfStream
 
        sTemp1 = sTemp1 & ts_r.ReadLine
        sTemp2 = sTemp2 & ts_r.ReadLine
         
    Loop

    ts_r.Close
End If

'clear memory used by FSO objects
Set ts_r = Nothing


'To write
Set ts_w = fs.OpenTextFile("C:\temp\output.txt", ForWriting, True)
    ts_w.writeLine sTitle_line1
    ts_w.writeLine sTitle_line2
    ts_w.writeLine sTemp1
    ts_w.writeLine sTemp2
 
    ts_w.Close
    Set fs = Nothing


attached find output file that I got.
output.txt
0
 
LeithauserCommented:
Use TAB, such as
PRINT TAB(2) ; NumAlign(7, MSHFlexGrid1.TextMatrix(i, 1)) ; TAB(10);  NumAlign(7, MSHFlexGrid1.TextMatrix(i, 2)) ;TAB(20);StrAlign(15, MSHFlexGrid1.TextMatrix(i, 3))
               
0
 
crystalsoftAuthor Commented:
Thanks for reply

You mean to this ??

Print #1, " TAB(2)" & NumAlign(7, MSHFlexGrid1.TextMatrix(i, 1)) & " TAB(10)" & _
                NumAlign(7, MSHFlexGrid1.TextMatrix(i, 2)) & "   TAB(20) " & _
                StrAlign(15, MSHFlexGrid1.TextMatrix(i, 3))""
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LeithauserCommented:
I use the semicolons, and they work fine. I am not sure that you can combine the TAB with &, because it may be physically moving the printer to the position, not forming a string. TAB(N) is no the same as SPACE$(N).
0
 
crystalsoftAuthor Commented:

its printing but as usual,
 not as desired format,

Print #1, Tab(2); NumAlign(7, MSHFlexGrid1.TextMatrix(i, 1)); Tab(10); NumAlign(7, MSHFlexGrid1.TextMatrix(i, 2)); Tab(20); StrAlign(15, MSHFlexGrid1.TextMatrix(i, 3))

Your code is printing same my code prints ,

Please check my question and format as i mentioned.

Thanks

0
 
crystalsoftAuthor Commented:
Format i want is like this.

ID No     Pcs     Product       ID No    Pcs    Product         ID No     Pcs        Product
-------------------------------------------------------------------------------------------------------
2971     4         SWITCH       2972     13     SOCKET        2973        7        MCB
2974     6         MCB             2977      2      SWITCH         2981      12       SOCKET
0
 
LeithauserCommented:
Please elaborate on how the code I sent you is printing different than the way you want it. Can you show how it looks now?
0
 
crystalsoftAuthor Commented:
its printing like this

ID No     Pcs     Product             ID No    Pcs    Product         ID No     Pcs        Product
-------------------------------------------------------------------------------------------------------
2971       4         SWITCH      
2972     13         SOCKET      
2973       7         MCB
2974       6         MCB            
2977       2         SWITCH        
2981     12         SOCKET
0
 
VBClassicGuyCommented:
Try this code (just pass the subroutine the name of the flexgrid, and whether to print just the selected lines or not. Text justification is taken from the FlexGrid itself. If the FlexGrid's Tag property is "NoEmptyXX", any column numbered XX that is empty will cause that row to not be printed:

Sub FlexPrint(fg As MSFlexGrid, sel%)
   Dim tabs%(2, 48)
   
   page% = 1
   
   txt$ = fg.Tag
   If Left$(txt$, 7) = "NoEmpty" Then
      NoEmpty% = True
      chkCol% = Val(Right$(txt$, 2))
   End If
   
   Rem *** Calc column width factor ***
   adj! = Round(Printer.fontSize) / 10
   
   Rem *** Calc total column gaps - add space if an LJ is preceded by an RJ ***
   For c% = 1 To fg.Cols - 1
      Select Case fg.ColAlignment(c%)
         Case 0 To 2 'LJ
            Select Case fg.ColAlignment(c% - 1)
               Case 6 To 8 'RJ
                  gap% = gap% + 50
               Case 9      'generic
                  txt$ = fg.TextMatrix(2, c% - 1)
                  If IsNumeric(txt$) Then 'RJ
                     gap% = gap% + 50
                  End If
            End Select
         Case 9
            txt$ = fg.TextMatrix(2, c%)
            If IsNumeric(txt$) Then
               Rem *** NOP ***
            Else
               Select Case fg.ColAlignment(c% - 1)
                  Case 6 To 8 'RJ
                     gap% = gap% + 50
                  Case 9      'generic
                     txt$ = fg.TextMatrix(2, c% - 1)
                     If IsNumeric(txt$) Then 'RJ
                        gap% = gap% + 50
                     End If
               End Select
            End If
      End Select
   Next c%
   
   Rem *** Calc line length ***
   For c% = 0 To fg.Cols - 1
      w% = w% + fg.ColWidth(c%) * adj!
   Next c%
   w% = w% + gap%
   
   Rem *** Set margin ***
   margin% = (Printer.ScaleWidth - w%) / 2
   
   Rem *** Calc column LJ/RJ positions ***
   tabs%(0, 0) = margin%   'LJ
   tabs%(1, 0) = margin% + fg.ColWidth(0) * adj!   'RJ
   tabs%(2, 0) = tabs%(0, 0) + ((tabs%(1, 0) - tabs%(0, 0)) \ 2)  'CJ
   sofar% = tabs%(1, 0)
   Rem *** Only add space between columns if an LJ is preceded by an RJ ***
   For c% = 1 To fg.Cols - 1
      Select Case fg.ColAlignment(c%)
         Case 0 To 2 'LJ
            Select Case fg.ColAlignment(c% - 1)
               Case 6 To 8 'RJ
                  gap% = 50
               Case 9      'generic
                  txt$ = fg.TextMatrix(2, c% - 1)
                  If IsNumeric(txt$) Then 'RJ
                     gap% = 50
                  End If
               Case Else
                  gap% = 0
            End Select
         Case 9
            txt$ = fg.TextMatrix(2, c%)
            If IsNumeric(txt$) Then 'RJ
               gap% = 0
            Else
               Select Case fg.ColAlignment(c% - 1)
                  Case 6 To 8 'RJ
                     gap% = 50
                  Case 9      'generic
                     txt$ = fg.TextMatrix(2, c% - 1)
                     If IsNumeric(txt$) Then 'RJ
                        gap% = 50
                     End If
                  Case Else
                     gap% = 0
               End Select
            End If
         Case Else
            gap% = 0
      End Select
      tabs%(0, c%) = sofar% + gap%  'LJ
      tabs%(1, c%) = tabs%(0, c%) + fg.ColWidth(c%) * adj!  'RJ
      tabs%(2, c%) = tabs%(0, c%) + ((tabs%(1, c%) - tabs%(0, c%)) \ 2) 'CJ
      sofar% = tabs%(1, c%)
   Next c%
   
   Rem *** Print header ***
   If Len(fg.TextMatrix(0, 0)) Then
      Rem *** Only prints header if Row 0 has text ***
      Printer.FontBold = True
      For c% = 0 To fg.Cols - 1
         If fg.ColWidth(c%) Then
            Select Case fg.ColAlignment(c%)
               Case 0 To 2   'LJ
                  Call PrintLJ(tabs%(0, c%), fg.TextMatrix(0, c%), False)
               Case 3 To 5   'CJ
                  Call PrintCJ(tabs%(2, c%), fg.TextMatrix(0, c%), False)
               Case 6 To 8   'RJ
                  Call PrintRJ(tabs%(1, c%), fg.TextMatrix(0, c%), False)
               Case 9
                  txt$ = fg.TextMatrix(0, c%)
                  If IsNumeric(txt$) Then
                     Call PrintRJ(tabs%(1, c%), fg.TextMatrix(0, c%), False)
                  Else
                     Call PrintLJ(tabs%(0, c%), fg.TextMatrix(0, c%), False)
                  End If
            End Select
         End If
      Next c%
      Printer.Print
      Printer.CurrentY = Printer.CurrentY + 50
      Printer.Line (margin%, Printer.CurrentY)-(margin% + w%, Printer.CurrentY)
      Printer.CurrentY = Printer.CurrentY + 50
   End If
   Rem *** Set lines to print ***
   If sel% Then
      st& = fg.Row
      nd& = fg.RowSel
   Else
      st& = 2
      nd& = fg.Rows - 1
   End If
   
   Rem *** Print it! ***
   Printer.FontBold = False
   For r& = st& To nd&
      DontPrint% = False
      If NoEmpty% Then
         txt$ = fg.TextMatrix(r&, chkCol%)
         If (Len(txt$) = 0) Or (Val(txt$) = 0) Then
            DontPrint% = True
         End If
      End If
      If Not DontPrint% Then
         If Printer.CurrentY + Printer.TextHeight("Sample") > Printer.ScaleHeight Then
            Printer.NewPage
            GoSub StartNewPage
         End If
         For c% = 0 To fg.Cols - 1
            DontPrint% = False
            If NoEmpty% And (c% = chkCol%) Then
               txt$ = fg.TextMatrix(r&, c%)
               If (Len(txt$) = 0) Or (Val(txt$) = 0) Then
                  DontPrint% = True
               End If
            End If
            If fg.ColWidth(c%) Then
               Select Case fg.ColAlignment(c%)
                  Case 0 To 2   'LJ
                     Call PrintLJ(tabs%(0, c%), fg.TextMatrix(r&, c%), False)
                  Case 3 To 5   'CJ
                     Call PrintCJ(tabs%(2, c%), fg.TextMatrix(r&, c%), False)
                  Case 6 To 8   'RJ
                     Call PrintRJ(tabs%(1, c%), fg.TextMatrix(r&, c%), False)
                  Case 9
                     txt$ = fg.TextMatrix(r&, c%)
                     If IsNumeric(txt$) Then
                        Call PrintRJ(tabs%(1, c%), txt$, False)
                     Else
                        Call PrintLJ(tabs%(0, c%), txt$, False)
                     End If
               End Select
            End If
         Next c%
         Printer.Print
      End If
   Next r&
   
   Exit Sub
   
StartNewPage:
   page% = page% + 1
   Rem Call PrintHdr(page%)   'my own routine to print page header
   Printer.FontBold = True
   Printer.Print
   Rem *** Print legend ***
   For c% = 0 To fg.Cols - 1
      Select Case fg.ColAlignment(c%)
         Case 1   'LJ
            Call PrintLJ(tabs%(0, c%), fg.TextMatrix(0, c%), False)
         Case 4   'CJ
            Call PrintCJ(tabs%(2, c%), fg.TextMatrix(0, c%), False)
         Case 7   'RJ
            Call PrintRJ(tabs%(1, c%), fg.TextMatrix(0, c%), False)
      End Select
   Next c%
   Printer.Print
   Printer.CurrentY = Printer.CurrentY + 50
   Printer.Line (margin%, Printer.CurrentY)-(margin% + w%, Printer.CurrentY)
   Printer.CurrentY = Printer.CurrentY + 50
   Printer.FontBold = False
   Return
End Sub
 
0
 
crystalsoftAuthor Commented:

Thanks VB Classic Guy

But Can you please explain me why its giving me ERROR
"Compile Error" Argument Not Optional"

and highlighted on


Private Sub Command4_Click()

Call FlexPrint

End Sub

Another thing i want to mention that i am using MSHflexgrid, its not MSFlexgrid,

Thanks
0
 
VBClassicGuyCommented:
First, call it like:
Call FlexPrint (MSHFlexGrid1, 0)   'replace "MSHFlexGrid1" with the actual name of your grid
Secondly, to use the different grid, change the declaration of the subroutine to:
Sub FlexPrint(fg As MSHFlexGrid, sel%)
 
0
 
VBClassicGuyCommented:
Opps, I meant:
Call FlexPrint (fg As MSHFlexGrid1, 0)
0
 
VBClassicGuyCommented:
Darn, did it again. This is the declaration:
Sub FlexPrint(fg As MSHFlexGrid, sel%)
 
0
 
VBClassicGuyCommented:
Better put it all together...
Declare the subroutine thusly;
Sub FlexPrint(fg As MSHFlexGrid, sel%)

and call it by using:
Call FlexPrint (MSHFlexGrid1, 0)   'replace "MSHFlexGrid1" with the actual name of your grid
0
 
crystalsoftAuthor Commented:

"Compile error:
Variable not defined"
Highlighted on
Page% = 1
(if i put Dim Page%)
Then another same error on
txt$ =
(again (if i put Dim txt$)
Then on
NoEmpty% = True

Can You Please Check what variable i have to declare.

Thanks

 
0
 
crystalsoftAuthor Commented:

Hello Experts
Can You Please help to print ID No on Sequence.

I am using following code to solve my problem, its working fine
but its not giving me ID NO on sequence

like i want following formate how can i do this ?

ID No      Pcs   Product        ID No    Pcs     Product          ID No     Pcs     Product

2971       4      SWITCH       2974     13       SOCKET        2977       7        MCB
2972       6           MCB        2975      2        SWITCH        2978       6        MCB
2973     13     SOCKET        2976    12       SOCKET        2979       4        SWITCH

i am using code is attached



Print #1, "M" & NumAlign(7, MSHFlexGrid1.TextMatrix(I, 1)) & "" & _
                NumAlign(7, MSHFlexGrid1.TextMatrix(I, 2)) & "" & _
                StrAlign(15, MSHFlexGrid1.TextMatrix(I, 3)) & "" & _
                NumAlign(7, MSHFlexGrid1.TextMatrix(I + 1, 1)) & "" & _
                NumAlign(7, MSHFlexGrid1.TextMatrix(I + 1, 2)) & "" & _
                StrAlign(15, MSHFlexGrid1.TextMatrix(I + 1, 3)) & "" & _
                NumAlign(7, MSHFlexGrid1.TextMatrix(I + 2, 1)) & "" & _
                NumAlign(7, MSHFlexGrid1.TextMatrix(I + 2, 2)) & "" & _
                StrAlign(15, MSHFlexGrid1.TextMatrix(I + 2, 3)) & ""
Print #1, ""
    I = I + 3

Open in new window

0
 
VBClassicGuyCommented:
I always use the default Opton Implicit (don't ask why). If you use Option Explicit, then you would need to declare ALL the variables used in my routine at the top of the routine.
0
 
crystalsoftAuthor Commented:

Thanks VBClassicGuy: for your help

But please check my earlier post as i mentioned that i solved my problem, just need to sequence ID No. like this.

ID No      Pcs   Product        ID No    Pcs     Product          ID No     Pcs     Product

2971       4      SWITCH       2974     13       SOCKET        2977       7        MCB
2972       6           MCB        2975      2        SWITCH        2978       6        MCB
2973     13     SOCKET        2976    12       SOCKET        2979       4        SWITCH

Crystalsoft
0
 
VBClassicGuyCommented:
Just sort before you print, with:
MSHFlexGrid1.Col = 0   'or whatever column holds the ID No.
MSHFlexGrid1.Sort 3   'sort rows by numeric ascending
now print...
 
I doubt you'll need this, but here's just an extra trick I've used in the past: If you need to print sorted, but leave the grid contents alone (unsorted), just copy and paste the grid, but make the copy invisible. Then copy the rows from the visible grid to the invisible gird, and print the invisible grid. If you need quickie code/routines for copying the contents of one grid to another, let me know.
0
 
crystalsoftAuthor Commented:

I appreciate your help but i have doubt that it'll work, because ID No already sorted in MSHflexgrid,
if we copy data form MSHflexgrid1 to another grid also giving us same format,

You know better than me,

0
 
DDBCommented:
I would try to create output file with format that you want to see.  For exmple first save N first records (if you want to see overall N lines). In case below it's = 3

2971       4    SWITCH  
2972       6    MCB      
2973     13     SOCKET  

Save file and read it again line by line adding to each line corresponding follows values, so after next cycle it will look as :

 2974     13    SOCKET       2973     13     SOCKET
 2971       4    SWITCH        2975      2       SWITCH  
2972       6     MCB              2976     12       SOCKET  
 
Repeate those steps unitl EOF.

Then just open the file and print it line by line. I would put title in the first line of the file as well.




 
0
 
crystalsoftAuthor Commented:

Thanks DDB

But Can you please show me code,
i tried several logic but no one is working.. :-(

Thanks in advance

0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
DDBCommented:
Sorry emoreau, I was nor aware about it. It seems the same question and the same user.
0
 
crystalsoftAuthor Commented:

First i posted this question,  and i didn't get my answer, after that i posted another one and emoreau: solved this problem,

BUT

DDB : your answer can solved my another question How to read - write - and print .txt file

and i tested also its working in some of cases , and some of my testing scenario,

Thanks DDB

I don't think any other experts have any problem if i will accept your answer,

 

0
All Courses

From novice to tech pro — start learning today.