?
Solved

Filter Crystal Report with date range on VB.net

Posted on 2005-03-29
4
Medium Priority
?
5,674 Views
Last Modified: 2008-07-01
My Report.rpt has two parameters "datestart" and "dateend", and
the form has two DTPickers to filter result.
Following codes work well on VB6, now I am learning VB.NET, could someone help me to achieve such function on VB.NET? many thanks.

Dim Appl As New CRAXDRT.Application
Dim Report As New CRAXDRT.Report
DTPicker1.Value = Format(Now, "DD/MM/YY")
DTPicker2.Value = Format(Now, "DD/MM/YY")
Set Report = Appl.OpenReport(App.Path & "\" & "Report.rpt")
CRViewer91.ReportSource = Report
Report.EnableParameterPrompting = False
Report.ParameterFields.GetItemByName("datestart").AddCurrentValue DTPicker1.Value
Report.ParameterFields.GetItemByName("dateend").AddCurrentValue DTPicker2.Value
CRViewer91.ViewReport
0
Comment
Question by:gavvic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:planocz
ID: 13654158
Sample code....

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared

  Public myReport As New ReportDocument        'Gobal Report Doc

  Private Sub Parmas(ByVal myReport) 'FOR Dates and Ranges
        ' Objects used to set the parameters in the report
        Dim pvCollection As New ParameterValues
        Dim pdvID As New ParameterDiscreteValue
        Dim pdvStartName As New ParameterDiscreteValue
        Dim pdvEndName As New ParameterDiscreteValue
        Try
                '********************************* ID **********************************
                ' Set the discreet value to the Start Date.
                pdvID.Value = sID
                ' Add it to the parameter collection.
                pvCollection.Add(pdvID)
                ' Apply the current parameter values.
                myReport.DataDefinition.ParameterFields("Id").ApplyCurrentValues(pvCollection)

                '********************************* Start Date **********************************
                pdvStartName.Value = sStartDate
                pvCollection.Add(pdvStartName)
                myReport.DataDefinition.ParameterFields("Start Date").ApplyCurrentValues(pvCollection)

                '********************************* End Date **********************************
                pdvEndName.Value = sEndDate
                pvCollection.Add(pdvEndName)
                myReport.DataDefinition.ParameterFields("End Date").ApplyCurrentValues(pvCollection)
   
        Catch Exp As ParameterFieldException
            MsgBox("Incorrect Parameter Field." & vbNewLine & "In Parmas Procedure Code", _
                    MsgBoxStyle.Critical, "Load Report Error")
        Catch Exp As InvalidArgumentException
            MsgBox(Exp.Message & vbNewLine & "In Parmas Procedure Code", MsgBoxStyle.Critical, "General Error")
        Catch Exp As Exception
            MsgBox(Exp.Message & vbNewLine & "In Parmas Procedure Code", MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub

 Private Sub ViewCR_PDF()
        sReportName = "MYReport.rpt"
        GetReportConnection(myReport)
        Parmas(myReport)
        Try
            crDateView.DisplayGroupTree = False
            ' Set the report source for the crystal reports viewer to the report instance.
            'crDateView is the CR Viewer
             crDateView.ReportSource = myReport
            'Good for total pages of all of the query reported
            'Dim str As String
            'str = myReport.FormatEngine.GetLastPageNumber(New ReportPageRequestContext)

            ' Zoom viewer to fit to the whole page so the user can see the report
            crDateView.Zoom(1) '1 is page width and 2 is view whole page)
            'Go get the Last Page Count Number
            crDateView.ShowLastPage()
            iPageCount = crDateView.GetCurrentPageNumber.ToString
        Catch Exp As LoadSaveReportException
            MsgBox("Incorrect path for loading report.", _
                    MsgBoxStyle.Critical, "Load Report Error")
        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub

   Private Sub GetReportConnection(ByVal myReport)
        ' Objects used to set the proper database connection information
        Dim tbCurrent As CrystalDecisions.CrystalReports.Engine.Table
        Dim tliCurrent As CrystalDecisions.Shared.TableLogOnInfo

        Try
            ' Load the report
            myReport.Load(sMYPath & sReportName, CrystalDecisions.[Shared].OpenReportMethod.OpenReportByTempCopy)
            ' Set the connection information for all the tables used in the report
            For Each tbCurrent In myReport.Database.Tables
                tliCurrent = tbCurrent.LogOnInfo
                With tliCurrent.ConnectionInfo
                    .ServerName = sDBServer
                    .UserID = sUSERid
                    .Password = ""
                    .DatabaseName = sDBName
                End With
                tbCurrent.ApplyLogOnInfo(tliCurrent)
            Next tbCurrent
        Catch Exp As LoadSaveReportException
            MsgBox("Incorrect path for loading report.", _
                    MsgBoxStyle.Critical, "Load Report Error")
        Catch Exp As Exception
            MsgBox(Exp.Message & "   in GetReportConnection Procedure", MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub

0
 

Author Comment

by:gavvic
ID: 13670111
Thank you for your reply, planocz. But I can not get it work with your sample code.  Lots of not declared items appear when I have tried it.

Is it possible that we could use "RecordSelectionFormula = " something like that to make the filter easier? or could you please give me more sources to understand more. Yep, I am just a starting out VB.net.
0
 
LVL 27

Accepted Solution

by:
planocz earned 500 total points
ID: 13671420
the Imports --- you have to add in the references
All the small s words like -- sDBName -- there string var's that you have to add.

That should do it.

Here is where I found good examples for getting started in VB.Net

.....http://msdn.microsoft.com/vbasic/downloads/samples/101samples.aspx

Link for ASP programs...

http://msdn.microsoft.com/asp.net/downloads/kits/default.aspx 


0
 

Author Comment

by:gavvic
ID: 13691156
After I have fixed my code, I get the result. Thanks for your help planocz.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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