Results 1 to 10 of 10

Thread: Crystal Asking A Parameter

  1. #1

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381

    Question Crystal Asking A Parameter

    When I run my program report, CR always asks for a parameter. I want to handle this paramater prompt in the code. I mean, instead of prompting the user, I want to give the report the value of the parameter during runtime.

    Any ideas?

    Thanks

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The exact code depends on which version of Crystal and how you are executing the report within VB.

    I use this code with the Crystal Reports Engine 8 Object Library (CRPEAuto), to set parameters.

    VB Code:
    1. 'objReport  is declared as a CRPEAuto.Report
    2. With objReport
    3.     .ParameterPromptingEnabled = False
    4.            
    5.     .ParameterFields(1).SetCurrentValue Trim$(objWard.Ward)
    6.     .ParameterFields(2).SetCurrentValue dteFrom
    7.     .ParameterFields(3).SetCurrentValue dteTo
    8. End With

    and this code is used with the Crystal Reports Designer 8.5 ActiveX Runtime Library (CRAXDRT)
    VB Code:
    1. 'objReport is declare as CRAXDRT.Report
    2. With objReport
    3.     .EnableParameterPrompting = False
    4.  
    5.     .ParameterFields(2).AddCurrentValue Trim$(objWard.Ward)
    6.     .ParameterFields(2).AddCurrentValue dteFrom
    7.     .ParameterFields(3).AddCurrentValue dteTo
    8. End With

    Note that if a parameter value has not been set Crystal will still display its dialog box, even if the ParameterPrompting property is False.

  3. #3

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    Thanks.

  4. #4
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Cool bruce would it be possible to show us code to set the object report to the actual report..and any other necessary code needed. I could really use what you had earlier posted to Prince regarding parameters..but ive never opened a report in vb.

    Please let me know...

    Jon

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Sure. For version 8.5 - I usually reference the Crystal Reports 8.5 ActiveX Designer Runtime Library.

    VB Code:
    1. Dim objCrystalApp As CRAXDRT.Application
    2. Dim objReport As CRAXDRT.Report
    3.  
    4. Set objCrystalApp = New CRAXDRT.Application
    5.  
    6. Set objReport = objCrystalApp.OpenReport(ReportPath, 0)
    7.  
    8. 'To Show the report.
    9. 'frmReport is a VB Form that contains the Crystal Viewer Control
    10. 'in this case called Viewer
    11. frmReport.Viewer..ReportSource = objReport
    12. frmReport.Viewer.ViewReport

    These are some of the things to consider but would depend on your situation. Database/Table datasources, Table Locations and SubReports.

    The Report object has Database and Table collections. For each Table used in your report (this includes subreports) you may need to call the SetLogonInfo method.

    I use the direct or oledb methods, no odbc dsn. This means if I develop a report using our development sql server I must change the login information to point to the production server and use the Users login id.

    You also may need to set the Table.Location property.

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    I decided to post this procedure that we use to set the login information and location for each report. Just remember, this has never been used with MS Access only Oracle and SQL Server - stored procedures or linked tables. I will leave you to figure out what is going on, hopefully the procedure comments will help.

    VB Code:
    1. '******************************************************************************************************************
    2. 'Procedure: SetReportDataSources
    3.  
    4. 'Description:  '      Crystal saves the Login Information and Location as part of the report.  This procedure
    5. '      overrides the login information and location for all the report's DatabaseTables including any
    6. '      Subreports.  It assumes all tables use the same login criteria (ie all data is from the same database).
    7. '      It also assumes that all reports are created from stored procedures(System standard).
    8. '      In order to override the Location we need to parse the DatabaseTable.Location property.
    9. '      This allows us to easily support several "environments" but we need to tell the Crystal engine
    10. '      which Login information to use and which stored procedure to use.
    11.  
    12. '      For example - An application has three environments.  Production, QA and Dev.  The QA and Dev
    13. '      databases are called Pubs_Dev and Pubs_QA on server Handel.  A developer creates a report
    14. '      using the Pubs_Dev database and a stored procedure called selAuthorsbyState.  Crystal will save the
    15. '      Location as pubs_dev.dbo.Proc(selAuthorsByState).  Note that the location written does depend on the
    16. '      author's options/preferences. We are not going to rely on a everyone following a "Standard Practice".
    17.  
    18. '      When a user named TestUser runs the application, selects the QA environment at login,
    19. '      and prints the report - the data is still retrieved from the Pubs_Dev database(even if we tell Crystal
    20. '      to login to the QA database).
    21.  
    22. '      We must change the Location to pubs_QA.dbo.Proc(selAuthorsByState) or simply selAuthorsByState
    23. '      this ensures the data is retrieved from the QA database tables.
    24. '******************************************************************************************************************
    25. Private Sub SetReportDataSources(CrystalReport As CRAXDRT.Report, ServerName As String, DatabaseName As String, UserName As String, Password As String)
    26.     On Error GoTo ErrorHandler
    27.    
    28.     Dim lngStartPos As Long
    29.     Dim lngEndPos As Long
    30.    
    31.     Dim objSubReport As CRAXDRT.Report
    32.    
    33.     Dim objDB  As CRAXDRT.Database
    34.     Dim objTables    As CRAXDRT.DatabaseTables
    35.     Dim objTable     As CRAXDRT.DatabaseTable
    36.    
    37.     Dim objSections  As CRAXDRT.Sections
    38.     Dim objSection As CRAXDRT.Section
    39.    
    40.     Dim objReportObjects As CRAXDRT.ReportObjects
    41.     Dim objSubReports As CRAXDRT.SubreportObject
    42.     Dim lngIdx As Long
    43.    
    44.    
    45.     Set objDB = CrystalReport.Database
    46.     Set objTables = objDB.Tables
    47.        
    48.     'set the login information and location for each table in the report
    49.     For Each objTable In objTables
    50.    
    51.         lngStartPos = InStr(1, objTable.Location, "(", vbTextCompare) + 1
    52.         lngEndPos = InStr(lngStartPos, objTable.Location, ")", vbTextCompare)
    53.        
    54.         If lngEndPos = 0 Then
    55.             lngEndPos = Len(objTable.Location) + 1
    56.         End If
    57.        
    58.         lngEndPos = lngEndPos - lngStartPos
    59.        
    60.         objTable.Location = Mid$(objTable.Location, lngStartPos, lngEndPos)
    61.         objTable.SetLogOnInfo ServerName, DatabaseName, UserName, Password
    62.     Next
    63.    
    64.     'set the login information and location for each table in the all subreports
    65.     'Access to the sub reports is through the Sections collection
    66.     Set objSections = CrystalReport.Sections
    67.  
    68.     For Each objSection In objSections
    69.        
    70.         Set objReportObjects = objSection.ReportObjects
    71.        
    72.         If objReportObjects.Count > 0 Then
    73.  
    74.             For lngIdx = 1 To objReportObjects.Count
    75.                 'make sure the report object is a subreport.
    76.                 If objReportObjects(lngIdx).Kind = crSubreportObject Then
    77.  
    78.                     'open the subreport and set the login information
    79.                     Set objSubReports = objReportObjects(lngIdx)
    80.                     Set objSubReport = CrystalReport.OpenSubreport(objSubReports.SubreportName)
    81.  
    82.                     Set objDB = objSubReport.Database
    83.                     Set objTables = objDB.Tables
    84.  
    85.                     For Each objTable In objTables
    86.                        
    87.                         lngStartPos = InStr(1, objTable.Location, "(", vbTextCompare) + 1
    88.                         lngEndPos = InStr(lngStartPos, objTable.Location, ")", vbTextCompare)
    89.                        
    90.                         If lngEndPos = 0 Then
    91.                             lngEndPos = Len(objTable.Location) + 1
    92.                         End If
    93.                        
    94.                         lngEndPos = lngEndPos - lngStartPos
    95.                        
    96.                         objTable.Location = Mid$(objTable.Location, lngStartPos, lngEndPos)
    97.                         objTable.SetLogOnInfo ServerName, DatabaseName, UserName, Password
    98.                     Next
    99.                
    100.                 End If
    101.            
    102.             Next
    103.  
    104.         End If
    105.  
    106.     Next
    107.  
    108. Exit Sub
    109.  
    110. ErrorHandler:
    111.     Err.Raise Err.Number, "SetReportDataSources", Err.Source, Err.Description
    112.  
    113. End Sub

  7. #7
    Member
    Join Date
    Sep 2005
    Posts
    59

    Re: Crystal Asking A Parameter

    Hi Bruce, I'm trying your code and it throws an error at the following line:

    objTable.Location = Mid$(objTable.Location, lngStartPos, lngEndPos)

    The error is: Run-time error '-2147189176 (80047e48)'
    Crystal Reports ActiveX Designer


    The parameters at that moment of my code are like this:
    objTable.Location=sp_getparts;1
    lngStartPos=1
    lngEndPos=17

    Why can this be happening?

    sp_getparts is my stored procedure design-timed written.

    Hope you can help me, I really cannot understand why is that difficult to throw a report with Crystal Reports with a stored procedure. It looks like it works great if no parameters are needed and if data is fixed. Not my case regretfully and I think it's not any other programmers case either.

    Thanks in advance, Marcelo.

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Crystal Asking A Parameter

    I have no idea why lngEndPos is equal to 17. From what you posted it should equal 12. Also this code is required whether the report has parameters or not.

    Take a look at this from my procedure comments.

    'Crystal will save the Location as pubs_dev.dbo.Proc(selAuthorsByState). Note that the location written does depend on the
    'author's options/preferences. We are not going to rely on a everyone following a "Standard Practice".
    In Crystal, use the Database -> Set Location menu. Tell me what is indicated in the location box near the bottom.

    Again, all our reports are created using the native Microsoft SQL Server Driver, not OLEDB or ODBC DSN - that probably makes a difference.

    This code was created 3 years ago, is used by 5 different applications with 100's of users running reports all day long. It has yet to fail me... (if I may - toot toot)...

  9. #9
    Member
    Join Date
    Sep 2005
    Posts
    59

    Re: Crystal Asking A Parameter

    Quote Originally Posted by brucevde
    I have no idea why lngEndPos is equal to 17. From what you posted it should equal 12.
    Sorry, it didn't say sp_getparts instead the stored procedure name is GetComputadoras... if this is a key to my problem.
    I mean you say, it should say 12 instead of 17 at lngEndPos, why is that?

    Code
    objTable.Location=GetComputadoras;1
    lngStartPos=1
    lngEndPos=17


    Quote Originally Posted by brucevde
    Also this code is required whether the report has parameters or not.
    Yes, that's my idea to use always this code, in case it finally works.


    Quote Originally Posted by brucevde
    Take a look at this from my procedure comments.

    In Crystal, use the Database -> Set Location menu. Tell me what is indicated in the location box near the bottom.
    At Database / Set Database Location it displays a tree of nodes which has different levels:

    ---------------------------------------------
    first level
    report

    second level
    MADSRV006 (which is my SQL Database)

    3rd level (it has 2 nodes):
    1.Properties
    2.GetComputadoras;1 (this is the stored proc)

    The info at:

    1.Properties
    Database Type: OLE DB (ADO)
    Provider: SQLOLEDB
    DataSource: SRVMAD006
    Initial Catalog: MyCatalog
    User ID: sa
    Integrated Security: False
    Locale Identifier: 11274
    Connect Timeout: 15
    General Timeout: 0
    OLE DB Services: -5
    Current Language:
    Initial Filename:
    Use Encryption for Data: 0
    Replication server name connect option:
    Tag with column collation when possible: 0

    2.GetComputadoras
    Properties
    Table Name: GetComputadoras;1
    Table Type: Stored Procedures
    Overriden Qualified Table Name:
    ---------------------------------------------

    Quote Originally Posted by brucevde
    Again, all our reports are created using the native Microsoft SQL Server Driver, not OLEDB or ODBC DSN - that probably makes a difference.
    Isn't it better to use OLEDB? I have understood it's better cause it skips a step on the connection cycle and so it's faster.

    Quote Originally Posted by brucevde
    This code was created 3 years ago, is used by 5 different applications with 100's of users running reports all day long. It has yet to fail me... (if I may - toot toot)...
    I understand this worked with different connections types like ODBC or other kind but I also understand that was 3 years ago... so... what do programmers use today to make reports? I really need to make a lots of reports and believe me I don't want to waste any more time if Crystal Reports 9.2 isn't worth.


    Hope you and anyone who has an idea programming with this tool can help me,

    Thanks again, Marcelo.

  10. #10
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Crystal Asking A Parameter

    The code I wrote was for version 8.5. The only thing I can suggest is to debug and step through the code line by line. What is the value of objTable.Location at run-time?

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