print an access report through visual basic

print an access report through visual basic

Post by Pucc » Wed, 14 May 2003 01:34:45



Hi,
I'm trying to print an access report through visual basic, but I have
2 report that I made in access. the first one is to print all the
report and the other one, the user must enter the name of the
department to print the report of that department.
I don't understand preety much access, so I would like to know if
there are any way that vb could print the report without open the
access.

Here's the code:
Private Sub Label7_Click()
On Error Resume Next
strReportName = "relatorio_escolha"

'Assigns an object reference to a variable or property.
Set appAccess = New Access.Application

With appAccess
'Abrir a base de dados
.OpenCurrentDatabase constrDBName

.Visible = True

'Abrir o relatrio para Preview
'.DoCmd.OpenReport strReportName, acViewPreview --> already comment
this code to prevent previewing in access.

'Imprimir o relatrio
.DoCmd.OpenReport strReportName, acViewNormal --> before print, the
access is open and a inputbox is open in access where the user must
enter the name of the department. I want if is possible, instead of
opening access to prompt the inputbox, can the inputbox prompt in
visual basic?
Because the users can't access to the database

End With

Set appAccess = Nothing
' erros
If Err.Number = 3011 Then
MsgBox ("Form with 'Old' name cannot be found, maybe it has already
been renamed.")
    appAccess.Quit
    Set appAccess = Nothing
    Exit Sub
ElseIf Err.Number = 7866 Then
    MsgBox (strDBpath & " has been opened exclusively by another
user.")
    appAccess.Quit
    Set appAccess = Nothing
    Exit Sub
End If
End Sub

Thanks!

     Pucca

 
 
 

print an access report through visual basic

Post by Dave War » Wed, 14 May 2003 14:36:49


Hi Pucca

Cheers
Dave

dblongName is the filepath
dbShortname is the mdb file
rptName is the name of the rpt.

rptWhere is an SQL query
the rptWhere is where you can specify the criteria via VB e.g.
rptWhere = "MyField = 'myCriteria'" (assumes a text datatype.)
The myCriteria can be passed in as a variable:
Dim myCriteria As String
rptWhere = "MyField = '" & myCriteria & "'"
and myCriteria can be obtained from the user via a VB InputBox

Public Sub PrintAccessReport(dbLongName As String, _
                             dbShortName As String, _
                             rptName As String, _
                             Optional rptFilter As Variant, _
                             Optional rptWhere As Variant)

On Error GoTo PrintAccessReport_Err

If rptName = "" Then
MsgBox "Please specify a report name", vbExclamation, _
         "No Report Specified"
Exit Sub
End If

Dim strDB As String

strDB = GetSetting(appname:=App.Title, Section:="Database", _
            Key:=dbShortName, Default:=dbLongName)

Dim objAccess As Object
Set objAccess = CreateObject("Access.Application")

    With objAccess
            ' open the database
        .OpenCurrentDatabase filepath:=strDB
            ' send the report report to printer.
        .DoCmd.OpenReport rptName, 0, rptFilter, rptWhere
        DoEvents   ' off it goes to the printer
    End With

' tidy up
Set objAccess = Nothing
Exit Sub

PrintAccessReport_Err:
MsgBox Error$(), vbInformation, " Access Database Automation Error"
End Sub

' **Print Report
'Private Sub PrintReport_Click()
'Dim db As String
'Dim rptName As String
'db = "C:\...\...\Database\MyDB.mdb"
'rptName = "My Report Name"
'
'PrintAccessReport db,  , rptName, , "MyField = '" & myCriteria & "'"
'End Sub


> Hi,
> I'm trying to print an access report through visual basic, but I have
> 2 report that I made in access. the first one is to print all the
> report and the other one, the user must enter the name of the
> department to print the report of that department.
> I don't understand preety much access, so I would like to know if
> there are any way that vb could print the report without open the
> access.

> Here's the code:
> Private Sub Label7_Click()
> On Error Resume Next
> strReportName = "relatorio_escolha"

> 'Assigns an object reference to a variable or property.
> Set appAccess = New Access.Application

> With appAccess
> 'Abrir a base de dados
> .OpenCurrentDatabase constrDBName

> .Visible = True

> 'Abrir o relatrio para Preview
> '.DoCmd.OpenReport strReportName, acViewPreview --> already comment
> this code to prevent previewing in access.

> 'Imprimir o relatrio
> .DoCmd.OpenReport strReportName, acViewNormal --> before print, the
> access is open and a inputbox is open in access where the user must
> enter the name of the department. I want if is possible, instead of
> opening access to prompt the inputbox, can the inputbox prompt in
> visual basic?
> Because the users can't access to the database

> End With

> Set appAccess = Nothing
> ' erros
> If Err.Number = 3011 Then
> MsgBox ("Form with 'Old' name cannot be found, maybe it has already
> been renamed.")
>     appAccess.Quit
>     Set appAccess = Nothing
>     Exit Sub
> ElseIf Err.Number = 7866 Then
>     MsgBox (strDBpath & " has been opened exclusively by another
> user.")
>     appAccess.Quit
>     Set appAccess = Nothing
>     Exit Sub
> End If
> End Sub

> Thanks!

>      Pucca