473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reports in listbox to reside in external db

tuxalot
200 100+
I have reports visible in a listbox on a tabbed form. The Row Source for the listbox is:

Expand|Select|Wrap|Line Numbers
  1. SELECT [TblReports].ReportID, [TblReports].ReportName, [TblReports].ReportCaption, [TblReports].ReportCriteriaFlags FROM [TblReports] ORDER BY [TblReports].ReportCaption;
QUESTION:

Some of the reports currently presented in the listbox are State-specific (i.e. California and Arizona have different forms for example). I would like to have these State-specific reports exist in a separate external database, and deploy the "State" database only to businesses in the appropriate State. The listbox then, in the main database would need to include reports from (2) locations, one, the main database, and two, the State-specific reports which reside in the external (but local) database.

I really appreciate the help you have all provided thus far.
Mar 6 '09 #1
11 2429
tuxalot
200 100+
Further explanation on why (I think) I need to do this: I foresee updating the reports as an issue I wish to avoid if possible. Each U.S. State updates their forms (which are the basis of my reports) annually. Using an external db to house the reports would allow me to simply email the updated reports to the 150+ locations, and eliminate any data migration that would be necessary if I updated the State-specific reports housed in their main db.

Does this make any sense? Being very new to Access, maybe I'm missing something. Ok, so I exported TblReports to an external db. From the main db, I setup a link to the exported table. I kept the same name "TblReports" (don't know if this will cause issues). Again here is the row source for the listbox:
Expand|Select|Wrap|Line Numbers
  1. SELECT [TblReports].ReportID, [TblReports].ReportName, [TblReports].ReportCaption, [TblReports].ReportCriteriaFlags FROM [TblReports] ORDER BY [TblReports].ReportCaption;
Can I just reference the linked TblReports by inserting in the code above?

I found a bit of code that might work (if I had a clue how to use it)

Expand|Select|Wrap|Line Numbers
  1. Access Example 1
  2.  
  3.  
  4.     Private Sub UserForm_Initialize()
  5. Dim myDataBase As Database
  6. Dim myActiveRecord As Recordset
  7. Dim i As Long
  8. 'Open the database to retrieve data
  9. Set myDataBase = OpenDatabase("E:\Junk\sourceAccess.mdb")
  10. 'Define the first recordset
  11. Set myActiveRecord = myDataBase.OpenRecordset("Table1", dbOpenForwardOnly)
  12. 'Set the listbox column count
  13. ListBox1.ColumnCount = myActiveRecord.Fields.Count
  14. i = 0
  15. 'Loop through all the records in the table until the EOF
  16. 'marker is reached. Use AddItem to add a new row for each record.
  17. 'Use List to populate the ListBox column and row field.
  18. Do While Not myActiveRecord.EOF
  19.     ListBox1.AddItem
  20.     ListBox1.List(i, 0) = myActiveRecord.Fields("Employee Name")
  21.     ListBox1.List(i, 1) = myActiveRecord.Fields("Employee DOB")
  22.     ListBox1.List(i, 2) = myActiveRecord.Fields("Employee ID")
  23.     i = i + 1
  24. 'Get the next record
  25.     myActiveRecord.MoveNext
  26. Loop
  27. 'Close the database and clean-up.
  28. myActiveRecord.Close
  29. myDataBase.Close
  30. Set myActiveRecord = Nothing
  31. Set myDataBase = Nothing
  32. End Sub
  33.  
  34. Access Example 2
  35.  
  36.  
  37.     Private Sub UserForm_Initialize()
  38. 'You need to set a reference in your project to the “Microsoft DAO 3.51 (or 3.6) Object Library”.
  39. Dim db As DAO.Database
  40. Dim rs As DAO.Recordset
  41. Dim NoOfRecords As Long
  42. 'Open the database to retrieve data
  43. Set db = OpenDatabase("E:\Junk\sourceAccess.mdb")
  44. 'Define the first recordset
  45. Set rs = db.OpenRecordset("SELECT * FROM Table1")
  46. 'Determine the number of records in the recordset
  47. With rs
  48.     .MoveLast
  49.     NoOfRecords = .RecordCount
  50.     .MoveFirst
  51. End With
  52. 'Set the number of ListBox columns = number of fields in the recordset
  53. ListBox1.ColumnCount = rs.Fields.Count
  54. 'Load the ListBox with the retrieved records
  55. ListBox1.Column = rs.GetRows(NoOfRecords)
  56. 'Cleanup
  57. rs.Close
  58. db.Close
  59. Set rs = Nothing
  60. Set db = Nothing
  61. End Sub
