borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Could not decrypt file ??? ERROR)Next Topic (how to save a file from one location to another) New Topic New Poll Post Reply
AndreaVB Forum : VB General : report
Poster Message
aneesa
Level: Graduate

Registered: 16-04-2005
Posts: 11

icon report

i want to know

from vb to print the report can we connect it to the MS access report?? if it is possible??

or how can i create a report in vb to print

i am created form with student detail such as rollno,name,sex,class,birthdate etc and command buttons to add ,delete,search. at onetime i can add only one record.
(when add button clicked clear function i added). if there is any possibility to do this manytimes?? otherwise i want to exit the program and then start.

please help me.i am new to vb

thanks in advance

21-04-2005 at 08:19 AM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1658
icon Re: report

I believe you have to actually create a reference to MS Access... then use a function of that object to open up the report (which actually opens the database in access, and opens the report).

Although if the application's being distributed to people that don't have MS Access (and may cause problems if the version is different), the program won't work that way.

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

21-04-2005 at 08:21 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 571
icon Re: report

Hi,
Quite correct - you do have to add a reference to the MSAccess Object Library, create the Application Object, Open it, then use its DoCmd action to print the Access Report.
This is OK all the time you know that users will have a copy of Access on their PC's.
Alternatively - you have several choices.
1. VB comes with a report designer (it is actually a cut down version of Crystal Reports). Personnally I find it very limited - particularly when you compare it to the power you have got with the Access reports generator.

2. You could buy a Developer copy of Crystal Reports which has a control that you can place onto your form that allows you to open a pre-designed report. All I would say on that, apart from the fact that you have to spend some more money, is that if you are used to designing reports using MSAccess then you will find Crystal Reports will take some getting used to.

3. You can use the "Printer" object in VB to output directly to the printer.  (Printer.Print puts a blank line into the report)

Printer.FontName = "Tahoma"
Printer.FontSize = 12
Printer.FontBold = True
Printer.FontUnderline = True
strText = "Student - " & rptRST("StudentName")
Printer.Print strText
Printer.Print

The advantages to this are that it is very quick, but you have to write a fair bit of code, and as this is not a "graphical" way to design a report it is sometimes difficult to get a picture of it in your head. The downside here is that ther is no Screen Preview of the report - it goes straight to the printer.

4. Put a RichTextBox Control onto a Form, use code to assemble strings from your data recordset as in the Printer method, but then put the string into the RTB.
(Lets call it rtbDisplay)

rtbDisplay.SelFontName = "Tahoma"
rtbDisplay.SelFontSize = 12
rtbDisplay.SelBold = True
rtbDisplay.SelUnderline = True

strText = strText = "Student - " & rptRST("StudentName") & vbCrLf
rtbDisplay.SelText = strText

Note here that we have to put the CrLf at the end to move to the next line - the Printer.Print method does that for us automatically.
Now you can open the form to preview the report on screen, and if you put a Command Button and a CommonDialog Control onto the form you can call up the Print Dialog and print out to a selected printer :-

Private Sub cmdPrint_Click()
On Error GoTo cmdPrintErr

cdl1.Flags = cdlPDReturnDC + cdlPDNoPageNums
   If rtbDisplay.SelLength = 0 Then
      cdl1.Flags = cdl1.Flags + cdlPDAllPages
      Else
      cdl1.Flags = cdl1.Flags + cdlPDSelection
      End If
   cdl1.ShowPrinter

   rtbDisplay.SelPrint cdl1.hDC
Exit Sub

cmdPrintErr:
If Err.Number <> 32755 Then 'Cancel on Print Dialog was pressed
MsgBox Err.Description, vbExclamation
End If

End Sub





____________________________
multi-tasking - the ability to hang more than one app. at the same time.

21-04-2005 at 10:33 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 933
icon Re: report

