Hello everyone,
I am trying to build a inventory and pos system 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

> 1.    > Code              Price         Quantity
> 2.    20StABFtelFSS22     730               3
> 3.    20StABFPABFKA41     788.80            6
> 4.    20StABFpriUSS60     1010              9

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

> 20StABFtelFSS22   20StABFtelFSS22    20StABFtelFSS22  
>   $ 730.00             $ 730.00          $ 730.00
>
> 20StABFPABFKA41   20StABFPABFKA41 20StABFPABFKA41   20StABFPABFKA41   20StABFPABFKA41    20StABFPABFKA41
> $ 788.80            $ 788.80         $ 788.80          $ 788.80          $ 788.80             $ 788.80
> 
> 20StABFpriUSS60   20StABFpriUSS60  20StABFpriUSS60  20StABFpriUSS60   20StABFpriUSS60 20StABFpriUSS60 
> $ 1010                 $ 1010         $ 1010             $ 1010           $ 1010              $ 1010  
> 
> 20StABFpriUSS60     20StABFpriUSS60    20StABFpriUSS60    
> $ 1010                $ 1010             $ 1010

I have made a post about this previously and got the following code under the click event of my Print button:

1.  Dim xQuantity As Integer, xPrint As Integer, PageNo As Integer
2.  Dim strCode As String, strPrice As String
3.  xQuantity = grid.TextMatrix(grid.Row, 3)
4.  ''assuming that quantity is in coloumn 3...
5.  strCode = grid.TextMatrix(grid.Row, 1)
6.  ''assuming that Code is in coloumn 1...
7.  strPrice = grid.TextMatrix(grid.Row, 2)
8.  ''assuming that Price is in coloumn 2...
9.  PageNo = 0
10. Line1:
11. PageNo = PageNo + 1
12. Printer.Font = "Times New Roman"
13. Printer.ScaleMode = vbCentimeters
14. Printer.Orientation = vbPRORPortrait
15. Printer.PaperSize = vbPRPSLetter
16. Printer.ScaleTop = 0
17. Printer.ScaleLeft = 0
18. Printer.FontSize = 10
19. 'Printer.Print Tab(70); "Total Quantities - "; xQuantity
20.     For xPrint = 1 To xQuantity ''In this case 4
21.     Select Case xQuantity
22.     Case Is = 1
23.     Printer.Print Tab(5); strCode
24.     Printer.Print Tab(5); strPrice
25.     Printer.Print Tab(5); String(55, "—")
26.     Case Is = 2
27.     Printer.Print Tab(5); strCode; Tab(35); strCode
28.     Printer.Print Tab(5); strPrice; Tab(35); strPrice
29.     Printer.Print Tab(5); String(55, "—")
30.     Case Is = 3
31.     Printer.Print Tab(5); strCode; Tab(35); strCode; Tab(65); strCode
32.     Printer.Print Tab(5); strPrice; Tab(35); strPrice; Tab(65); strPrice
33.     Printer.Print Tab(5); String(55, "—")
34.     Case Is = 4
35.     Printer.Print Tab(5); strCode; Tab(35); strCode; Tab(65); strCode; Tab(95); strCode
36.     Printer.Print Tab(5); strPrice; Tab(35); strPrice; Tab(65); strPrice; strPrice; Tab(95); strPrice
37.     Printer.Print Tab(5); String(55, "—")
38.     End Select
39.     Next xPrint
40. Printer.EndDoc
41. End sub

But the above code is giving me the following result:

> 20StABFtelFSS22       20StABFtelFSS22     20StABFtelFSS22 
> $ 730.00             $ 730.00               $ 730.00
> 20StABFtelFSS22       20StABFtelFSS22     20StABFtelFSS22 
> $ 730.00             $ 730.00               $ 730.00
> 20StABFtelFSS22       20StABFtelFSS22     20StABFtelFSS22 
> $ 730.00             $ 730.00               $ 730.00
> 20StABFtelFSS22       20StABFtelFSS22     20StABFtelFSS22 
> $ 730.00             $ 730.00               $ 730.00

