Hello everyone,
I am trying to build a inventory ans pos systeme with Ms-Access as database and VB 6.0 programming. I got stuck in one place again. What I want to do is trying to print out the codes and price of the products. In this form, I have a Mshflexgrid where I have three (3) columns holding the code, price and quantity of the selected products. Just for example, like

> Code            Price         Quantity
79StABFComIFS52   $250.00           4

what I want now is, a report will generate with code and price and it will be 4 Pieces. Like

> 79StABFComIFS52  79StABFComIFS52  79StABFComIFS52  79StABFComIFS52
> $250.00          $250.00           $250.00         $250.00 

I think I make it understandable. How it is possble ? I am very much inexperienced in programming so I have googled it but did not find anything satisfactory yet. Any kinds of help will be very much appreciated. Thanks in advance.

Silversurf

Edited 4 Years Ago by silversurf

To print the actual flexgrid, use the following -

In a module add this code -

Public Sub PrintFlexGridLandscape(ByVal Ptr As Object, ByVal flxData As MSHFlexGrid, ByVal xmin As Single, ByVal ymin As Single)
Const GAP = 60

Dim xmax As Single
Dim ymax As Single
Dim X As Single
Dim c As Integer
Dim r As Integer

    With Ptr.Font
        .Name = flxData.Font.Name
        .Size = flxData.Font.Size
    End With

    Ptr.Orientation = 2

    With flxData
        ' See how wide the whole thing is.
        xmax = xmin + GAP
        For c = 0 To .cols - 1
            xmax = xmax + .ColWidth(c) + 2 * GAP
        Next c

        ' Print each row.
        Ptr.CurrentY = ymin
        For r = 0 To .rows - 1
            ' Draw a line above this row.
            If r > 0 Then Ptr.Line (xmin, _
                Ptr.CurrentY)-(xmax, Ptr.CurrentY)
            Ptr.CurrentY = Ptr.CurrentY + GAP

            ' Print the entries on this row.
            X = xmin + GAP
            For c = 0 To .cols - 1
                Ptr.CurrentX = X
                Ptr.Print BoundedText(Ptr, .TextMatrix(r, _
                    c), .ColWidth(c));
                X = X + .ColWidth(c) + 2 * GAP
            Next c
            Ptr.CurrentY = Ptr.CurrentY + GAP

            ' Move to the next line.
            Ptr.Print
        Next r
        ymax = Ptr.CurrentY

        ' Draw a box around everything.
        Ptr.Line (xmin, ymin)-(xmax, ymax), , B

        ' Draw lines between the columns.
        X = xmin
        For c = 0 To .cols - 2
            X = X + .ColWidth(c) + 2 * GAP
            Ptr.Line (X, ymin)-(X, ymax)
        Next c
    End With

Ptr.EndDoc
End Sub

In your print button, add the following code -

Call PrintFlexGridLandscape(Printer, YourMSHFlexgridnameHere, 0, 0)

Thanks Mr. AndreRet for your quick reply. I will try the code. But will it print the format I needed ? I don't need to print the whole flexgrid as it shows, I need to pull out the specific data (Code and price) and print as many times as the "quantity" row data. thanks anyway.

Just a few miniutes ago I tried the code. I add a module with the code in my project and in the click even I call the module, but when I run the program it gives me an error message saying "expected variable or procedure not module".

Just to try, I have changed the module name and again run the program, now its again giving another error message higilighting "BoundText" in the line 36 saying "Sub or Function not defined". I am totaly lost.

Edited 4 Years Ago by silversurf

If you only want to print certain data from your flexgrid, try the following -

''In your print button click event...

Dim xRows As Integer

For xRows = 0 to mshFlexgrid.Rows - 1
    ''If you have a static heading row on row 0, then start counter without the - 1 ...
    ''Set the row for grid...
    mshFlexgrid.Row = xRows
    Printer.Print mshFlexgrid.TextMatrix(mshFlexgrid.Row, 1)
    Printer.Print mshFlexgrid.TextMatrix(mshFlexgrid.Row, 2)
    ''Assuming that you are printing coloumn 1 and 2's data...
Next xRows

To correct the above code to print the grid, add the following function to the module, sorry, my bad...

' Truncate the string so it fits within the width.
Public Function BoundedText(ByVal Ptr As Object, ByVal txt As String, ByVal max_wid As Single) As String
    On Error Resume Next

    Do While Ptr.TextWidth(txt) > max_wid
        txt = Left$(txt, Len(txt) - 1)
    Loop
    BoundedText = txt
End Function

Edited 4 Years Ago by AndreRet

Thanks again Mr. AndreRet for your quick reply.The code of the module which you suggested in your first post is now working nicely. But matter of regret is that it prints the grid as it is in the screen. Sorry to say, I didn't want to print it like that way.
The second code you send, I put it into the click event of the command button, run the program, when I click the print button, it doesn't show any error message rather in the printer status it showing "spooling" but I waited for 15-20 miniutes, the printer doesn't print anything and still showing "spooling' in the staus. What is the reason ?
I am not trying to demoralize you, but the thing is as per my frist post i like to quote that I want the code and price to be printed in a precise format and the quantity column will decide how many of the same code and price should be printed. Just for example, like

