Link to home
Start Free TrialLog in
Avatar of Rikol
Rikol

asked on

Calling a crystal report with a parameter field

I am using VB 6 to create an application that opens a crystal report that has a parameter field called OrderNum.  I have two forms, the starting form has a text field and two command buttons.  I place the value of the text field in a public variable so that I can pass it to the crystal reports form.  Here is the code for the starting form:

Private Sub cmdClose_Click()
 End
End Sub

Private Sub cmdView_Click()

OrderN = txtOrderNum.Text
frmReportView.Show vbModal

End Sub

Private Sub Form_Unload(Cancel As Integer)

Set CrxSubreport = Nothing
Set CrxReport = Nothing
Set crxApplication = Nothing

End Sub

I based this heavily from the example project they offer from the Crystal Reports website, here is the second form's code:

Private Sub cmdExit_Click()
frmReportView.Hide
Form1.Show
End Sub

Private Sub Form_Load()

Dim OrderNM As Integer

OrderNM = OrderN

Screen.MousePointer = vbHourglass

Set CrxReport = crxApplication.OpenReport(App.Path & "\Order-Detail.rpt")

CrxReport.DiscardSavedData

CrxReport.ParameterFields.GetItemByName("@OrderNum").AddCurrentValue (OrderN)

CRViewer1.ReportSource = CrxReport
CRViewer1.ViewReport

frmReportView.WindowState = vbMaximized

CRViewer1.Zoom 100
Screen.MousePointer = vbDefault

End Sub

Now, no matter what I seem to do to get it to work I'm getting errors on this line of code:

CrxReport.ParameterFields.GetItemByName("@OrderNum").AddCurrentValue (OrderN)

VB highlights .GetItemByName and says method or data member not found.  Any help would be greatly appreciated.  I'm using CR 8.0

Timmy
Avatar of Rikol
Rikol

ASKER

Sorry, take the @ symbol out.  I was trying something out.

CrxReport.ParameterFields.GetItemByName("OrderNum").AddCurrentValue (OrderN)

Timmy
use CrxReport.ReportSelectionFormula = "OrderNum=" & OrderN instead of using Parameters..
then

CRViewer1.ReportSource = CrxReport
CRViewer1.ViewReport
Avatar of Rikol

ASKER

Private Sub cmdExit_Click()
frmReportView.Hide
Form1.Show
End Sub

Private Sub Form_Load()

Dim OrderNM As Integer
Dim APath As String

OrderNM = OrderN

Screen.MousePointer = vbHourglass

Set CrxReport = crxApplication.OpenReport(App.Path & "\Order-Detail.rpt")

CrxReport.DiscardSavedData

'CrxReport.ParameterField.GetItemByName("OrderNum").AddCurrentValue (OrderNM)

CrxReport.ReportSelectionFormula = "OrderNum=" & OrderN

CRViewer1.ReportSource = CrxReport
CRViewer1.ViewReport

frmReportView.WindowState = vbMaximized

CRViewer1.Zoom 100
Screen.MousePointer = vbDefault

End Sub

With the code like that I get an error on the line I added.

My module looks like this:

Option Explicit

Global crxApplication As New CRAXDRT.Application
Global CrxReport As CRAXDRT.Report
Public OrderN As Integer
Avatar of Rikol

ASKER

Error is Object does not support this property or method.
Avatar of Mike McCracken
Try
CrxReport.RecordSelectionFormula = "{Table.OrderNum}=" & OrderN

Change {Table.OrderNum} to the appropriate table and field name.

mlmcc
If you wish to go back to the parameter method

CrxReport.ParameterFields.GetItemByName("{?OrderNum}").AddCurrentValue (OrderN)

The name includes the { } and the ?

mlmcc
Avatar of Rikol

ASKER

Tried both.  Same error.  Hmmm....do you think it has anything to do with the fact that I'm using an ODBC driver to pull the data?

Timmy
Avatar of Rikol

ASKER

Hmmm..new error.  Took out the ? and now getting type mismatch.  Will keep trying.

Timmy
Do you have the developer edition of Crystal 8.0?

Have you installed the latest hotfix?

mlmcc
Avatar of Rikol

ASKER

Pretty sure it is not developers edition, it came bundled with a DB software that we've got.  I'll get the latest hotfix though.


Timmy
Avatar of Rikol

ASKER

NM.  Can't get any hotfixes.  Since it is an OEM version it's not compatable with the hotfixes from Seagate.  I'll be downloading an updated version of the software this afternoon that will bump my CR up to 8.5.  So maybe that will clear this up.

Timmy
Avatar of Rikol

ASKER

Meant to update the points.  :)
Avatar of Rikol

ASKER

Ok, CR now at 8.5.  Now getting Invalid name and it points to this line

CrxReport.ParameterFields.GetItemByName("{?OrderNum}").AddCurrentValue (OrderNM)

I've tried it without the ? and without the {} marks.  Still getting the same error.

Timmy
Avatar of Rikol

ASKER

After fiddling with it and commenting that line out and adding this one :

CrxReport.ParameterFields(1).Value = OrderNM

I now get an object required error.  But I know that I have that parameter field set up.  I've checked the report 3 times now.

