Posted on 1998-03-30
Last Modified: 2008-02-01
How can i close the current database and open another one in the same window? i have tried using opencurrentdatabase and closecurrentdatabase but i just can't seem to get it. Any help would be great.
Question by:Jambyte
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 17

Expert Comment

ID: 1972483
Help says:
"You can use the OpenCurrentDatabase method to open an existing database as the current database. You can use this method to open a database from another application that is controlling Microsoft Access through Automation, formerly called OLE Automation. For example, you can use the OpenCurrentDatabase method from Microsoft Excel to open the Northwind sample database in the Microsoft Access window."
The key thing here that this method works when you are controlling Access from another application. It sounds like you want the access session itself to close one database and open another.

One way to do what you want is:

Sub Switch_To(new_mdb)
  SendKeys "%{f}c%{f}o" & new_mdb & ".mdb{enter}"
End Sub

Call this sub with the name of the .meb file you want to open, e.g. Switch_To "sales"


Author Comment

ID: 1972484
I would rather not use sendkeys, its kind of slopy, do you know of another way?
LVL 17

Expert Comment

ID: 1972485
Sloppy? what does that mean? IF it works, why not use it?

I know of no other way within access. Consider that access is only functional when an mdb file is open in it. There is very little that can be done with no mdb open. The only way I know is to use sendkeys as illustrated or some other 3rd party macro player such as tempo.
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Author Comment

ID: 1972486
is there a way to open a new database in a new window and then close the current database and window? programicaly? (w/o sendkeys)
LVL 17

Expert Comment

ID: 1972487
Sub OpenDB(path) ' your pathname may vary
  Shell pathname:="c:\office97\office\msaccess.exe" & " " & _
  Chr(34) & path & Chr(34), windowstyle:=1
End Sub

Sub test()
  OpenDB "c:\foo\bar.mdb"
End Sub

LVL 12

Expert Comment

ID: 1972488
ramrom: I don't think Jambyte ment to offend you in any way. Sendkeys are sloppy programming because it makes your code depend on the user not touching the computer till the keys has done their job. My experience is that a lot of users get impatient and start cliking and typing "to see what happened" if the program does not appear to do anything. This could make the keys getting sent to the wrong destination (speaking from experience).

There is almost always a way around using the sendkeys approach; docmd domenuitem ... and in Access'97 docmd.runcommand ... But sometimes there is no way around and then we have to cross our fingers and hope that the user does not manage to interfer with the process.

Have a nice day !

Author Comment

ID: 1972489
any other way?

Expert Comment

ID: 1972490
Somtimes ago i had the same problem. I worked a lot on it.And the only way I found was to use sendkeys.

Accepted Solution

jtb earned 0 total points
ID: 1972491
Have you tried this?

Dim acc As Object
Set acc = CreateObject("Access.Application.7")
acc.OpenCurrentDatabase "C:\MYDB.MDB"

Now your database is open, have a look.  If you want it to stay open, you should open a form or something.

acc.Docmd.openform "Formname"

Hope this helps,

Expert Comment

ID: 6858511
Fixed the -1 problem on this question.
Escrow points corrected.

Community Support Moderator
Experts Exchange

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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