Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO PRINT A DATAGRID

Status
Not open for further replies.

Hiccup

Programmer
Jan 15, 2003
266
US
I have a DataGrid on a Form. The DataGrid is connected to an Access 2K Recordset via an Adodc. I'd like to add a Command Button to the Form that, when clicked, will print the Records shown on the DataGrid onto 11X17 landscaped paper.

Does anyone know the code to accomplish this?

Thanks in advance!
 
If you are using a record set you can send it to the Data Report.

I do that manually with a For, line by line sending to the printer each line.
 
Thanks bandazagorath! I'll give it a shot.
 
Bandazagorath

I'm having a little trouble get it to work correctly. Do have any sample code you could show me?

Thanks in advance!
 

add a datareport to project

add rptTextBoxes to Detail and heading sections


'++++++++++++++++++++++++++++++++++++++++++++++++++++++
Global Altrst As ADODB.Recordset
Global Altcnn As ADODB.Connection
Global AltcnnOpen As Boolean

Attribute VB_Name = "rsts"

Private Const mk_iQueryTimeout As Integer = 2
Private Const mkConnectType As Long = 0 'Jet OLEDB
Public Const SW_SHOWNORMAL As Long = 1 'Shell Window constant
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Const EM_SETREADONLY As Long = &HCF ' = 207 SendMessage constant
Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wparam As Long, ByVal lParam As Any) As Long

SUB LordAddress ()
Dim PrintTitle As String
PrintTitle = " LANDLORD ADDRESS "

Sql = "SELECT * FROM ADDRESSforLandlord Order By SortKey"

ExecuteAltSQL Sql, MsgString

With Lad 'DATA REPORT NAME
.DataMember = vbNullString
Set .DataSource = Altrst
.Caption = Trim(PrintTitle)
With .Sections("Section1").Controls
.Item("Lname").DataField = Altrst.Fields(1).Name
.Item("LStreet").DataField = Altrst.Fields(2).Name
.Item("LTown").DataField = Altrst.Fields(3).Name
End With

.Sections("Section2").Controls.Item("TradeAs").Caption = ClientBName
.Sections("Section2").Controls.Item("PrintTitle").Caption = PrintTitle
.Sections("Section2").Controls.Item("Abn").Caption = "ABN : " & ClientABN

'.Sections("Section5").Controls.Item("StdAdminGst").Caption = StdAdminGst

Lad.WindowState = 2

.Show
End With
AltcnnOpen = False
set Altcnn = Nothing
SET Altrst = Nothing
End Sub
Public Function ConnectString(Optional ByVal ConnectMethod As Long = 0) As String
'returns a DB ConnectString
Select Case ConnectMethod
Case 1
'Jet
ConnectString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & App.Path & "\Pman.mdb;" & _
"DefaultDir=" & App.Path & ";" & _
"UID=;PWD=;"
'doesn't work - gives provider incapable error
' Case 2
' 'System DSN
' ConnectString = "DSN=NWTest0007;UID=admin;PWD=;"
Case Else
'default = OLEDB
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Pman.mdb"
End Select
End Function


Public Function ExecuteAltSQL(ByVal Sql As String, MsgString As String) As ADODB.Recordset
'executes SQL and returns Recordset

Dim sTokens() As String
On Error GoTo ExecuteAltSQL_Error
Sql = Trim(Sql)
MsgString = ""
sTokens = Split(Sql)
If AltcnnOpen = False Then
Set Altcnn = New ADODB.Connection
Altcnn.Open ConnectString(mkAltConnectType)
End If
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
Altcnn.Execute Sql
MsgString = sTokens(0) & " query successful"
Else
Set Altrst = New ADODB.Recordset
Altrst.Open Trim$(Sql), Altcnn, adOpenKeyset, adLockOptimistic
Altrst.MoveFirst
Set ExecuteAltSQL = Altrst


Altrst.MoveLast

'MsgString = CStr(Altrst.RecordCount) & " records found from SQL"
End If
ExecuteAltSQL_Exit:

Exit Function

ExecuteAltSQL_Error:

MsgString = "ExecuteAltSQL Error: " & Err.Description
MsgString = Str(Err.Number)


Resume ExecuteAltSQL_Exit
End Function
Public Function ExecuteLgSQL(ByVal Sql As String, MsgString As String) As ADODB.Recordset
'executes SQL and returns Recordset

Dim sTokens() As String
On Error GoTo ExecuteLgSQL_Error
Sql = Trim(Sql)
MsgString = ""
sTokens = Split(Sql)
If cnnOpenLG = False Then
Set cnnLG = New ADODB.Connection
cnnLG.Open ConnectString(mkAltConnectTypeLg)
End If
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnnLG.Execute Sql
MsgString = sTokens(0) & " query successful"
Else
Set rstLG = New ADODB.Recordset
rstLG.Open Trim$(Sql), cnnLG, adOpenKeyset, adLockOptimistic
rstLG.MoveFirst
Set ExecuteLgSQL = rstLG
rstLG.MoveLast

'MsgString = CStr(RstLg.RecordCount) & " records found from SQL"
End If
ExecuteLgSQL_Exit:
Exit Function

ExecuteLgSQL_Error:

MsgString = "ExecuteLgSQL Error: " & Err.Description
MsgString = Str(Err.Number)


Resume ExecuteLgSQL_Exit
End Function

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top