Solved

problems with printing a dbgrid

Posted on 1998-01-02
10
348 Views
Last Modified: 2012-08-14
I am trying to print the data and column header in a  dbgrid by using the printer.print (rowdata),but since the column header contains more characters than the column data therefore they get all mixed up ie. I am trying to print the  dbgrid like MS Excel sheet (without the grid) with each column data under its corresponding column header. I tried to use vbtab and adding spaces but it still did not work.

So is there a way in VB5 to print the data in dbgrid with its column headers in a nice format?

any comments,suggestions or sample code?
Thanks
0
Comment
Question by:khoty
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 6

Expert Comment

by:anthonyc
ID: 1448984
Here's my suggestion.  I am assuming yu have a data control on your form with the records you have, because you probably bound it to the data control

Add a crystal report control to your form.  Set the ReportSource = 3, set the datasource = data1 or whatever teh data control is.

when you want the report, just sset the action property of the crystal control to 1, and it will print the recordset.  With Crystal Reports Pro (I dont have it) you can actually do this same method with the data grid itself.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1448985
knoty, You can do it easily using the printer object just like you're doing.  To align columns use the currentx property.  For example,

Printer.CurrentX =

When you're printing a row, just set the CurrentX for each column just before print the column.

If this answer is more in line with what you are seeking, please reject anthonyc's answer and give me credit.  If you need further help with this method, just comment.
0
 

Author Comment

by:khoty
ID: 1448986
if I use the printer.currentX= then I have to truncate some column headers, in order to fit all data (because the number of columns headers is greater than the width of the page) or I may
need to change the page orientation to landscape at run time (which I don't know how)and if the user insists on portrait view then everything will be mixed up. Is there a way to dectect this at run time and change the font size to shrink everything and fit  it in one page (ie. fit it to page width)?
0
 
LVL 5

Accepted Solution

by:
dirtdart earned 100 total points
ID: 1448987
If you use the DrawText API, you can create a bounding box for each text field that you send to the printer.  You can specify word wrapping in this box, and it will automatically wrap the words for you.

Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Type DOCINFO
        cbSize As Long
        lpszDocName As String
        lpszOutput As String
End Type

Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long

Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hdc As Long, lpdi As DOCINFO) As Long

Declare Function StartPage Lib "gdi32" Alias "StartPage" (ByVal hdc As Long) As Long

Declare Function EndDoc Lib "gdi32" Alias "EndDoc" (ByVal hdc As Long) As LongDeclare Function EndDoc Lib "gdi32" Alias "EndDoc" (ByVal hDC As Long) As Long

Declare Function EndPage Lib "gdi32" Alias "EndPage" (ByVal hdc As Long) As Long

Public Const DT_WORDBREAK = &H10

Private Sub cmdPrintGrid_Click()

Dim doc As DOCINFO
Dim rec As RECT
Dim pDC As Long

pDC = Printer.hDC

StartDoc pDC, doc
StartPage pDC

rec.top = 10
rec.bottom = 500
rec.left = 10
rec.right = 500

DrawText pDC, DBGrid1.Text, Len(DBGrid1.Text), rec, DT_WORDBREAK

EndPage pDC
EndDoc pDC

End Sub

Of Course, this would only print one cell of the grid, but I think you get the idea.


0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1448988
What was wrong with  my answer?  It works fine.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:khoty
ID: 1448989
there is nothing wrong with your answer anthonyc ....I was looking for something simple ie. I just want to have a file print option in File menu and when the user clicks on it ...it will just send the data in dbgrid to printer....I don't know about the  report generation...it may make it a little complex ...
oh well thanks anyway...

0
 
LVL 8

Expert Comment

by:mrmick
ID: 1448990
Sometimes, the answer isn't as simple as you'd like - but that doesn't make it incorrect.  Looks to me like dirtdart has offered the most trying to help you.  I'd go ahead and award these points to dirtdart.
0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1448991
complex?   It takes 5 lines of code, and the report generates itself!  Not to mention it comes out more presentable!
0
 
LVL 5

Expert Comment

by:dirtdart
ID: 1448992
More presentable than what?  I can make my reports look better than anything Crystal ever thought about.
0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1448993
still, I don't see how 5 lines of code is more complex then 5 api calls.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now