Changing Database location

Posted on 2011-03-05
Last Modified: 2012-05-11
I need to be able to change the database location in Crystal 10, in vb code I have searched for solutions here
and have found one that would be just right for my problem.
I get the following error when I try to print a report on the mySQL data base which is located on the web.

Run-time error '-2147189180(80047e44)'
The table 'tblproject' could not be found.

this is the code I am using:-


    Dim crx As New CRAXDDRT.Application ' creates an instance of the Crystal Report application
    Dim rpt As CRAXDDRT.Report 'will hold the report object later
    Dim i As Integer
    Set rpt = crx.OpenReport(strReport) 'asks the crystal application to open a report file named C:\blah\CoverPage.rpt and returns it to rpt
    rpt.DiscardSavedData 'throws out any saved data the report might already have

    For i = 1 To rpt.Database.Tables.Count 'the database.tables collection is all the tables used by the report

------------------  The following Line Gives The Error -------------------------------------

        rpt.Database.Tables.Item(i).SetTableLocation rpt.Database.Tables.Item(i).Location, "", m_objConnConnectString
            'SetTableLocation sets where Crystal will look for that table.  g_objcon is just an ADO connection I've already opened on my dataset
            'the connectionstring could simply be specified here, e.g. "provider=SQLOLEDB;server=MySQLServer;database=MyDatabaseName;UID=MyUser;PWD=MyPassword"
            'I use theg_objcon.connectionstring because in most of my apps, I have logic to choose which database the user will connect to, and once the
            'connection is open, I simply use it's connection string to avoid repeating the logic to choose which database.
    CrystalActiveXReportViewer1.ReportSource = rpt

Although not related Using the same code but the connect string below I have a strange problem with the same code but attempting to print a report from a local Access database for each table in the report it asks for database location ans user and password the connect string is:-
myDBConnectString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Data\" & strMyDB
username and password are not required as the program generates the data
Question by:RichardLudkiewicz
  • 5
  • 3
  • 2
LVL 100

Expert Comment

ID: 35044313
Are you trying to change database types as well as location?


Author Comment

ID: 35045248
well yes but I am using the code twice in different forms, one form for the mysql database, and a different one for the Access.

In Access:
But essentially if I create a report and then place it in a different location the the problem arises for each table in the report it asks for user name and password to be entered.

In mysql:
The mysql problem is that it does not seem to find the first table in the report as it falls over at that point I cant say it wont find any other table.
LVL 100

Accepted Solution

mlmcc earned 500 total points
ID: 35046901
So you aren't trying to change a report to use 2 different database types just  different reports use different databases

When changing the Access database, is it using the same MDW file?

Try it this way for Access

rpt.Database.Tables(1).Location = App.Path & "\Cust_Orders.mdb"

Also fo the user/password (default user)
'sets the database password enabling the report to connect

rpt.Database.Tables(1).SetSessionInfo "", Chr(10) & "bits"


Author Comment

ID: 35046925
ok will try that, am I correct inthinking that I dont need to use the connect string and should I just use the code as you have written it except use my database name.
LVL 100

Assisted Solution

mlmcc earned 500 total points
ID: 35047087
I think so.  That should work for Access.  If MySQL has the same type of file then it may work for that also.

If you are trying to use 1 report against 2 different types of databases that won't work.

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.


Author Comment

ID: 35067217
Yes thank you it worked for the access database, now all we need to do is to get the mysql working, this is important because most of the reports will be run for there.

Author Comment

ID: 35115910
I recieved an email with a possible solution, but have lost it I thought the solution worked but have just tried it against a second database and the report printed the data from the original database, this is the code I am now using

Private Sub Form_Load()
    Dim App As New CRAXDDRT.Application
    Dim rpt As Report
    SetReportConnection (strReport) ' strReport is the path to the file and the report name
    Set rpt = App.OpenReport(strReport)
    CrystalActiveXReportViewer1.ReportSource = rpt
End Sub

Private Sub Form_Resize()
    CrystalActiveXReportViewer1.Top = 0
    CrystalActiveXReportViewer1.Left = 0
    CrystalActiveXReportViewer1.Height = ScaleHeight
    CrystalActiveXReportViewer1.Width = ScaleWidth
End Sub

Private Sub SetReportConnection(ByVal FileName As String)
  On Error GoTo ErrorHandler
  Dim x
  Dim oCrxApp           As New CRAXDDRT.Application
  Dim oCrxReport        As CRAXDDRT.Report
  Dim oCrxSubReport     As CRAXDDRT.Report
  Dim oSection          As CRAXDDRT.Section
  Dim oReportObject     As Object
  Dim sConnectionString As String
  Dim bReportChanged    As Boolean
  Dim i                 As Integer
  'sConnectionString = "Driver={SQL Server}; UID=sa; PWD=$password$; Server=MYSQL; Database=MyArchive;"
  bReportChanged = False
  Set oCrxReport = oCrxApp.OpenReport(FileName)
  For i = 1 To oCrxReport.Database.Tables.Count
    If bReportChanged Or oCrxReport.Database.Tables(i).ConnectionProperties("Connection String") <> m_objConnConnectString Then
      bReportChanged = True
      oCrxReport.Database.Tables(i).ConnectionProperties.Add "Connection String", m_objConnConnectString
      oCrxReport.Database.Tables(i).SetTableLocation oCrxReport.Database.Tables(i).Location, "", ""
    End If
  Next i
  If bReportChanged Then
    For Each oSection In oCrxReport.Sections
      For Each oReportObject In oSection.ReportObjects
        If TypeOf oReportObject Is ISubreportObject Then
          Set oCrxSubReport = oCrxReport.OpenSubreport(oReportObject.SubreportName)
          For i = 1 To oCrxSubReport.Database.Tables.Count
            oCrxSubReport.Database.Tables(i).ConnectionProperties.Add "Connection String", m_objConnConnectString
            oCrxSubReport.Database.Tables(i).SetTableLocation oCrxSubReport.Database.Tables(i).Location, "", ""
          Next i
        End If
  End If
  If bReportChanged Then
    On Error Resume Next
    oCrxReport.SaveAs FileName, crDefaultFileFormat
  End If
  Set oCrxApp = Nothing
  Set oCrxReport = Nothing
  Set oCrxSubReport = Nothing
  Set oSection = Nothing
  Set oReportObject = Nothing
  Exit Sub
  x = Err.Number
  GoTo ErrorResume
End Sub

Expert Comment

ID: 35123369
Not very nice to use my code without assigning me any points...

Expert Comment

ID: 35123934
It was in his original, previous question that was PAQs'd.

Author Comment

ID: 35126740
my appoligies I could not find the thread that your code came from and as I could not get the code as presented to work. I did mention that I couldnt find the thread and in the end did not use the code although the ideas from the code did come in usefull. if it is possible please divide the points between the two of them

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql left join sentence 7 36
Crystal Reports Server 2016 Installation, setup and use 5 74
MsgBox 4 47
VB 6 error 5 in windows 10 but not in XP 7 44
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now