Just one or two bits of mine.
The 'VB and VBA for Word and Access' section of this site contains this tip from our beloved admin, where is explained how to call an Access existing report from within our VB project (be careful: the code has to be adapted a little). You better have a look on it. I have made a working code based on this article.

GeoffS' post is a very exhaustive speech and useful for alternative methods (I'll print it), but about Access I have to say that when you create an installation package, the dependencies are read by the package creation manager, so into the setup file it should be contained the MSAccess runtime file, that is free distributable.
So the installed application should run the report even without MSAccess installed.
I placed all these "should" because I made it once, but I don't remember now if it worked or not without MSAccess on target machine (I seem to remember it did, but I'm not sure). I only made a test, but finally the application was ran only on machines with Access installed. I will check it again ASAP anyway, but it'd be worth a check.

____________________________
Real Programmer can count up to 1024 on his fingers

21-04-2005 at 11:34 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 933
icon Re: report

I checked my installation pack.
I ran it into a WinXP machine, with MSAccess97, but my app uses an Access2k db.

Access97 is present but not installed: it means that the C:\ProgramFiles\MSOffice\Office\MSAccess.exe file is present, but there is no link - none of Office links - into the Start menu. I guess is due to an upgrade of the o.s. Anyway, if I double click it, it run. But if I double click the .mdb I copied (MSAcc2k) it doesn't open it.

I installed my app. When run at first time it has prompted me several times with "Unable to register the key HKLM... you don't have the necessary permissions...", and I always clicked on Ignore. It didn't happen in W9x, I think is the admin system of WinXP, but it's a sign it's installing some new features in the registry, and I know I didn't put any in my app. So I'm sure it's tuning up Access2k runtimes. After a while, the program run properly.
I installed another program. Everything's fine. Neither the "Unable to..." windows (I guess it was due to some specific controls) . The program run properly.
I uninstalled the second app: Everything's fine.
I uninstalled first app: only one single "Unable..." window, and I clicked Ignore.
In the end, both apps are uninstalled. Access97 works again, but don't open the Acc2k file.

Finally, I'm quite sure that when you add a reference to MSAccess Objects, and later you build a setup package, it installs MSAccess runtimes on target machine, so your app will work even without MSAccess installed. Quite sure.

You check, too.

____________________________
Real Programmer can count up to 1024 on his fingers

22-04-2005 at 07:58 AM
View Profile Send Email to User Show All Posts | Quote Reply
aneesa
Level: Graduate

Registered: 16-04-2005
Posts: 11
icon Re: report

I made  a report named orderdetails. the code in vb is like this

Private Sub Form_Load()

Dim appaccess As New Access.Application

Set appaccess = New Access.Application
appaccess.OpenCurrentDatabase "C:\Documents and Settings\ADMIN\My Documents\oscomp\database1\company.mdb", True
End Sub

Private Sub cmdprint_Click()
appaccess.DoCmd.OpenReport "orderdetails", acViewNormal
End Sub

when executing this it directly goes to print. i didn't connect the printer. i just want to see the report. can i see the report using this code?

please help me

30-04-2005 at 01:59 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 933
icon Re: report

I told it has to be adapted a little.
Following I post a working function I made, to view (in print preview) a report into an existing .mdb file.
When I wrote it, I put a description saying Access has to be installed on target machine, but now I am not that sure anymore (see my previous post) . If it has not, it'll be better.

' the Access instance is declared outside the function,
' so remember to set it on Nothing when finished
Dim AccRep As Access.Application