These alternatives seem way too complex. I hope there is an easier way. Thoughts? Thanks in advance.
Mar 7 '09 #2
ADezii
8,834 Expert 8TB
I haven't actually tried this, but you can 'Link' to the Table in the External Database, then perform a Union Query to populate your List Box.
Mar 7 '09 #3
tuxalot
200 100+
Thanks ADezii, that's exactly what I did and it works well. I've now created a form to allow a user to select the path to the linked mdb and it's working. However, I would like a listbox on the form that would show the full path to the current linked mdb. I've tried
Expand|Select|Wrap|Line Numbers
  1. SELECT MSysObjects.Name, MSysObjects.Database FROM MSysObjects WHERE (((Left([Name],1))<>"~") AND ((MSysObjects.Type) In (4,6))) ORDER BY MSysObjects.Name;
which gives me the linked table name, but what I would like to show is the full path to the mdb instead. Any ideas?
Mar 7 '09 #4
tuxalot
200 100+
solved this bit. I created a function
Expand|Select|Wrap|Line Numbers
  1. Public Function GetLinkedDBName(TableName As String)
  2.    Dim db As DAO.Database, Ret
  3.    On Error GoTo DBNameErr
  4.    Set db = CurrentDb()
  5.    Ret = db.TableDefs(TableName).Connect
  6.    GetLinkedDBName = Right(Ret, Len(Ret) - (InStr _
  7.       (1, Ret, "DATABASE=") + 8))
  8.    Exit Function
  9. DBNameErr:
  10.    GetLinkedDBName = "  Unable to find linked database.  Does it exist?"
  11.  
  12. End Function
And set this as the control source in the textbox where I wanted the path to show:
Expand|Select|Wrap|Line Numbers
  1. =GetLinkedDBName("TblReportsState")
where TblRecordState is the table in the linked db.
Mar 8 '09 #5
tuxalot
200 100+
Now I'm stuck. Using the union query, the listbox properly shows reports from the local and external databases. The button on the reports tab has as it's code:
Expand|Select|Wrap|Line Numbers
  1. On Error GoTo Err_cmdGlobalReportOpen_Click
  2.  
  3.     Dim stDocName As String
  4.  
  5.     stDocName = [Forms]![FrmMain]![lstReportName].Column(1)
  6.     DoCmd.OpenReport stDocName, acPreview
  7.  
  8. Exit_cmdGlobalReportOpen_Click:
  9.     Exit Sub
  10.  
  11. Err_cmdGlobalReportOpen_Click:
  12.     MsgBox Err.Description
  13.     Resume Exit_cmdGlobalReportOpen_Click
Of course, this code does not allow me to open remote reports. The external reports have as their record source a query which resides in my main db. So the task at hand is to modify the code above to retrieve local and external reports, and somehow have the local query pass data to the external reports.

Any help is very much appreciated as I've no idea where to begin with this one.
Mar 8 '09 #6
ADezii
8,834 Expert 8TB
@tuxalot
You can probably use Automation Code to accomplish what you have requested but it may be a little tricky. I've written some code for you that will:
  1. Check for the existence of a Report in an External Database to see if it also exists Locally.
  2. If it exists Locally, Delete it since it may have been modified within the External Database, and we need the most recent Version.
  3. Import the Report from the External Database and Open (Print) it. This can be done because its Record Source exists Locally.
  4. Delete it when finished, since there is no longer any need to store it.
  5. This code if for a single Report only, but I'm sure you can easily adapt it for multiple Reports.
  6. This is probably not the Optimal solution, so I would be patient and wait and see what some of the other Members have to say on this Topic.
    Expand|Select|Wrap|Line Numbers
    1. Dim rptCounter As Integer
    2. Const conPATH_TO_EXTERNAL_DB As String = "C:\Test\External.mdb"
    3. Const conREPORT_NAME As String = "rptCustomers"
    4.  
    5. For rptCounter = 0 To CurrentDb.Containers("Reports").Documents.Count - 1
    6.   If CurrentDb.Containers("Reports").Documents(rptCounter).Name = conREPORT_NAME Then
    7.     'DELETE the Report if it currently exists in the DB, since it may have changed
    8.     DoCmd.DeleteObject acReport, conREPORT_NAME
    9.       Exit For
    10.   End If
    11. Next
    12.  
    13. 'Import the Report (bring in a 'Fresh' Version
    14. DoCmd.TransferDatabase acImport, "Microsoft Access", conPATH_TO_EXTERNAL_DB, acReport, _
    15.                                   conREPORT_NAME, conREPORT_NAME, False
    16.  
    17. 'Print the External/now Internal Report
    18. DoCmd.OpenReport conREPORT_NAME, acViewNormal
    19.  
    20. 'All done, let's DELETE the Report once again
    21. DoCmd.DeleteObject acReport, conREPORT_NAME