Also this code is only printing the first row data. I know I am very novish in VB and can’t make the code working for me in the right way. What is I am missing here?
I think I make it understandable. How it is possble ? Any kinds of help will be very much appreciated. Thanks in advance.

Silversurf

Recommended Answers

All 21 Replies

Also this code is only printing the first row data

That is becuase you are not looping through all the rows in the grid...

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

''Added xRow above...

For xRow = 0 To grid.Rows - 1 ''Assuming that you have no titles in the first row...
    ''Set the row of the grid to be selected...
    grid.Row = xRow

    xQuantity = grid.TextMatrix(grid.Row, 3)        
    strCode = grid.TextMatrix(grid.Row, 1)      
    strPrice = grid.TextMatrix(grid.Row, 2)


    ''ALL THE PRINTING CODE HERE FROM PageNo...
Next xRow

''EndDoc goes here after all data has been loaded to start printing...
Printer.EndDoc

To move certain words further left or right, change it's tab number property i.e. Tab(35), move more left will then be Tab(27) or as much as you like, to the right - Tab(42) or as you like. When you get to an end of a line, normally about Tab(140), go to another line. You can add the code in your print Select Case, similar to this code...

Case Is = 5
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); "Continued on line 2"

Printer.Print Tab(5); strCode
Printer.Print Tab(5); strPrice

Printer.Print Tab(5); String(55, "—")

Case Is = 6
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); "Continued on line 2"

Printer.Print Tab(5); strCode; Tab(35); strCode
Printer.Print Tab(5); strPrice; Tab(35); strPrice

Printer.Print Tab(5); String(55, "—")

And so on to as many as you want between __________ lines...

Set your page orientation to portrait as well to get more in one line, should go to about Tab(175) I think. This will be something you need to play with until it fits your needs.

thanks for the reply. I have put the code as you said. but do I have to declear all the case to print(like Case1, case2.............. case50) ? another thing, I am getting an erro message now in

xQuantity = grid.TextMatrix(grid.Row, 3)

saying Type mismatch, what is the reason ?

Yes, unfortunately you have to add all the select cases otherwise the printer will not know how many to print.

The error is because we are referencing a different row than was selected. It should be -

xQuantity = grid.TextMatrix(grid.Row, xRow)        
    strCode = grid.TextMatrix(grid.Row, xRow)      
    strPrice = grid.TextMatrix(grid.Row, xRow)

You need to read the code and make sure that you understand it properly. You would have picked up on the error then immediatly. :)

I know you might be getting annoyed but i failed again. Will you please againg look at the total code. Here it is:

Private Sub cmdprint_Click()
Dim xRows As Integer
Dim xQuantity As Integer, xPrint As Integer, PageNo As Integer
Dim strCode As String, strPrice As String
For xRows = 0 To grid.Rows - 1
grid.Row = xRows
xQuantity = grid.TextMatrix(grid.Row, xRows)
''assuming that quantity is in coloumn 3...
strCode = grid.TextMatrix(grid.Row, xRows)
''assuming that Code is in coloumn 1...
strPrice = grid.TextMatrix(grid.Row, xRows)
''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 = 12
'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
Next xRows
Printer.EndDoc
End Sub

I am still getting type mismatch here

xQuantity = grid.TextMatrix(grid.Row, xRows)

I am totally lost. Please look at the total code and provide me a good solution plz.

silversurf

Which datagrid are you using? The code above is for MSHFlexgrid.

I am using MSHFlexgrid

Change the following code -

grid.Row = xRows

''TO

grid.RowSel = xRows

It will select the current row of the loop.

Also, your mshflexgrid is called grid right?

changed it to Rowsel, but still getting the Type mismatch error on

xQuantity = grid.TextMatrix(grid.Row, xRows)

yes my MSHFlexgrid is called grid

Ok, I'm gonna have to do this the long way. I'll test the code quickly, will let you know in a few.

