CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Mar 2000
    Posts
    292

    Passing Parameter from VB to Crystal Report

    Hi.

    I just would like to ask how to pass parameter data from VB to Crystal Report. I have created reports before but these only use groupings and sortings and just displaying data from the database. What if, for instance, certain values have to be user-inputted, must be displayed in the report itself and must be used in filtering records, such as in the sample report heading below:

    Cost of Goods Sold
    Branch: {branch_name}
    From {date1} To {date2}
    Printed on: {date_today}

    where {branch_name}, {date1}, and {date2} are all user-supplied and {date_today} is the system date. How can I pass these data from VB to the report? Does the Crystal Report version matter in the procedure for passing data? I'm using Crystal Report v4.6.

    Thanks for the info in advance!


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Passing Parameter from VB to Crystal Report

    Have a look here (second example may help you)
    http://codeguru.com/cgi-bin/bbs/wt/s...age=&view=&sb=
    You may also make a search here (look on top, where there are:
    Forum Index|Faq|...|Search|Unanswered|... and click on "Serach") for
    the word "Crystal".

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Mar 2000
    Posts
    292

    Re: Passing Parameter from VB to Crystal Report

    Thanks for the reply.

    Actually, I tried passing values, displaying them in the report, and using them in record filtering. I tried different data types such as string and numeric (integer, currency) and encountered no problem. But when I used a date type, I encountered this error, pointing to the .Action = 1 line:

    Run-time error: '20515':
    Error in formula <Record_Selection>
    '{Profile.BDAY}>=01/01/2000 AND {Profile.BDAY}<=12/31/2999'
    A date is required here

    Below is my code:

    Code:
    Private Sub CmdShowReport_Click()
    
        Dim strFromBDay As String, strToBDay As String
        Dim strFromAge As String, strToAge As String
        Dim strName as String
        Dim zAmt As Currency
        Dim zCount As Integer
         
        strName = "dan"
        strFromBDay = "1/1/2000"
        strToBDay = "12/31/2999"
        strFromAge = "1"
        strToAge = "10"
        zAmt = 100
        zCount = 5
         
        With CrystalReport1
            .Destination = crptToWindow
            .DiscardSavedData = True
            .Formulas(0) = "FromAge=" & strFromAge
            .Formulas(1) = "FromBDay='" & strFromBDay & "'"
            .Formulas(2) = "Name='" & strName & "'"
            .Formulas(3) = "ToAge=" & strToAge
            .Formulas(4) = "ToBDay='" & strToBDay & "'"
            .Formulas(5) = "zAmt=" & zAmt
            .Formulas(6) = "zCount=" & zCount
            .SelectionFormula = "{Profile.BDAY}>=" & CDate(strFromBDay) & " AND {Profile.BDAY}<=" & CDate(strToBDay)
            '.SelectionFormula = "{Profile.AGE}>=" & strFromAge & " and {Profile.AGE}<=" & strToAge
            '.SelectionFormula = "{Profile.NAME}='dan'"
            '.SelectionFormula = "{Profile.AMT}=" & zAmt
            '.SelectionFormula = "{Profile.COUNT}=" & zCount
            .WindowTitle = "Sample Report"
            .ReportFileName = App.Path & "\Reports\Walalang.rpt"
            .Action = 1
            .Formulas(0) = "FromAge=''"
            .Formulas(1) = "FromBDay=''"
            .Formulas(2) = "Name=''"
            .Formulas(3) = "ToAge=''"
            .Formulas(4) = "ToBDay=''"
            .Formulas(5) = "zAmt=''"
            .Formulas(6) = "zCount=''"
        End With
    
    End Sub
    Commented lines ran successfully without errors. The BDAY field is of Date/Time data type. FromAge, FromBDay, Name, ToAge, ToBDay, zAmt, and zCount are all formula fields in the report. Any suggestions? Thanks!
    Last edited by Cimperiali; June 1st, 2003 at 02:56 PM.

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Passing Parameter from VB to Crystal Report

    Code:
    .SelectionFormula = "{Profile.BDAY}>=" & CDate(strFromBDay) & " AND {Profile.BDAY}<=" & CDate(strToBDay)
    Cdate is not the right function:
    try this to see:

    Code:
    Dim strFRomBday As String
    strFRomBday = "10/12/2001"
    MsgBox Format(strFRomBday, "dd/mm/yyyy")
    strFRomBday = "10,12,2001"
    MsgBox CDate(strFRomBday)
    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    Last edited by Cimperiali; June 1st, 2003 at 02:57 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Mar 2000
    Posts
    292

    Re: Passing Parameter from VB to Crystal Report

    Thanks again for the response. I tried using the "01,01,2001" format and the CDate function again in the .SelectionFormula line, but still I got the same error. I don't think I got your answer right. Why is it that for date types, I'm having this problem whereas for other data types, the code worked fine. Please help.


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Passing Parameter from VB to Crystal Report

    In your case it may be:
    "01/01/2001"
    or
    "01:01:2001"
    or
    "01;01;2001"
    depending on general settings in your control panel... Have a try.

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Mar 2000
    Posts
    292

    Re: Passing Parameter from VB to Crystal Report

    Hi again.

    The date format in my Regional Settings is "01/01/2001". Actually, it is not in the assigning of values to the formulas in Crystal Report that produces the error, it is in the .SelectionFormula line itself. This is a simple record filtering process, what seems to be the error?


  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Passing Parameter from VB to Crystal Report

    sql = "{persona.Cognome}='Imperiali'"
    this means you have a table named "persona" in which there is a field named "Cognome" and it is like you write
    select....where persona.Cognome='Imperiali'
    you have to:
    1)match exact case (upper/lower letters)
    2)be sure to have selected fields in query that you would like to use in where clause
    3) match the type:
    for string use 'xxx'
    for number avoid the ' and '
    for date:
    try with: '#yourdate#'
    or with no ' like if it was a number

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    Mar 2000
    Posts
    292

    Re: Passing Parameter from VB to Crystal Report

    Yes, you're right. The .SelectionFormula can easily be translated into SQL statements, and I didn't have errors when using other data types. I have done #1, and #3, though it is in the date data type that I encounter errors. I also tried to put # before and after in the .SelectionFormula but another error appeared. For #2, where will this SQL query be placed? I have done everything, from manipulating codes in VB to manipulating formulas and record selection properties in Crystal, but to no avail. I can't figure out what's wrong with date data type!


  10. #10
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Passing Parameter from VB to Crystal Report


    'You may use a parameter field for the date
    'and then use a formula inside a section to filter records

    '(Special thanks to SurrenderMonkey for this)
    'A simply report with one parameter field
    'called SelectMe in a omit formula for a section.
    'Matter is not to make the selection but to pass a parameter
    'value to a rpt file
    'or: to make parameter fields to be filled
    'via code form VB.
    Cr1.Reset
    'your rpt file
    Cr1.ReportFileName = "D:\Condivisa\Cesarevb\Crystol\cryweb\Report1.rpt"
    Cr1.ReportSource = 0
    Cr1.DiscardSavedData = true
    'instead of "1", use the data
    Cr1.ParameterFields(1) = "SelectMe;1;false" 'parameter field name={?SelectMe}
    'Cr1.ParameterFields(0) = "SelectMe;1;false"
    Cr1.Action = 1 'make it print




    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  11. #11
    Join Date
    Mar 2000
    Posts
    292

    Re: Passing Parameter from VB to Crystal Report

    I'm using Crystal Report v4.6. I didn't find Parameter Fields in any of the menus. In recent Crystal versions, there is a Field option in the Insert menu, where you can specify a parameter field. Where will I filter records?


  12. #12
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Passing Parameter from VB to Crystal Report

    Then I think you should try different kinds of format till you find the correct
    sintax. Sorry I could not help more.

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  13. #13
    Join Date
    Mar 2000
    Posts
    292

    Re: Passing Parameter from VB to Crystal Report

    Okay. Thanks for all the suggestions. I'll just try different formats for the Crystal Report and in the VB code.


  14. #14
    Join Date
    Aug 2001
    Location
    India
    Posts
    173

    Re: Passing Parameter from VB to Crystal Report

    For date fields, you have use this function Datetime something like
    CrystalReport.recordselectionformula = "{DataEnvironment1.Command1.BirthDate = DateTime(Year,Month,Day,0,0,0}"

    This is the way how I am working, even today also.


    If, this, doesn't work, reply back.


  15. #15
    Join Date
    Mar 2000
    Posts
    292

    Re: Passing Parameter from VB to Crystal Report

    Thanks for the reply. I just would like to ask what's the DataEnvironment1.Command1 for? Well, I commented my .SelectionFormula line and just edited the formula in the Select Records Expert in Crystal Report. The report appeared, but couldn't get the correct records that pass the condition. My formula in the report is this:

    ToText(Year ({Profile.BDAY}))>= {@FromBDay} [7 to 10] and ToText(Year ({Profile.BDAY}))<= {@ToBDay} [7 to 10]

    BDAY is a Date/Time field in Long Date format in the Profile table. This means that only those records having BDAY year within the two years specified must appear in the report. For example, if @FromBDay = "01/01/1900" and @ToBDay = "12/31/2000", then {@FromBDay} [7 to 10] = "1900" and {@ToBDay} [7 to 10] = "2000", and the report must show only those records born from 1900 to 2000. But the report could not detect this. Any ideas?



Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured