Here's a little something that took me forever to figure out... it's printing the data STRAIGHT from a data grid with OUT it printing the entire page. Just plug in what you want. Some of the stuff in there is just the spacing used for column sizes for the headers. Anyhow, here it is (some was pieced from different places and then the rest I pretty much fumbled through until it worked) Im including the entire form, so u get a better idea. There are actually 3 forms, but only one is shown:
Option Explicit
Public server As String
Public area As String
Public rs As ADODB.Recordset, dataconn As ADODB.Connection, tble As String, param4 As String
Public sqlQuery As String, connsource As String, flag As Integer
Public param3 As String, rsprint As ADODB.Recordset
Private Sub cmdclose_Click()
Unload Me
Load frmmain
frmmain.Show
End Sub
Private Sub cmddate_Click()
param4 = " order by offload_datetime"
Form_Load
End Sub
Private Sub cmdprint_Click()
Printer.Orientation = 2
Call PrintGrid(DataGrid2)
End Sub
Private Sub cmdsernum_Click()
param4 = " order by serial_number"
Form_Load
End Sub
Private Sub Form_Load()
Dim param As String, x As Integer, param2 As String, param3 As String
Dim serv As String, dbase As String, address As String
DataGrid1.DefColWidth = 2300
If frmmain.opsernum = True Then
param = frmmain.serialNum
param2 = frmmain.serialNum2
param3 = "serial_number"
Else 'If frmmain.PDSNum = True Then
param = frmmain.PDSNum
param2 = frmmain.PDSNum2
param3 = "sched_pdsn"
End If
If frmmain.vqop = True Then
serv = "nmmd_vq_area"
address = "xx.xx.xx.xx, 1433"
dbase = "vq_assembly"
tble = "vq_table"
ElseIf frmmain.trop = True Then
serv = "ta5a01"
address = "xx.xx.xx.xx, 1433"
dbase = "ta5_data"
tble = "ta5_table"
End If
connsource = "Driver={SQL Server};" & "Server=" & serv & ";" & _
"Address=" & address & ";" & "Network=DBMSSOCN;" & _
"Database=" & dbase & ";" & "Uid=sa;" & "pwd="
Set dataconn = New ADODB.Connection
Set rs = New ADODB.Recordset
dataconn.ConnectionString = connsource
dataconn.CursorLocation = adUseClient
dataconn.Open
'plug in sql query values
If frmmain.optsingle.Value = True Then
sqlQuery = "Select * from " & tble & " where " & param3 & " = '" _
& param & "'" & param4
Else
sqlQuery = "Select * from " & tble & " where " & param3 & " between '" _
& param & "' And '" & param2 & "'" & param4
End If
rs.Open sqlQuery, dataconn, adOpenStatic
If rs.RecordCount = 0 Then
MsgBox ("Either the search came up empty or there is a problem with the database.")
Unload frmresult
frmmain.Show
Exit Sub
Else
Set DataGrid1.DataSource = rs
End If
'datagrid2 for print only
Set rsprint = New ADODB.Recordset
If frmmain.optsingle.Value = True Then
sqlQuery = "Select serial_number,broadcast_name,offload_datetime,engi ne_type,nissan_part," _
& "sequence_number,trans71_sent,trans73_sent,trans74 _sent," _
& "trans75_sent,sched_PDSN,offload_PDSN from " & tble & " where " & param3 & " = '" _
& param & "'" & param4
Else
sqlQuery = "Select serial_number,broadcast_name,offload_datetime,engi ne_type,nissan_part," _
& "sequence_number,trans71_sent,trans73_sent,trans74 _sent," _
& "trans75_sent,sched_PDSN,offload_PDSN from " & tble & " where " & param3 & " between '" _
& param & "' And '" & param2 & "'" & param4
End If
rsprint.Open sqlQuery, dataconn, adOpenStatic
Set DataGrid2.DataSource = rsprint
End Sub
Private Sub PrintGrid(ctlGrid As Control)
Dim I As Integer
Dim J As Integer
Dim PTab As Integer
With ctlGrid
' PTab = 25 'Set the first tab value
For J = 0 To .Columns.Count - 1
.Col = J 'Set the current column
If .Col = 0 Then
PTab = PTab + 18
ElseIf .Col = 1 Then
PTab = PTab + 20
ElseIf .Col = 2 Then
PTab = PTab + 25
ElseIf .Col = 3 Then
PTab = PTab + 15
ElseIf .Col = 4 Then
PTab = PTab + 15
ElseIf .Col = 5 Then
PTab = PTab + 20
ElseIf .Col = 6 Then
PTab = PTab + 16
ElseIf .Col = 7 Then
PTab = PTab + 16
ElseIf .Col = 8 Then
PTab = PTab + 16
ElseIf .Col = 9 Then
PTab = PTab + 16
ElseIf .Col = 10 Then
PTab = PTab + 15
ElseIf .Col = 11 Then
PTab = PTab + 15
End If
'Send the field to the print line and add the tab.
Printer.Print Trim$(.Columns(J).Caption); Tab(PTab);
Next
Printer.Print
For I = 0 To .ApproxCount - 1
PTab = 0 'Set the first tab value
'This checks to see if a page break is needed
If Printer.CurrentY + Printer.TextHeight(.Text) > Printer.ScaleHeight - 600 Then
Printer.NewPage
End If
For J = 0 To .Columns.Count - 1
.Col = J 'Set the current column
If .Col = 0 Then
PTab = PTab + 18
ElseIf .Col = 1 Then
PTab = PTab + 20
ElseIf .Col = 2 Then
PTab = PTab + 25
ElseIf .Col = 3 Then
PTab = PTab + 15
ElseIf .Col = 4 Then
PTab = PTab + 15
ElseIf .Col = 5 Then
PTab = PTab + 20
ElseIf .Col = 6 Then
PTab = PTab + 16
ElseIf .Col = 7 Then
PTab = PTab + 16
ElseIf .Col = 8 Then
PTab = PTab + 16
ElseIf .Col = 9 Then
PTab = PTab + 16
ElseIf .Col = 10 Then
PTab = PTab + 15
ElseIf .Col = 11 Then
PTab = PTab + 15
End If
'Send the field to the print line and add the tab.
Printer.Print Trim$(.Text); Tab(PTab);
Next
Printer.Print
On Error Resume Next
.Row = .Row + 1 'Set the active row
On Error GoTo 0
Next I
End With
Printer.EndDoc
End Sub