473,396 Members | 1,891 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.

Open Another Database in a New Window

Hello,
I'm trying to create a form that works as a Switchboard to open a number of databases from one place. I want users to be able to click a button and open an existing database in a new window. I tried the following code and the database flashes open and then disappears (fyi - I'm using Access 2007). Any ideas?
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.  
  3.     Dim appAccess As Access.Application
  4.     Dim strDB As String
  5.  
  6.     strDB = Me.PMTCTPath 'references text box with file path on form
  7.  
  8.      Set appAccess = _
  9.         CreateObject("Access.Application")
  10.     ' Open database in Microsoft Access window.
  11.     appAccess.OpenCurrentDatabase strDB
  12.  
  13. End Sub
Sep 6 '08 #1
11 13227
ADezii
8,834 Expert 8TB
  1. The problem is that appAccess is declared as a Procedural Level Variable, and is destroyed immediately when the Procedure ends. Change the Declaration to either a Private Declaration in a Form Module. Declare strDB for other reasons, in the same manner, as in:
    Expand|Select|Wrap|Line Numbers
    1. Private appAccess As Access.Application
    2. Private strDB As String
  2. Or as a Public Declaration in a Standard Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Public appAccess As Access.Application
    2. Public strDB As String
  3. Make the Instance of appAccess Visible, as in Code Line #5:
    Expand|Select|Wrap|Line Numbers
    1. strDB = Me.PMTCTPath    'references text box with file path on form
    2.  
    3. Set appAccess = CreateObject("Access.Application")
    4.  
    5. appAccess.Visible = True
    6.  
    7. 'Open database in Microsoft Access window.
    8. appAccess.OpenCurrentDatabase strDB
    9.  
    10. 'Open a specific Form to View
    11. appAccess.DoCmd.OpenForm "<some old Form>"
  4. Any questions, feel free to ask.
Sep 6 '08 #2
NeoPa
32,556 Expert Mod 16PB
...
4. Any questions, feel free to ask.
I'll nick in there and take you up on that ;)

Q1. If the application variable goes out of scope, will the application necessarily be closed?

Q2. If the application is made visible first, will that allow the process to continue indefinitely (until closed by the operator)?
Sep 6 '08 #3
ADezii
8,834 Expert 8TB
I'll nick in there and take you up on that ;)

Q1. If the application variable goes out of scope, will the application necessarily be closed?

Q2. If the application is made visible first, will that allow the process to continue indefinitely (until closed by the operator)?
Q1. If the application variable goes out of scope, will the application necessarily be closed?
It does not appear so, but you will not be able to reference the Properties or Methods of the appAccess Objects, as well as to react to any of its Events. You will not be able to Close the DB programmatically.The following will generate an Object Required Error in this current context:
Expand|Select|Wrap|Line Numbers
  1. MsgBox appAccess.CurrentObjectName
Q2. If the application is made visible first, will that allow the process to continue indefinitely (until closed by the operator)?
Expand|Select|Wrap|Line Numbers
  1. This would also appear to be True.
Sep 6 '08 #4
Stewart Ross
2,545 Expert Mod 2GB
Hi. Although the OP is posting about opening Access from Access and my experience is in opening Excel from Access I can confirm ADezii's answer to NeoPa's questions. There are some ramifications of all this which apply to this thread.

In the case of Q1, the non-visible automation instance will remain open indefinitely but not be accessible via code or the user. The running application is listed by Windows Task Manager and can be shut down from there, but otherwise it remains dormant but running until Windows is shut down.

In the case of Q2, the visible instance of the automation server responds to user commands just as if it had been started normally, so yes it can be shut down by the user, but if the link between the automation server and the instance of Access that called it is broken - through error, or a variable going out of scope before shutting the automation instance down, for example - then there is no further process interaction between the visible Access (or Excel in my case) instance and the process that was running in the original Access database.

Returning to the theme of this thread for a moment, the fact that the other databases remain independently operating is a potential disadvantage which may need to be borne in mind - the user can indeed close the application, and if that automation server instance is then referred to once again from the calling instance an error will arise as the application object will no longer be valid.

When the automation server is visible but running under control of automation code from elsewhere it is very easy (far too easy) to forget that that the running instance is operating under 'remote control' as it were. I have on many occasions caused the automation code to crash by accidentally interrupting an operation, forgetting that (in my case) the Excel worksheet in front of me is not available to do other things. That is the main advantage of running in 'invisible' mode - there is no temptation to interfere, and as long as the code has no errors the process will definitely run to completion.

Having the application visible introduces uncertainty into whether or not the automation code will run to completion, by the nature of the asynchronous and unpredictable actions the user may take. It's fascinating to watch automation code at work, however; in my case worksheets fill up as if by magic. I set all of mine to invisible now, however, not just to avoid my own interference - it also avoids the window focus shifting from the current application to the automation server every time a new instance is opened (a real nuisance if you are trying to work concurrently on other things).

-Stewart
Sep 6 '08 #5
FishVal
2,653 Expert 2GB
Hello gentlemen.

Excel and Access expose Application.UserControl property which determines whether an application instance would be terminated when object variable referring to has been set to nothing.

At the same time for Word you should use Document.UserControl property - Word.Application.UserControl is read only.

Regards,
Fish
Sep 6 '08 #6
NeoPa
32,556 Expert Mod 16PB
Thanks to all of you for your clarifications :)
Sep 6 '08 #7
ADezii
8,834 Expert 8TB
Hi. Although the OP is posting about opening Access from Access and my experience is in opening Excel from Access I can confirm ADezii's answer to NeoPa's questions. There are some ramifications of all this which apply to this thread.

In the case of Q1, the non-visible automation instance will remain open indefinitely but not be accessible via code or the user. The running application is listed by Windows Task Manager and can be shut down from there, but otherwise it remains dormant but running until Windows is shut down.

In the case of Q2, the visible instance of the automation server responds to user commands just as if it had been started normally, so yes it can be shut down by the user, but if the link between the automation server and the instance of Access that called it is broken - through error, or a variable going out of scope before shutting the automation instance down, for example - then there is no further process interaction between the visible Access (or Excel in my case) instance and the process that was running in the original Access database.

Returning to the theme of this thread for a moment, the fact that the other databases remain independently operating is a potential disadvantage which may need to be borne in mind - the user can indeed close the application, and if that automation server instance is then referred to once again from the calling instance an error will arise as the application object will no longer be valid.

When the automation server is visible but running under control of automation code from elsewhere it is very easy (far too easy) to forget that that the running instance is operating under 'remote control' as it were. I have on many occasions caused the automation code to crash by accidentally interrupting an operation, forgetting that (in my case) the Excel worksheet in front of me is not available to do other things. That is the main advantage of running in 'invisible' mode - there is no temptation to interfere, and as long as the code has no errors the process will definitely run to completion.

Having the application visible introduces uncertainty into whether or not the automation code will run to completion, by the nature of the asynchronous and unpredictable actions the user may take. It's fascinating to watch automation code at work, however; in my case worksheets fill up as if by magic. I set all of mine to invisible now, however, not just to avoid my own interference - it also avoids the window focus shifting from the current application to the automation server every time a new instance is opened (a real nuisance if you are trying to work concurrently on other things).

-Stewart
Excellent points, Stewart. I think another very important aspect that could potentially come into play here, is the OPs Switchboard-like design in which a Switchboard could open multiple, Independent Instances, of several Databases. I would imagine that it would be a rather complex operation to maintain programmatic control of the individual Instances, let alone the confusion involved in such an operation. The Object Variable appAccess, if maintained at the Form Level, would now be meaningless since it would only point to the last Opened Instance. I imagine that you could assign multiple Object Variables to all the Instances, but again the problem of manageability arises. The best approach would probably be to manage them via a Collection with each Instance uniquely identified by its hWnd Property, but it would still be a mess.

Personally, instead of the Switchboard approach, I would have a single Command Button opening a File Dialog Box, filtered for *.mdbs, with Multiselect set to False. Once a File has been selected, the Database would be opened via Automation as indicated by the previous code. It would not be that simple to inadvertently open multiple Databases, and the code base would be substantially reduced. Any thoughts?
Sep 6 '08 #8
NeoPa
32,556 Expert Mod 16PB
I have a thought ADezii :-
Only you would even GO there! :D
Sep 7 '08 #9
For anyone else who came across this looking for a quick answer...I just set it up as a hyperlink using VBA:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command25_Click()
  2.  
  3.     Dim strInput As String
  4.  
  5.     strInput = FilePathCommonOth1 'This references a text box on my form that has a file path
  6.  
  7.    Application.FollowHyperlink strInput, , True 'Setting this to True opens the database in another window
  8.  
  9. End Sub
Sep 8 '08 #10
ADezii
8,834 Expert 8TB
For anyone else who came across this looking for a quick answer...I just set it up as a hyperlink using VBA:

Private Sub Command25_Click()

Dim strInput As String

strInput = FilePathCommonOth1 'This references a text box on my form that has a file path

Application.FollowHyperlink strInput, , True 'Setting this to True opens the database in another window

End Sub
Glad you figured it out for yourself. Personally, I never considered this option, since with your initial approach, I was under the impression that you wanted to use Automation to Open the DBs.
Sep 8 '08 #11
NeoPa
32,556 Expert Mod 16PB
For anyone else who came across this looking for a quick answer...I just set it up as a hyperlink using VBA:
...
Thanks for posting your resolution. It's always interesting to see different approaches to the same problem.

Please try to remember the [ CODE ] tags in future though.
Sep 8 '08 #12

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

Similar topics

1
by: dixie | last post by:
I used to have a copy of this, but can't locate it. I want to be able to open the database window in an mde by double clicking a hidden button. Does any one know the code needed to do that? ...
0
by: dixie | last post by:
I have now used the following code from Allen Brown to open my mde database from a hidden button: DoCmd.SelectObject acTable, , True. My problem now is that I can't get the normal top menu...
7
by: Shannon Rotz | last post by:
All of a sudden when I open any Access database, I can't view the database window. This includes databases where I have hidden the database window on startup, and those that I haven't. I think...
4
by: dixie | last post by:
If I have a form open and the database window is open, is there a command I can use from an on click event that will close the database window for me, but still leave the application open? dixie
3
by: maddman | last post by:
In our database, we have some problem users that think they need to get at the tables themselves rather than use the forms. So I want to keep them from getting to them at all. I've set the DB...
3
by: MLH | last post by:
I did not know pressing F-11 key would open database window in a runtime installation. I found it out by accident today. How might I prevent this undesirable event?
16
by: MLH | last post by:
If I give someone a runtime app, they can open the database window by pressing the F-11 key. How to prevent???
3
by: Zammy | last post by:
I have a database on a server that many people access repeatedly during the day. I have set up the switchboard form with code to minimize the database window, but when I try to open the database...
0
by: hcuellar | last post by:
Hi. Please I need your help for this: After a database query, a table is created with a cell for each information retrieved, and for each record, two buttons, and second one have besides it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.