Timmy
Hi... Rikol... maybe this would help.

This is your code from your first message above.....

Private Sub Form_Load()

Dim OrderNM As Integer

OrderNM = OrderN

Screen.MousePointer = vbHourglass

Set CrxReport = crxApplication.OpenReport(App.Path & "\Order-Detail.rpt")

CrxReport.DiscardSavedData
'==========================================================
'   This was your old code
'   CrxReport.ParameterFields.GetItemByName("@OrderNum").AddCurrentValue (OrderN)
'==========================================================
' change it with the following....

crxReport.ParameterFields(1).AddCurrentValue (OrderN)

' The number represents the parameter field's order in your crystal report.
' If there are many parameter fields in your report, just place its order accordingly...
' e.g. crxReport.ParameterFields(1) , crxReport.ParameterFields(2),. ... etc.

CRViewer1.ReportSource = CrxReport
CRViewer1.ViewReport

frmReportView.WindowState = vbMaximized

CRViewer1.Zoom 100
Screen.MousePointer = vbDefault

End Sub
Avatar of Rikol

ASKER

No errors when I switch it to that, but coming up with a blank report again.

Keeps doing that, I've got a feeling that it's not connecting the ODBC to the server.  That's the only way I know to put it.

Timmy
Avatar of Rikol

ASKER

This is my code as it now stands.  I have commented out various stuff that I've been trying to get it to work with.  I'm still working on getting the log on server line correct.

Hehe.  For security purposes I've replaced my username with Myusr and my password with Mypswrd.  Just a silly little precaution.  :)

Private Sub Form_Load()

Dim OrderNM As String
Dim APath As String
Dim FieldName As String
Dim CheckData As String

OrderNM = Str(OrderN)

APath = "C:\Custom Reports\VB Stuff\Order-Detail.rpt"

Screen.MousePointer = vbHourglass

Set CrxReport = crxApplication.OpenReport(APath)

CrxReport.DiscardSavedData

CrxReport.EnableParameterPrompting = False

FieldName = CrxReport.ParameterFields(1).Name

'crxReport.Database.LogOnServer "invenodbc.dll", "PROGRESS", "INVEN", "myuser", "mypswrd"

'CrxReport.ParameterFields.GetItemByName("OrderNum").AddCurrentValue (OrderNM)

'CrxReport.ParameterFields(1).Value = OrderNM

CrxReport.ParameterFields(1).AddCurrentValue (OrderNM)

CheckData = CrxReport.ParameterFields(1).Value


CRViewer1.ReportSource = CrxReport
CRViewer1.ViewReport

frmReportView.WindowState = vbMaximized

CRViewer1.Zoom 100
Screen.MousePointer = vbDefault

End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rikol

ASKER

I've tried that out and still no go.  Just using the parameter prompt from CR to get the data.  Will try again another time to pass data to parameter fields from VB.  Thanks for all the help though.  :)

Timmy
Try this ...



Public Function CrystalExport(sDocPath As String, sFilePath As String, lUTBDAT As Long, sPNR As String, sEmailAdd As String, sBatch As String) As Boolean
Dim lCount As Long
Dim lMax As Long
Dim sSelectString As String
Dim c_Mail As cMail
Dim fs As FileSystemObject
Dim strFile As String

On Error GoTo Err

'add the reference to Crystal Report Viewer Control and the Crystal report 8 ActiveX Designer Run Time Lib


If IsObject(crRpt) Then
   Set crRpt = Nothing
End If

Set crApp = New CRAXDRT.Application
Set crRpt = crApp.OpenReport(sDocPath, 1)

    With crRpt
        ' Logon do the Log on
        .Database.LogOnServer "pdsodbc.dll", "RESPONS", "RESPONS", "SYSADM", "sysadm"
        lCount = 1
        lMax = .Database.Tables.Count
        'For some stupid reason, you have to log on all the tables
        Do While lCount <= lMax
             .Database.Tables(lCount).SetLogOnInfo "RESPONS", "RESPONS", "SYSADM", "SYSADM"
             lCount = lCount + 1
        Loop
        'Get rid of saved data
        .DiscardSavedData
    End With

    With crRpt
        'Do the parameters
        'We will only check for these 2 parameters
        For lCount = 1 To crRpt.ParameterFields.Count

            If UCase(.ParameterFields(lCount).Name) = "{?DATE}" Then
                .ParameterFields(lCount).EnableNullValue = True
                .ParameterFields(lCount).AddCurrentValue (CLng(lUTBDAT))
            ElseIf UCase(.ParameterFields(lCount).Name) = "{?EMP NO}" Then
                .ParameterFields(lCount).EnableNullValue = True
                .ParameterFields(lCount).AddCurrentValue sPNR
            Else

            End If

        Next lCount
           
        'Do the Export to HTML
        .ExportOptions.FormatType = crEFTHTML40
        .ExportOptions.DestinationType = crEDTDiskFile
        .ExportOptions.DiskFileName = "c:\test.htm"
        .ExportOptions.HTMLFileName = "c:\test.htm"
        .Export False
       
    End With

    CrystalExport = True

Exit Function
Err:
    MsgBox Err.Description, vbInformation
    CrystalExport = False
End Function


Hope it works

VX