> Code               Price        Quantity
79StABFComIFS52     $250.00         4

what I want now is, a report will generate with code and price and it will be 4 Pieces. Like

> 79StABFComIFS52 79StABFComIFS52 79StABFComIFS52 79StABFComIFS52
> $250.00           $250.00         $250.00        $250.00 

as I have 4 (four) in the row of quantity, I will like to have four of them to be printed. If I have any other numbers in the column of quantity, (3, 2, 5, 10 anything) I would like the program to print as much as the number of quantity.

Hope to hear from you very soon. Thanks again.

You need to tell the the printer that you have reached the end of document -

''In your print button click event...
Dim xRows As Integer
For xRows = 0 to mshFlexgrid.Rows - 1
    ''If you have a static heading row on row 0, then start counter without the - 1 ...
    ''Set the row for grid...
    mshFlexgrid.Row = xRows
    Printer.Print mshFlexgrid.TextMatrix(mshFlexgrid.Row, 1)
    Printer.Print mshFlexgrid.TextMatrix(mshFlexgrid.Row, 2)
    ''Assuming that you are printing coloumn 1 and 2's data...
Next xRows

Printer.EndDoc

''This will end the spooling and start the printjob.

This however will not sort your problem, we need to get the qauntity value and then print each quantity. I understand your question much better now. :)

Try the following -

Dim xQuantity As Integer, xPrint As Integer, PageNo As Integer
Dim strCode As String, strPrice As String

xQuantity = mshFlexgrid.TextMatrix(mshFlexgrid.Row, 3) 
''assuming that quantity is in coloumn 3...
strCode = mshFlexgrid.TextMatrix(mshFlexgrid.Row, 1) 
''assuming that Code is in coloumn 1...
strPrice = mshFlexgrid.TextMatrix(mshFlexgrid.Row, 2) 
''assuming that Price is in coloumn 2...

PageNo = 0
Line1:

PageNo = PageNo + 1

Printer.Font = "Times New Roman"
Printer.ScaleMode = vbCentimeters
Printer.Orientation = vbPRORPortrait
Printer.PaperSize = vbPRPSLetter

Printer.ScaleTop = 0
Printer.ScaleLeft = 0
Printer.FontSize = 6

Printer.Print Tab(70); "Total Quantities - "; xQuantity

For xPrint = 1 To xQuantity ''In this case 4
    Printer.Print Tab(5); strCode; Tab(35); strPrice
Next xPrint
''This will print the Items below each other...

To print them as per your sample, play with the tab code to line them up, depending on the quantity... This should put you on the right track. :)

Thank you, thank you very much Mr. AndreRet. the code is working very fine. I just need to add printer.enddoc before end sub. I am happy but not fully satisfied. Still want to annoy you for something more. You see the thing is, now the code and the price are coming side by side, like "79StABFComIFS52" "$250.00 " in the first line, aging in the 2nd line code and then the price, side by side. But I want them Like

> 79StABFComIFS52 79StABFComIFS52 79StABFComIFS52 79StABFComIFS52
> $250.00           $250.00          $250.00        $250.00 

Another thing is, the code is working only for the first row of the grid, I mean it is printing only the data of the first row, the data of the other row are not coming in to the print. Whats the problem ? I know you are trying very hard for me. Please help me a little more, plz.

When I googled my question before posting it here, I got some ideas about the printing. I am just sharing it with you. Is it possible to make a crystal report for this type of printing ? where the crystal report will have the field "code" and "Price" designed as I want them and then the report will take data from the mshflexgrid for the "code" field and for the "price" field and will print as many quantity as required taken the data from "quantity" column of the mshflexgrid, is this possible ?
Thanks again.

Edited 4 Years Ago by silversurf

I don't know wheather Mr. AndreRet has leave me on this topic or not, but isn't there anybody eles to help me out on this topic, plz I need help.

No, I did not. :) Extremely busy past few days...

Change the following part of my code to print the way you need it...

For xPrint = 1 To xQuantity ''In this case 4
    Select Case xQuantity

    Case Is = 1
        Printer.Print Tab(5); strCode
        Printer.Print Tab(5); String(55, "—")
        Printer.Print Tab(5); strPrice

    Case Is = 2
        Printer.Print Tab(5); strCode; Tab(35); strCode
        Printer.Print Tab(5); String(55, "—")
        Printer.Print Tab(5); strPrice; Tab(35); strPrice

    Case Is = 3
        Printer.Print Tab(5); strCode; Tab(35); strCode; Tab(65); strCode
        Printer.Print Tab(5); String(55, "—")
        Printer.Print Tab(5); strPrice; Tab(35); strPrice; Tab(65); strPrice

    Case Is = 4
        Printer.Print Tab(5); strCode; Tab(35); strCode; Tab(65); strCode; Tab(95); strCode
        Printer.Print Tab(5); String(55, "—")
        Printer.Print Tab(5); strPrice; Tab(35); strPrice; Tab(65); strPrice; strPrice; Tab(95); strPrice
    End Select