Ok, found the problem. First change the code to the following...

Dim xRows As Integer
Dim xQuantity As String, xPrint As Integer, PageNo As Integer ''See xQuantity is now a string, not number...
''If xQuantity is a number, keep it on integer...
Dim strCode As String, strPrice As String

For xRows = 1 To grid.Rows - 1
    grid.RowSel = xRows
    xQuantity = grid.TextMatrix(xRows, 3) ''Keep the coloumn value - col 1,2,3 etc. Change the row value to xrows
    ''assuming that quantity is in coloumn 3...
    strCode = grid.TextMatrix(xRows, 2)
    ''assuming that Code is in coloumn 1...
    strPrice = grid.TextMatrix(xRows, 1)
    ''assuming that Price is in coloumn 2...
    PageNo = 0

    MsgBox xQuantity ''Just to test a returned value.
    MsgBox strCode
    MsgBox strPrice

    ''All printing code back here again. I did NOT test printing, you need to set it up yourself, you have all the tools and code :)

Next xRows
Printer.EndDoc

Also see the notes I made within the code...

now i have the following code

Private Sub cmdprint_Click()
Dim xRows As Integer
Dim xQuantity As Integer, xPrint As Integer, PageNo As Integer ''See xQuantity is now a string, not number...
''If xQuantity is a number, keep it on integer...
Dim strCode As String, strPrice As String
For xRows = 1 To grid.Rows - 1
grid.RowSel = xRows
xQuantity = grid.TextMatrix(xRows, 3) ''Keep the coloumn value - col 1,2,3 etc. Change the row value to xrows
''assuming that quantity is in coloumn 3...
strCode = grid.TextMatrix(xRows, 2)
''assuming that Code is in coloumn 1...
strPrice = grid.TextMatrix(xRows, 1)
''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 = 12
'Printer.Print Tab(70); "Total Quantities - "; xQuantity
MsgBox xQuantity ''Just to test a returned value.
MsgBox strCode
MsgBox strPrice
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
Next xRows
Printer.EndDoc
End Sub

First I have changed the xQuantity into String and again chaged it into Integer, in both cases, two things happened, first I have error message on

Printer.Orientation = vbPRORPortrait

second still the print is happening this way

>  730.00            730.00             730.00
>  20StABFtelFSS22  20StABFtelFSS22     20StABFtelFSS22
>______________________________________________________
>  730.00            730.00             730.00
> 20StABFtelFSS22   20StABFtelFSS22    20StABFtelFSS22
>______________________________________________________
>  730.00 $           730.00              730.00
> 20StABFtelFSS22   20StABFtelFSS22    20StABFtelFSS22
>______________________________________________________

The price is coming before the code now and the second, third row still not printing
Another thing,the following part which you have included

MsgBox xQuantity ''Just to test a returned value.
    MsgBox strCode
    MsgBox strPrice

is only giving the return value of the first row of the grid like 3, 730 & 20StABFtelFSS22 in three separate message box before starting the printing.

Silversurf

From the bottom up -

1) msgbox was to catch if a value was returned, it did, so all working fine up to there. You can now delete it.

2) You need to CHANGE the code to suit your needs. If money is printing before the others, change the code.... strCode and strPrice etc.

3) Printer orientation has nothing to do with xQuantity... Printer.Orientation = 2 should be ok.

From the bottom up -

1) msgbox was to catch if a value was returned, it did, so all working fine up to there. You can now delete it.

2) You need to CHANGE the code to suit your needs. If money is printing before the others, change the code.... strCode and strPrice etc.

3) Printer orientation has nothing to do with xQuantity... Printer.Orientation = 2 should be ok.

ok, I understand. but why there is nothing printing from the second, third row still not printing ? the first row is printing. I didn't have a clue. Please solve this.

Post all your printing code so I can have a look at what you got so far.

Ok. Here it is again

Private Sub cmdprint_Click()
Dim xRows As Integer
Dim xQuantity As Integer, xPrint As Integer, PageNo As Integer ''See xQuantity is now a string, not number...
''If xQuantity is a number, keep it on integer...
Dim strCode As String, strPrice As String
For xRows = 1 To grid.Rows - 1
grid.RowSel = xRows
xQuantity = grid.TextMatrix(xRows, 3) ''Keep the coloumn value - col 1,2,3 etc. Change the row value to xrows
''assuming that quantity is in coloumn 3...
strCode = grid.TextMatrix(xRows, 2)
''assuming that Code is in coloumn 1...
strPrice = grid.TextMatrix(xRows, 1)
''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 = 12
'Printer.Print Tab(70); "Total Quantities - "; xQuantity
MsgBox xQuantity ''Just to test a returned value.
MsgBox strCode
MsgBox strPrice
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
Next xRows
Printer.EndDoc
End Sub

thats all I have.

Silversurf

I have tested the following code, works perfect. If you want to change the price and code around, easy just swap it around.

You can not set the papersize or orientation. i'll have a look at a solution later on, whole new question :)

Private Sub cmdprint_Click()

Dim xRows As Integer
Dim xQuantity As Integer, xPrint As Integer, PageNo As Integer ''See xQuantity is now a string, not number...
''If xQuantity is a number, keep it on integer...
Dim strCode As String, strPrice As String

For xRows = 0 To grid.Rows - 1 ''If you have headings, set 0 to 1...
    grid.RowSel = xRows
    xQuantity = grid.TextMatrix(xRows, 3) ''Keep the coloumn value - col 1,2,3 etc. Change the row value to xrows
    ''assuming that quantity is in coloumn 3...
    strCode = grid.TextMatrix(xRows, 2)
    ''assuming that Code is in coloumn 1...
    strPrice = grid.TextMatrix(xRows, 1)
    ''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 = 8
    '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; Tab(95); strPrice
        Printer.Print Tab(5); String(55, "—")
        End Select
    ''Next xPrint

    strCode = ""
    strPrice = ""

Next xRows
Printer.EndDoc
End Sub

Please see the comments...

Thanks for the code. I have now the following code as per your codes:

Private Sub cmdprint_Click()
Dim xRows As Integer
Dim xQuantity As Integer, xPrint As Integer, PageNo As Integer ''See xQuantity is now a string, not number...
''If xQuantity is a number, keep it on integer...
Dim strCode As String, strPrice As String
For xRows = 1 To grid.Rows - 1
grid.RowSel = xRows
xQuantity = grid.TextMatrix(xRows, 3) ''Keep the coloumn value - col 1,2,3 etc. Change the row value to xrows
''assuming that quantity is in coloumn 3...
strCode = grid.TextMatrix(xRows, 2)
''assuming that Code is in coloumn 1...
strPrice = grid.TextMatrix(xRows, 1)
''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 = 12
'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
    strCode = ""
    strPrice = ""
Next xRows
Printer.EndDoc
End Sub

the problem is, second or third row data is still not printing.

Please take a picture of your datagrid filled with data and attach it here. I have tested this and it works fine, it prints all of thye lines, tested it up to 9 lines...

I want to see what the data looks like and where they are in your grid.

Its working at last. yes, yes its working. I need some small tips now,
1) 'Printer.Orientation = vbPRORPortrait is not woking when it is active. How can I print it in Landscape ?
2) amount is coming before the code, how can i make to alter like code first then amount
3) can I put some sign before the amount, like $, if so, what should be the code ?
4) Should I put the whole code in a module and call it, cauz I think I need to write 200 case in this method.

Thanks anyway.

Silversurf

:) Glad you got it to work.

1) - Printer.orientation is a new question, please open a new thread...
2) - swap strcode and strprice around. This I have mentioned before, please read my posts carefully so that you understand it...

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

3) - Yes you can -

strPrice = "$ " & strPrice
''Insert this AFTER you gave a value to strPrice...

4) - If it is going to be that long, yes, put it in a module...

This thread is now closed according to me. Please mark it as solved thanx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.