P.S. - You would need some kind of mechanism to differentiate between 'External' and 'Local' Reports.
Mar 8 '09 #7
tuxalot
200 100+
ADezii, thanks for the code. Some comments:
  • The external reports will not exist locally.
  • At most, there will only be 4-5 reports in the external db.
  • The external db will not be accessed as it will simply provide a home for the reports.
  • A button on the reports tab of the main form selects the report for printing based on the report previously selected in the listbox of the main form.
  • The path to the external db is selected by the user.
If you could provide direction on how to modify the code you provided based on these comments that would be much appreciated.

Other options or possible alternatives:
  • If this is too tricky to accomplish I could have all the reports stored in the external db. This would result in only about 15 reports total stored externally.
  • Each external report only require as their record source data from a single record (one employee). These data are stored in (2) tables, TblEmployeeInjury and TblHotelInformation. Based on this, could I create links to these local tables from the external db, and use a filter on the external reports? I would still need a mechanism to open them from within the local db.

Just throwing that out there, perhaps this could be part of the solution.

Thanks all,

Tux
Mar 8 '09 #8
tuxalot
200 100+
I added columns in my local and linked tables called RemoteReport with values 0=local and 1=remote. My button code now looks like this, to check to see if the report is local or remote:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdGlobalReportOpen_Click()
  2.  
  3. 'On Error GoTo Err_cmdGlobalReportOpen_Click
  4.  
  5. ' determine if the report is local or external (i.e. for lstReportName column(4) remote = 1)
  6.  
  7. Dim stDocRemote As String
  8.  
  9. stDocRemote = [Forms]![FrmMain]![lstReportName].Column(4)
  10.  
  11. If stDocRemote = "1" Then
  12.  
  13. Call OpenExternalRpt
  14.  
  15. Else
  16.  
  17.     Dim stDocName As String
  18.  
  19.     stDocName = [Forms]![FrmMain]![lstReportName].Column(1)
  20.     DoCmd.OpenReport stDocName, acPreview
  21.  
  22. Exit_cmdGlobalReportOpen_Click:
  23.     Exit Sub
  24.  
  25. Err_cmdGlobalReportOpen_Click:
  26.     MsgBox Err.Description
  27.     Resume Exit_cmdGlobalReportOpen_Click
  28.  
  29. End If
  30.  
  31. End Sub
Local reports print fine. External reports (stDocRemote = "1") still do not print. I modified the code ADezii provided earlier to this:

Expand|Select|Wrap|Line Numbers
  1. Public Function OpenExternalRpt()
  2.  
  3. 'Dim rptCounter As Integer
  4. 'Dim PathToExternalDb As String
  5. Dim conPATH_TO_EXTERNAL_DB As String
  6. Dim conREPORT_NAME As String
  7.  
  8. conPATH_TO_EXTERNAL_DB = PathToExternalDb ' this is the field in TblPathToExternalDb which stores the path selected by the user
  9. conREPORT_NAME = [Forms]![FrmMain]![lstReportName].Column(1) ' this is the name of the report
  10.  
  11. 'Import the Report (bring in a 'Fresh' Version
  12. 'DoCmd.TransferDatabase acImport, "Microsoft Access", conPATH_TO_EXTERNAL_DB, acReport, conREPORT_NAME, conPATH_TO_EXTERNAL_DB, False
  13. DoCmd.TransferDatabase acImport, "Microsoft Access", conREPORT_NAME, acReport, conPATH_TO_EXTERNAL_DB, conREPORT_NAME, False
  14. '
  15. ' 'Print the External/now Internal Report
  16. DoCmd.OpenReport conREPORT_NAME, acPreview
  17. '
  18. ' 'All done, let's DELETE the Report once again
  19. DoCmd.DeleteObject acReport, conREPORT_NAME
  20.  
  21. End Function
I am getting a run time error 3024 at this line of code:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferDatabase acImport, "Microsoft Access", conREPORT_NAME, acReport, conPATH_TO_EXTERNAL_DB, conREPORT_NAME, False
stating "Could not find file c:\Documents and Settings\Mikey\My Documents\(report name in the remote db)".

So the path is incorrect to the remote db. The correct path to the external db as stored in my table is:

C:\Documents and Settings\mikey\Desktop\Hyatt\StateReports\StateRep orts.mdb

and is the same as the file shown in the Access link manager.

Any ideas?
Mar 8 '09 #9
ADezii
8,834 Expert 8TB
Your Arguments to the TransferDatabase Method are out of whack:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferDatabase acImport, "Microsoft Access", conPATH_TO_EXTERNAL_DB, acReport, conREPORT_NAME, conREPORT_NAME, False
Mar 9 '09 #10
tuxalot
200 100+
Hi ADezzi, thanks for the reply. Problem is, that TransferDatabase Method produces a run-time error 3001 "invalid argument". Any idea why?