' open an Access instance, open a given .mdb file, even password-protected,
' and open a specific report in this file.
' requires a Microsoft Access (9.0) Object Library reference, a MS DAO 3.6
' reference, and moreover requires that Access is installed on target machine
Public Sub OpenAccessReport(reportname As String, mdbname As String, _
    Optional dbpassword As String = "", Optional PrintThisReport As Boolean = False)
    Set AccRep = New Access.Application
    AccRep.Visible = True   ' if I don't put it, it won't show the preview
    AccRep.DoCmd.RunCommand acCmdAppMaximize    ' maximize Access
    ' store an mdb database into a DAO variable (necessary to open a password .mdb)
    ' Notice: I didn't find a way to pass a password using ADO, that's why I need
    '  to use DAO here
    Set db = AccRep.DBEngine.OpenDatabase(mdbname, False, False, ";PWD=" & dbpassword)
    ' force VB to load the currently open db into the Access variable
    AccRep.OpenCurrentDatabase mdbname, False
    db.Close                    ' close the db into the DAO variable
    Set db = Nothing        ' empty the DAO variable (not yet needed)
    AccRep.DoCmd.Minimize   ' minimize MSAccess database window
    AccRep.DoCmd.OpenReport reportname, acViewPreview       ' open the report
    ' next two lines hide completely the database window    
    ' it has to be done after opening the report, otherwise it can't open it
    AccRep.DoCmd.SelectObject acReport, , True
    AccRep.DoCmd.RunCommand acCmdWindowHide         ' hide database window
    If PrintThisReport Then
        AccRep.DoCmd.PrintOut           ' print the report
    End If
End Sub

Usage is as following:
' show a report in print preview
Private Sub btnReport_Click()
    OpenAccessReport repMyReport, App.Path & "\mydb.mdb", "mypassword"
End Sub

Hope it helps

____________________________
Real Programmer can count up to 1024 on his fingers

30-04-2005 at 02:51 PM
View Profile Send Email to User Show All Posts | Quote Reply
aneesa
Level: Graduate

Registered: 16-04-2005
Posts: 11
icon Re: report

i added

appaccess.visible=true

that is working now i can see the report in access.

i didn't check with the printer. hope it will work with the printer also by adding .printout code.

Thank you

02-05-2005 at 07:34 AM
View Profile Send Email to User Show All Posts | Quote Reply
aneesa
Level: Graduate

Registered: 16-04-2005
Posts: 11
icon Re: report

one more question,

i want to create a report based on some condition.

my table contains date. i want to see the report based on tha from to date. in the form i created 2 datapicker for from to dates.based on this selection i want to open the report. how this was possible

Please help me

Thanks in advance

04-05-2005 at 08:19 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 933
icon Re: report

The idea is to create a query that retrieves only some records according with the selected date, then use this query as source for your report.

In general, we always have to base on queries. The concept is: if the query works - it means: return the records I'm expected it did - I can either print the result, or just view it in a form or in a print preview.

So try to build a working query, based on that date. To pass a value from a form to a query, you have to put the value in the query criteria, accordin with the "Forms!MyForm!MyTexbox.Value" format.
And of course the form has to be open when you run the query, even if you can previously store the desired value into a global variable, and run the query via VBA. In this case you can build the query at runtime, and set it as report's RecordSource property via VB code.

Hope it helps.

____________________________
Real Programmer can count up to 1024 on his fingers

04-05-2005 at 11:29 AM
View Profile Send Email to User Show All Posts | Quote Reply
aneesa
Level: Graduate

Registered: 16-04-2005
Posts: 11
icon Re: report

Sir,

    I have visual basic project with MS access Backend.ok.

I created a form in vb to display the report. I want to see the report based on some condition.like this,

i added two datepickers one for From date and other for To date. and also a command button to show the report. (in the report i have date field.) and i click show button i want the report based on this date ie,inbetween these dates. ok i think you get it. you just suggest me what to do.

Reply me

07-05-2005 at 06:43 AM
View Profile Send Email to User Show All Posts | Quote Reply
aneesa
Level: Graduate

Registered: 16-04-2005
Posts: 11
icon Re: report

i got the solution.

i just give the condition in open report procedure

08-05-2005 at 12:18 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : report
Previous Topic (Could not decrypt file ??? ERROR)Next Topic (how to save a file from one location to another) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder