cancel
Showing results for 
Search instead for 
Did you mean: 

How to change the path of an Access .mdb file @ runtime in Visual Studio 2012

Former Member
0 Kudos

Hello,

  I'm hoping my question is simple enough to merit a quick answer.  I will lay out the details:

  I am writing a WindowsForms App in VB 2012.  I have a Crystal Report that points to an Access .mdb at design time in order to get the proper structure and layout correctly.

  However, during runtime, I need to point to a completely different .mdb file with the same identical structure.  How do I do this?  I've tried everything I could find on the net with no success and mostly because the answers are for C programmers.  I mean, I can pretty much decipher it, but have gotten zero results.

  Thank you for your time.

-Mike

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Could you provide a snippet of your code?

Former Member
0 Kudos

What part exactly do you mean?  I am attempting to use .SetDataSource to specify the database.  But, let me see:

-------------------------------------------------------------------------------------------------

Dim meter_report As New meters (where meters is meters.rpt)

CrystalReportViewer1.ReportSource = Nothing

CrystalReportViewer1.RefreshReport()

meter_report.SetDataSource("C:\Users\Public\MORTIZ\mydata.mdb")

CrystalReportViewer1.ReportSource = meter_report

CrystalReportViewer1.Show()

--------------------------------------------------------------------------------------------------

My original mydata,mdb is located on another drive and used solely to help me design the report.  During RunTime mydata.mdb will reside in C:\Users\Public\$$$$\mydata.mdb where $$$$ is the user's unique user name.

former_member188030
Active Contributor
former_member183750
Active Contributor
0 Kudos

From the SAP Crystal Reports .NET API Guide

SetDataSource:

Passes a Recordset or Dataset to the report engine.


E.g.; you are not using the correct APIs. See if Bhushan's idea helps. If not, there is a n umber of other KBAs that may help (search box is in top right corner). I find simple search terms such as 'crystal access how path' give me best results.

- Ludek

Senior Support Engineer AGS Product Support, Global Support Center Canada

Follow us on Twitter

Former Member
0 Kudos

The link appears to be for an older version of CrystalReports.  I am using he latest version for Visual Studio Developers (Free).  When I get to the part:

table_old = ClientDoc.Database.Tables(0)

I do not get the "Database" as an option.

former_member183750
Active Contributor
0 Kudos

The version of CR should not matter. If you were not able to get this working using the suggested KBA, what was the issue?

- Ludek

Former Member
0 Kudos

I'm fairly sure that I may be forgetting to do something, perhaps even when it comes to adding any references to the solution.

But, honestly, why would it be so hard to simply change the connection to a report?  I don't want to OPEN a report by calling and pointing to a physical file location for report.rpt.  It's already in my solution and should be able to call it by referencing it as I would any form.

i.e.

Dim Contacts As New Form1

Contacts.Show() ... etc

I should be able to do:

Dim myreport As New report

myreport.Database.File("C:\report.mdb")

myreport.Database.Table("mytable")

That would be logical and as simple as it should be.

Bottom line.  I want to change the database location of my Access database that the report uses.  I need a clean and detailed step by step instruction on doing so.  If I have to add .rpt files to my deployment, I will, but otherwise, I would simply want a straight set of instructions.

former_member183750
Active Contributor
0 Kudos

You don't have to load the report from path, but you are not answering my questions... If you could re-read my posts, and answer my queries that may get us somewhere. Otherwise, your temp increases and we're both wasting time...

- Ludek

Former Member
0 Kudos

My apologies.  Definitely flustered with this and time limitations.

Let me create a sample app and get it ready and I can provide you with better details.

Former Member
0 Kudos

I have started a new single form app with just the Viewer on it.  I've plugged in the code from the link and fixed the typos.

I need to confirm, which Extensions references should be added to the Solution.  I am presuming:

CrystalDecisions.ReportAppServer....

ClientDoc

DataDefModel

ReportDefModel

Controllers

I've gone ahead and done so.  But get these messages (warnings):

A reference was created to embedded interop assembly 'CrystalDecisions.ReportAppServer.DataDefModel' because of an indirect reference to that assembly from assembly 'CrystalDecisions.Shared'.  Consider changing the 'Embed Interop Types' property on either assembly.

When I run the app, it gives an error when it executes ClientDoc.Open("C:\Crystal\meters.rpt") (Of course that being my report location)

I will continue testing until you respond.

former_member188030
Active Contributor
0 Kudos

ok. The code needs some small modifications.

I gave it a quick try and here is the InProc RAS SDK code which works for me. Add the assembly references to the app for all those assemblies used in the 'imports' statements in the code.

Imports CrystalDecisions.ReportAppServer.ClientDoc
Imports CrystalDecisions.ReportAppServer.DataDefModel
Imports CrystalDecisions.ReportAppServer.ReportDefModel
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Web
Imports CrystalDecisions.Shared

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ClientDoc As ISCDClientDocument

        Dim logonPb As New CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag()
        Dim table_Old As New CrystalDecisions.ReportAppServer.DataDefModel.Table()
        Dim table_New As New CrystalDecisions.ReportAppServer.DataDefModel.Table()

        'Open the report object to initialize the ReportClientDocument
        Dim rd As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
        rd.Load("C:\Users\Administrator\Desktop\Report1.rpt")
        ClientDoc = rd.ReportClientDocument()
        table_old = ClientDoc.Database.Tables(0)

        'Clone the original table in the report
        table_new = table_old.Clone(True)

        ' Get the connection information from the report document for the first table in the collection.
        logonPb = table_new.ConnectionInfo.Attributes("QE_LogonProperties")

'set the path for new mdb
        logonPb.Item("Database Name") = "C:\Users\Administrator\Desktop\New folder\efashion1.mdb"

        ' Set a new table for the report, set the old table to the new one.
        ClientDoc.DatabaseController.SetTableLocation(table_old, table_new)

        'view the report
        CrystalReportViewer1.ReportSource = rd


    End Sub
End Class

Hope this clears the doubts

Former Member
0 Kudos

Thank you both for your help.  However, turns out, I figured out a way on my own using one line of code without any Imports at all.

my_report.Database.Table(0).Location = "C:\Crystal\mydatabase.mdb"

my_report = report.rpt which is in my Solution so I created the instance of it using:

Dim my_report As New report

That's all it took.  Is this an incorrect approach?  Is there a potential bug/issue that is not coming up at this point?

Answers (1)

Answers (1)

Former Member
0 Kudos

Mr. Ortiz,

You need to read up on and consider using My.Settings to set up and save the user's path to the database. It is not really a CR issue, but a Visual Studio issue.