get parameter for crystal report from VB automatically at runtime

Posted on 2009-04-10
Last Modified: 2012-05-06
Dear experts,

I'm using crystal reports to display sales record between 2 particular dates from VB6. I'm able to accomplish this by passing 2 parameters startdate and enddate. Currently, at VB runtime, whenever i call the crystal report procedure, it will prompt the user to enter these 2 dates. Is there anyway that i can pass these 2 values from VB directly? i put the 2 dates in vb textboxes. Any advice please experts? Thx u
Question by:Slythie
1 Comment

Accepted Solution

dpbouchard earned 500 total points
ID: 24120803
I have done this for a client of mine by developing a dll that returns back an XML string of the parameter values that the user desired which is then in turn passed into CR directly without the user needing to deal with the CR prompts which are less than friendly.  I originally did this to allow the user to avoid the CR prompts and to also allow the dll (which in my case actually was a winform) to do some sensible things, lke always suggest a start date of the prior Monday and then suggest an end date of the following sunday so that the user could just hit the return key and the CR engine would then get the parameters without the user having to type it in.  It is very complex .NET VB code that I originally did in VS 2002 and I've kept enhancing it thru VS 2008.  Not sure if this could be done in VB6 because it does need to use the application domain loading capability of loading a DLL into memory and kicking it off and I just don't remember if VB6 could do it.  So, bottom line, it can be done but I just haven't worked in VB6 for so long, I don't know if you can initiate a DLL from within VB6.  Perhaps you could just use the XML trick of passing in the values which I'll show here as a code snippet.  The XML parameters get passed back as a result of the ToString method of the DLL.  Looking further into this program I wrote, the real magic happens in the last routine shown below which takes the XML parameters and stuffs them into the CR parameter values.  So, perhaps that's all you need to do.  The program we wrote does way more than what you probably need, multi-threaded reports, customizable winforms to determine the parameters, etc.  Check out the last routine and see if that helps you.  Good luck...
Imports System.Xml

Imports System.Web

Imports System.Windows.forms

Public Class formParameters

    Private Sub Parameters_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim dtmMonthAgo As Date = Date.Today.AddMonths(-1)

        dtpStart.Value = GetFirstOfMonth(dtmMonthAgo)

        dtpEnd.Value = GetEndOfMonth(dtmMonthAgo)

    End Sub

    Public Overrides Function ToString() As String

        Dim xmldocParameters As New XmlDocument

        Dim xmlelmParameter As XmlElement

        Dim xmltxtParameter As XmlText

        xmlelmParameter = xmldocParameters.CreateElement("Parameters")


        xmlelmParameter = xmldocParameters.CreateElement("Customer")

        xmltxtParameter = xmldocParameters.CreateTextNode(HttpUtility.HtmlEncode(txtCustomer.Text))



        xmlelmParameter = xmldocParameters.CreateElement("Project")

        xmltxtParameter = xmldocParameters.CreateTextNode(HttpUtility.HtmlEncode(txtProject.Text))



        xmlelmParameter = xmldocParameters.CreateElement("Employee")

        xmltxtParameter = xmldocParameters.CreateTextNode(HttpUtility.HtmlEncode(txtEmployee.Text))



        xmlelmParameter = xmldocParameters.CreateElement("Start")

        xmltxtParameter = xmldocParameters.CreateTextNode(dtpStart.Value.ToString("yyyy-MM-dd"))



        xmlelmParameter = xmldocParameters.CreateElement("End")

        xmltxtParameter = xmldocParameters.CreateTextNode(dtpEnd.Value.ToString("yyyy-MM-dd"))



        Return xmldocParameters.OuterXml

    End Function

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        If IsValid() Then Me.Close()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click


    End Sub

    Friend Function IsValid() As Boolean

        If dtpEnd.Value < dtpStart.Value Then

            MessageBox.Show("Start date should precede the end date. Your dates will be swapped." & vbCrLf & "Please reconfirm and click OK again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)

            Dim dtmTemp As Date = dtpEnd.Value

            dtpEnd.Value = dtpStart.Value

            dtpStart.Value = dtmTemp

        End If

    End Function

    Friend Function GetFirstOfMonth(ByVal dtm As Date) As Date

        Return New Date(dtm.Year, dtm.Month, 1)

    End Function

    Friend Function GetEndOfMonth(ByVal dtm As Date) As Date

        Return GetFirstOfMonth(dtm).AddMonths(1).AddDays(-1)

    End Function

End Class

The below routine is what calls the IPlugin entry to launch the routine.

Imports System.Xml

Imports System.Windows.Forms

Public Interface IPluginEntry

    Function Launch() As String

End Interface

Public Class Entry

    Inherits MarshalByRefObject

    Implements IPluginEntry

    Public Function Launch() As String Implements IPluginEntry.Launch

        Dim frmParameters As New formParameters

        If frmParameters.ShowDialog() = DialogResult.OK Then

            Return frmParameters.ToString()


            Return Nothing

        End If

    End Function

    Public Overrides Function ToString() As String

        Return Launch()

    End Function

End Class

This might be all you need...

    Private Sub SetParameters(ByVal xml As XmlDocument)


            If xml Is Nothing Then Exit Sub

            Dim xn As XmlNode

            For Each pf As ParameterField In _clsReportDocument.ParameterFields

                xn = xml.DocumentElement.Item(pf.Name)

                If Not (xn Is Nothing) Then

                    Dim pvs As New ParameterValues

                    Dim pv As New ParameterDiscreteValue

                    pv.Value = xn.InnerText()


                    pf.CurrentValues = pvs

                End If


        Catch exAbort As ThreadAbortException


            If Not finish Is Nothing Then finish(_clsReport)

        Catch ex As Exception


        End Try

    End Sub

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now