VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB Expert!

Join 306,836 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,732 people online right now. Registration is fast and FREE... Join Now!




VB6 and crystal reports 8.5

 

VB6 and crystal reports 8.5, need help with these 2 working together

kajirus3

31 Mar, 2009 - 09:23 AM
Post #1

New D.I.C Head
*

Joined: 6 Aug, 2008
Posts: 1

I have a report named control_record.rpt that does everything i want in crystal reports 8.5. It prints all the records.
What i cant figure out is how to call the report from within vb6 when a button is clicked. ANyone help with this?


Thanks in Advacne

This post has been edited by kajirus3: 31 Mar, 2009 - 09:24 AM

User is offlineProfile CardPM
+Quote Post


emacrack

RE: VB6 And Crystal Reports 8.5

1 Apr, 2009 - 09:00 AM
Post #2

New D.I.C Head
*

Joined: 28 Mar, 2009
Posts: 6


My Contributions
Well i use crystal reports XI R2 but it's the same process to link the .rpt file with the vb6 aplication. I don't remember if in the 8.5 version is necessary, but in the XI R2 you need to have a "developer" license to use it trought visual basic.
There's two ways to do what you want.

- The first one is disigning the report directly trought visual basic, adding the Crystal Reports Designer. I will not explain this method since you are telling that you already have the report disigned in Crystal Report with the corresponding .rpt file. Anyway, this method is for making simple reports since it doesn't bring you as much disign tools as the Crystal Reports aplication.

- The second way (and the better one in my opinion) is designing the report first with the Crystal Reports aplication, and saving the file. Then in Visual Basic you need to add the "Crystal Reports 9 ActiveX Designer Run Time Library" reference and the "Crystal Reports Viewer Control" component.
Add the CRViewer object (used to show the .rpt report file) to a form and use this code:

Variable Declaration:
CODE
Option Explicit
Private crApp As New CRAXDRT.Application
Private crReport As New CRAXDRT.Report


Code in the FormLoad event:
CODE
Private Sub Form_Load()

Dim crParamDefs As CRAXDRT.ParameterFieldDefinitions
Dim crParamDef As CRAXDRT.ParameterFieldDefinition

On Error GoTo ErrHandler

    'Opening the report
    Screen.MousePointer = vbHourglass
    Set crReport = crApp.OpenReport("ReportFilePath.rpt", 1)

    ' If you want to send parameters to it
    Set crParamDefs = crReport.ParameterFields
    For Each crParamDef In crParamDefs
        Select Case crParamDef.ParameterFieldName
            Case "Parametro1"
                 crParamDef.AddCurrentValue (mstrParametro1)
            Case "Parametro2"
                 crParamDef.AddCurrentValue (mlngParametro2)
        End Select
    Next

    crViewer.ReportSource = crReport
    crViewer.DisplayGroupTree = False
    crViewer.ViewReport
    Screen.MousePointer = vbDefault
    
    Set crParamDefs = Nothing
    Set crParamDef = Nothing
    Exit Sub

ErrHandler:
    If Err.Number = -2147206461 Then
        MsgBox "The file doesn't exist", vbCritical + vbOKOnly
    Else
        MsgBox Err.Description, vbCritical + vbOKOnly
    End If
    Screen.MousePointer = vbDefault

End Sub


Unload Cleaning
CODE
Private Sub Form_Unload(Cancel As Integer)
    Set crReport = Nothing
    Set crApp = Nothing
End Sub


So, when you want to show the report by clicking a button, you only have to show this form and that's all.
Note that in the FormLoad event we are opening and sending the data to the .rpt file previously created. In this case i'm sending only parameters type of data, but you can add any other.
Well i hope that this help you at least to begin. Greetings... wink2.gif

This post has been edited by emacrack: 1 Apr, 2009 - 09:03 AM
User is offlineProfile CardPM
+Quote Post

scottkellman

RE: VB6 And Crystal Reports 8.5

8 Apr, 2009 - 05:12 AM
Post #3

New D.I.C Head
*

Joined: 20 Mar, 2009
Posts: 1


My Contributions
QUOTE(kajirus3 @ 31 Mar, 2009 - 09:23 AM) *

I have a report named control_record.rpt that does everything i want in crystal reports 8.5. It prints all the records.
What i cant figure out is how to call the report from within vb6 when a button is clicked. ANyone help with this?


Thanks in Advacne


CODE

I am passing a custom SQL query as well as just passing parameters will not always get the job done

Dim sql As String
    Dim yBeginDate As String
    Dim yEndDate As String
            
    'clear out old data
    Me.CrystalReport1.DiscardSavedData = True
    Me.CrystalReport1.Reset

    'get the start and stop date for the report
    GetDates Trim(Str(Me.cboMonth.ListIndex + 1)), Trim(Me.cboYear.Text)
        
    sql = "({AuditTable.AuditDate} >= DateTime("
    sql = sql + Str(Year(dtStartDate))
    sql = sql + "," & Month(dtStartDate)
    sql = sql + "," & Day(dtStartDate)
    sql = sql + "," & Hour(dtStartDate)
    sql = sql + "," & Minute(dtStartDate)
    sql = sql + "," & Second(dtStartDate) & "))"
    sql = sql + " AND ({AuditTable.AuditDate} <= DateTime("
    sql = sql + Str(Year(dtEndDate))
    sql = sql + "," & Month(dtEndDate)
    sql = sql + "," & Day(dtEndDate)
    sql = sql + "," & Hour(dtEndDate)
    sql = sql + "," & Minute(dtEndDate)
    sql = sql + "," & Second(dtEndDate) & "))"
    
    'reset all good params
    Me.CrystalReport1.SelectionFormula = sql
    Me.CrystalReport1.ReportFileName = "report name.rpt"
    Me.CrystalReport1.Connect = "uid=xxxx;pwd=xxxx;"
    Me.CrystalReport1.PrintFileType = crptRecord
    Me.CrystalReport1.ReportSource = crptReport
    Me.CrystalReport1.ReportTitle = "Audit Report - Deleted Entries"
    Me.CrystalReport1.WindowTitle = "Reports: Audit Report - Deleted Entries"
    
    yBeginDate = "DateTime(" & Year(dtStartDate) & "," & Month(dtStartDate) & "," & Day(dtStartDate) & "," & Hour(dtStartDate) & "," & Minute(dtStartDate) & "," & Second(dtStartDate) & ")"
    yEndDate = "DateTime(" & Year(dtEndDate) & "," & Month(dtEndDate) & "," & Day(dtEndDate) & "," & Hour(dtEndDate) & "," & Minute(dtEndDate) & "," & Second(dtEndDate) & ")"
    
    Me.CrystalReport1.Formulas(0) = "startDate = " & yBeginDate
    Me.CrystalReport1.Formulas(1) = "endDate = " & yEndDate
            
    'launch the report
    Me.CrystalReport1.WindowState = crptMaximized
    Me.CrystalReport1.Action = 1

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 11:05PM

Live VB Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month