How do I open my Crystal Report in VB code (RPT files) on password-protected Access 2007 database? Ever since I protected my database with password, my Crystal Report generates an error. Thanks

while loading the crystal reports pass the database connection along with the username and password and then try to call the crystal report...

 Dim crtableLogoninfos As New TableLogOnInfos

    Dim crtableLogoninfo As New TableLogOnInfo
    Dim crConnectionInfo As New ConnectionInfo
    Dim CrTables As Tables
    Dim CrTable As Table

    Dim cryRpt As New ReportDocument
    cryRpt.Load("Crystal report path goes here") 

           With crConnectionInfo
        .ServerName = databasename
        .DatabaseName = databasename
        .UserID = "your username"
        .Password = "your password"
    End With
    CrTables = cryRpt.Database.Tables
    For Each CrTable In CrTables
        crtableLogoninfo = CrTable.LogOnInfo
        crtableLogoninfo.ConnectionInfo = crConnectionInfo
        CrTable.ApplyLogOnInfo(crtableLogoninfo)
    Next

    'show the crystal report

where do i put that code? i tried putting it in the form where i had drag a crystal report viewer but it generates an error
this is the code

    Private Sub Audit1_InitReport(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Audit1.InitReport
        Dim crtableLogoninfos As New TableLogOnInfos
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim crConnectionInfo As New ConnectionInfo
        Dim CrTables As Tables
        Dim CrTable As Table
        Dim cryRpt As New ReportDocument
        cryRpt.Load("Crystal report path goes here")
        With crConnectionInfo
            .ServerName = databasename
            .DatabaseName = databasename
            .UserID = "your username"
            .Password = "your password"
        End With
        CrTables = cryRpt.Database.Tables
        For Each CrTable In CrTables
            crtableLogoninfo = CrTable.LogOnInfo
            crtableLogoninfo.ConnectionInfo = crConnectionInfo
            CrTable.ApplyLogOnInfo(crtableLogoninfo)
        Next
    End Sub
  • this generates an error saying is not defined
    TableLogOnInfos
    TableLogOnInfo
    ConnectionInfo
    Tables
    Tables
    ReportDocument

    • this generates an error saying is not declared
      databasename
      databasename

This code shud be placed in the form load event....or the button click if u r generating the report on button click.....

this code shud be placed wherever u r showing the crystal report...

check my below code just for reference

I have a form rptBill in which I have placed the crystal report viewer

On the form load event I am calling the below code...

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Public Class rptBill

    Private Sub rptBill_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim crtableLogoninfos As New TableLogOnInfos
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim crConnectionInfo As New ConnectionInfo
        Dim CrTables As Tables
        Dim CrTable As Table

        Dim cryRpt As New ReportDocument
        cryRpt.Load("cystal report path") 'when any doctor and date is selected


        With crConnectionInfo
            .ServerName = database name
            .DatabaseName =  database name
            .UserID = username
            .Password = password
        End With
        CrTables = cryRpt.Database.Tables
        For Each CrTable In CrTables
            crtableLogoninfo = CrTable.LogOnInfo
            crtableLogoninfo.ConnectionInfo = crConnectionInfo
            CrTable.ApplyLogOnInfo(crtableLogoninfo)
        Next

        'the below code is because I had parameters in my crystal report....

        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue

        crParameterDiscreteValue.Value = frmMain.dtpFromBill.Value.ToString("yyyy-MM-dd")
        crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("fromdate")
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)


        crParameterDiscreteValue.Value = frmMain.dtpToBill.Value.ToString("yyyy-MM-dd")
        crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("todate")
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

        crParameterDiscreteValue.Value = frmMain.txtPatIDBill.Text
        crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("patid")
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

        'show rhe cystal report...

        crvBill.ReportSource = cryRpt
        crvBill.Refresh()
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.