I've zipped and attached my main database, as well as the one that houses the state reports. If you have a chance to have a look at it, just set the path to the external db from the utilities tab and you should see the external reports on the reports tab.

Thanks so much for your help.
Attached Files
File Type: zip ExternalReport.zip (1.07 MB, 90 views)
Mar 9 '09 #11
tuxalot
200 100+
Now it's working. I set a hidden subform to pull the path to the external db (I'm sure there is a cleaner way to do this) but I couldn't figure it out. So the final code is:

Expand|Select|Wrap|Line Numbers
  1. Public Function OpenExternalRpt()
  2.  
  3. Dim rptCounter As Integer
  4. Dim conPATH_TO_EXTERNAL_DB As String
  5. Dim conREPORT_NAME As String
  6. Dim conEXTERNAL_DB_NAME As String
  7.  
  8. ' get path to external db from hidden subform located on the utilities tab of FrmMain
  9. conPATH_TO_EXTERNAL_DB = Forms!FrmMain!fsubPathToExternalDb.Controls!txtPathToExternalDb
  10.  
  11. conREPORT_NAME = [Forms]![FrmMain]![lstReportName].Column(1)
  12. conEXTERNAL_DB_NAME = ExternalDbName
  13.  
  14.  
  15. ' Delete the local copy of the report if it exists
  16. For rptCounter = 0 To CurrentDb.Containers("Reports").Documents.Count - 1
  17. If CurrentDb.Containers("Reports").Documents(rptCounter).Name = conREPORT_NAME Then
  18. DoCmd.DeleteObject acReport, conREPORT_NAME
  19. Exit For
  20. End If
  21. Next
  22.  
  23.  
  24. 'Import the external report based on the report selected in the listbox of FrmMain
  25. DoCmd.TransferDatabase acImport, "Microsoft Access", conPATH_TO_EXTERNAL_DB, acReport, conREPORT_NAME, conREPORT_NAME, False
  26.  
  27. ' 'Print preview the external/now Internal Report
  28. DoCmd.OpenReport conREPORT_NAME, acPreview
  29.  
  30. End Function
I chose to leave the local copy of the report as is until it is opened again, after which the local copy is deleted.

The button code is:

Expand|Select|Wrap|Line Numbers
  1. Dim stDocRemote As String
  2.  
  3. stDocRemote = [Forms]![FrmMain]![lstReportName].Column(4)
  4.  
  5. If stDocRemote = "1" Then
  6.  
  7. Call OpenExternalRpt
  8.  
  9. Else
  10.  
  11.     Dim stDocName As String
  12.  
  13.     stDocName = [Forms]![FrmMain]![lstReportName].Column(1)
  14.     DoCmd.OpenReport stDocName, acPreview
  15.  
  16. Exit_cmdGlobalReportOpen_Click:
  17.     Exit Sub
  18.  
  19. Err_cmdGlobalReportOpen_Click:
  20.     MsgBox Err.Description
  21.     Resume Exit_cmdGlobalReportOpen_Click
  22.  
  23. End If
  24.  
Thanks ADezii for all your help. For now, this will work but I'd be interested if anyone has a cleaner way to get from A => B.
Mar 9 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: DD | last post by:
Hi I want the user to be able to view all reports from a form either by way of a list box or dropdown or ?? any help please DD
1
by: KEVIN97810 | last post by:
Hello to all, I am trying to fill all my reports in a listbox but I may not need to show other reports. How do you modify this function to do that. I have losts of reports but don't want to...
6
by: NOSPAMrclark | last post by:
I'm wondering . . . . What is the generally accepted method for calling reports from multiple locations? Example: REPORT-A is called by selecting a record in a listbox and clicking a...
12
by: Tony Ciconte | last post by:
We are evaluating the prospect of integrating and/or using Crystal Reports with some of our current products. Some of these are still in Access 97 and are running well. Since we cannot include the...
7
by: Jlo | last post by:
Hi, I have a c# winforms application. When I call the report file, it shows me all the records in the table. How can I make it to call only a particular range. i have the following code...
10
by: kimiraikkonen | last post by:
Hello, For example i have listbox with 10 items. how can i create a job queue (automated)? I used "for each" but after processing 5 items, it stopped, why? Thanks.
1
by: Refugnic | last post by:
I tried to fill a ListBox with a DataSource pointing to an ArrayList. It all works fine...up to one point. The ArrayList is dynamic, which means the contents of it change, during the course of...
0
by: Mark Gold | last post by:
Hi! We have a VB application using Crystal Reports 6 that has worked successfully on hundreds of systems for over 10 years. Now, on one network, the application and access database does not close....
7
by: google | last post by:
I am trying to automate the way reports are printed in an Access 2003 database - I have ~200 records (people) who require between 5 and 10 customized reports (depending on values within certain...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.