Solved

get parameter for crystal report from VB automatically at runtime

Posted on 2009-04-10
1
555 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

821 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