?
Solved

get parameter for crystal report from VB automatically at runtime

Posted on 2009-04-10
1
Medium Priority
?
574 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
[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
1 Comment
 
LVL 1

Accepted Solution

by:
dpbouchard earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

718 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