Solved

get parameter for crystal report from VB automatically at runtime

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make an ADE file by code? 11 87
Date is within the current week in Crystal Reports 2 61
Selection between two dates and time range. 21 31
Advice in Xamarin 21 79
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 …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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