Solved

get parameter for crystal report from VB automatically at runtime

Posted on 2009-04-10
1
527 Views
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
0
Comment
Question by:Slythie
1 Comment
 
LVL 1

Accepted Solution

by:
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")

        xmldocParameters.AppendChild(xmlelmParameter)
 

        xmlelmParameter = xmldocParameters.CreateElement("Customer")

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

        xmldocParameters.DocumentElement.AppendChild(xmlelmParameter)

        xmldocParameters.DocumentElement.LastChild.AppendChild(xmltxtParameter)
 

        xmlelmParameter = xmldocParameters.CreateElement("Project")

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

        xmldocParameters.DocumentElement.AppendChild(xmlelmParameter)

        xmldocParameters.DocumentElement.LastChild.AppendChild(xmltxtParameter)
 

        xmlelmParameter = xmldocParameters.CreateElement("Employee")

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

        xmldocParameters.DocumentElement.AppendChild(xmlelmParameter)

        xmldocParameters.DocumentElement.LastChild.AppendChild(xmltxtParameter)
 

        xmlelmParameter = xmldocParameters.CreateElement("Start")

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

        xmldocParameters.DocumentElement.AppendChild(xmlelmParameter)

        xmldocParameters.DocumentElement.LastChild.AppendChild(xmltxtParameter)
 

        xmlelmParameter = xmldocParameters.CreateElement("End")

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

        xmldocParameters.DocumentElement.AppendChild(xmlelmParameter)

        xmldocParameters.DocumentElement.LastChild.AppendChild(xmltxtParameter)
 

        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

        Me.Close()

    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()

        Else

            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)
 

        Try

            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()

                    pvs.Add(pv)

                    pf.CurrentValues = pvs

                End If

            Next

        Catch exAbort As ThreadAbortException

            Thread.ResetAbort()

            If Not finish Is Nothing Then finish(_clsReport)

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try
 

    End Sub

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

705 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

18 Experts available now in Live!

Get 1:1 Help Now