Next xPrint

Edited 4 Years Ago by AndreRet

Also, keep in mind that if you have a lot of quantities, you will run out of page width,space. You then need to use my previous code to print beneath all OR go to the next line.

I have tried the code you provide me in the last reply, its fine and working as my expectation, but problem is, **the code is only printing the data of the first row of the MSHFlexgrid, its not printing the other row data's. What's the catch ? ** its not only printing the same data in three column, but also printing in three rows also. I mean the same data is printing in column and row. like,

> 79StABFComIFS52      79StABFComIFS52       79StABFComIFS52          79StABFComIFS52
> $250.00               $250.00               $250.00                  $250.00
> 79StABFComIFS52       79StABFComIFS52      79StABFComIFS52         79StABFComIFS52
> $250.00               $250.00                $250.00                $250.00
> 79StABFComIFS52       79StABFComIFS52      79StABFComIFS52         79StABFComIFS52
> $250.00               $250.00               $250.00                $250.00
> 79StABFComIFS52       79StABFComIFS52      79StABFComIFS52         79StABFComIFS52
> $250.00                $250.00              $250.00                 $250.00

I just want the first row, then the second row data of MSHFlexgrid in the second row, third row data of MSHFlexgrid in the third row, its not hapenning.
Here You have decleared 4 case in your code, does that mean that it will only work for 4 time, if I have more then 4 in my quantity, what will happen then, which you already mentioned in your reply that

Also, keep in mind that if you have a lot of quantities, you will run out of page width,space. You then need to use my previous code to print beneath all OR go to the next line.

How can I overcome that limitation ? for your convinience, i am providing the full code as stands in the print command button in the click event as follows:

Dim xQuantity As Integer, xPrint As Integer, PageNo As Integer
Dim strCode As String, strPrice As String
xQuantity = grid.TextMatrix(grid.Row, 3)
''assuming that quantity is in coloumn 3...
strCode = grid.TextMatrix(grid.Row, 1)
''assuming that Code is in coloumn 1...
strPrice = grid.TextMatrix(grid.Row, 2)
''assuming that Price is in coloumn 2...
PageNo = 0
Line1:
PageNo = PageNo + 1
Printer.Font = "Times New Roman"
Printer.ScaleMode = vbCentimeters
Printer.Orientation = vbPRORPortrait
Printer.PaperSize = vbPRPSLetter
Printer.ScaleTop = 0
Printer.ScaleLeft = 0
Printer.FontSize = 10
'Printer.Print Tab(70); "Total Quantities - "; xQuantity
    For xPrint = 1 To xQuantity ''In this case 4
    Select Case xQuantity
    Case Is = 1
    Printer.Print Tab(5); strCode
    Printer.Print Tab(5); strPrice
    Printer.Print Tab(5); String(55, "—")
    Case Is = 2
    Printer.Print Tab(5); strCode; Tab(35); strCode
    Printer.Print Tab(5); strPrice; Tab(35); strPrice
    Printer.Print Tab(5); String(55, "—")
    Case Is = 3
    Printer.Print Tab(5); strCode; Tab(35); strCode; Tab(65); strCode
    Printer.Print Tab(5); strPrice; Tab(35); strPrice; Tab(65); strPrice
    Printer.Print Tab(5); String(55, "—")
    Case Is = 4
    Printer.Print Tab(5); strCode; Tab(35); strCode; Tab(65); strCode; Tab(95); strCode
    Printer.Print Tab(5); strPrice; Tab(35); strPrice; Tab(65); strPrice; strPrice; Tab(95); strPrice
    Printer.Print Tab(5); String(55, "—")
    End Select
    Next xPrint
Printer.EndDoc
end sub

thanks again. Hope have some reply from you very soon.

Edited 4 Years Ago by silversurf

the code is only printing the data of the first row of the MSHFlexgrid

That is becasue you have not looped through the rows in your grid - SEE MY ABOVE CODE.... You need to change the code to suit your needs. Do not just copy and paste the code, you will not understand what it does!

Dim xRows As Integer
For xRows = 0 to mshFlexgrid.Rows - 1
    ''If you have a static heading row on row 0, then start counter without the - 1 ...
    ''Set the row for grid...
    mshFlexgrid.Row = xRows
    ''NOW ADD THE QUANTITY SELECT STATEMENT AND LOOP CODE HERE....

Next xRows

How can I overcome that limitation ?

You need to change the code... You have all the code needed in this post to make it fit your needs. when the limitation is reached, either print on a next row, print them below each other etc. According to me, this thread is now solved, please mark it as such, thanx :)

Edited 4 Years Ago by AndreRet

Ok. I think I need to exercise more with the code. Thanks for all the effort. Bye

This question has already been answered. Start a new discussion instead.