Opening a new DB with VBA - vba

I am trying to open another db using VBA from my current db then close the one I am in. When I use the code listed here it opens Access but closes it immediately. I am sure it is simply something I am overlooking but the past hour I have wracked my brain. Any help would be greatly appreciated.
Private Sub Command115_Click()
Dim objAccess As Access.Application
Const conPATH = "C:\Users\user\Desktop\Database1.accdb"
'Create an instance of the Access application object.
Set objAccess = CreateObject("Access.Application")
'Open the database
objAccess.Visible = True
objAccess.OpenCurrentDatabase conPATH
'Open the form.
objAccess.DoCmd.OpenForm "Main-Form"
' Maximize other Access window
objAccess.DoCmd.RunCommand acCmdAppMaximize
End Sub
Thanks in advance for any help in this matter

If you use the shell then when you close your first database, the second will remain open.
Sub test()
Dim sh As Variant
sh = Shell("""C:\...\MSACCESS.EXE"" ""C:\...\FileName.accdb""")
End Sub
My guess the reason your code didn't work is because, the second access you start is an object that exists within the first one. The moment the first one closes and cleanup starts for its objects/variables. It closes the second one.

Say your first app is access1 and it is trying to open access2 and then close access1 app and makes access2 as active app. One thing you could do is,
Once your access1 app loads try to open access2 app programatically and make it visible and then use Application.closeCurrentdatabase to close access1 database and then in the unload event of access1 apps form (if there is any form in access1 app) call Application.Quit.
This works.

Related

Open a new database and close the old one

Greetings all: I have been struggling for 2 days to figure this out and have tried various techniques with frustrating results. I'm trying to start up a different database and close/quit the calling (first) database. I've created 2 button-click subs, one works very well and the second (which appears to be identical) flashes the new Db and then immediately returns to the calling DB. Here's the code that works well:
Dim objLeaveLookup As Object
Dim objOtherMenu As Object
Set objOtherMenu = GetObject("M:\QueryData\DBA Maintenance.mdb")
Set objLeaveLookup = GetObject("M:\QueryData\DBA_LookUp.mdb")
' Open Maintenance menu & quit DBA_Lookup
objOtherMenu.DoCmd.OpenForm "frm_MaintMenu"
objLeaveLookup.Application.Quit
and here is the code flashes the different Db, then frustratingly immediately returns to the original Db:
Dim objLeaveLookup As Object
Set objOtherMenu = GetObject("M:\QueryData\PurchMenu.mdb", "Access.Application").Application
Set objLeaveLookup = GetObject("M:\QueryData\DBA_LookUp.mdb", "Access.Application").Application
' Open Purchasing menu & quit DBA_LookUp
objOtherMenu.DoCmd.OpenForm "frm_PurMenu"
objLeaveLookup.Application.Quit
Any help to get the desired results will be GREATLY appreciated.
You may use FollowHyperlink.
See here: https://www.devhut.net/2018/01/21/ms-access-vba-open-another-database/

VBA - How to set focus on OLE object window (IBM DOORS)

I'm writing a script in the VB to be used in excel, I'm using OLE to run a DXL script in DOORS. The DOORS script creates a popup window, which I'd like to give focus to when it's created.
Currently I have reference to the DOORS object, but I can't seem to find out how to set focus to the window. It might be that it's something that I have to do in the dxl, but I was wondering if there's a way to do this on the VB side of things.
So far I have:
Public Sub DoThing()
Const DxlFilepath As String = "C:\FilePath"
Dim DOORSObj As Object
'Double check the user wants to do this
vbCreateList = MsgBox("Current list will be lost. Please confirm to proceed? (Note: Parent folder must be selected in DOORS popup)", vbOKCancel, "Do thing")
If (vbCreateList = vbCancel) Then
Exit Sub
End If
'Get access to the DOORS application database
Set DOORSObj = CreateObject("DOORS.Application")
DOORSObj.result = "OK"
DOORSObj.runFile (DxlFilepath)
End Sub
Thanks in advance

Close MS Project using VSTO

I have a VSTO on MS Project. I use VB.NET. What I need is when I press the button I created on the ribbon, it will perform some codes which will update the info of some task, however, I would need to close the MS Project automatically. I tried application.FileCloseEx(), but it only closes the file, the MS Project is still loaded. I need similar to clicking the x button of the window.
Thanks,
Gilbert
If your MS Project application object is represented by "appMSProject" then it's as simple as:
appMSProject.Quit
OR say in a macro running under Project:
Application.Quit
Here's how I do it in VBA from Excel or Access. As far as I can tell the objects & methods are the same in VB.NET. Bottom line is that I create an instance of the MS Project object which starts the app & opens a file, execute some work, close the file, then destroy the MS Project object by setting it to Nothing. That has the effect of closing the app. You can also use "appMSProject.Quit" followed by setting it to Nothing. Frankly the 2nd option looks more orderly & easier to understand in code. Anyway, here's a sample of the way I do it:
Dim appMSProject As MSProject.Application
Dim prjPrj As MSProject.Project
Dim strPrjFile As String
strPrjFile = "C:\where_is_my_file\file_name.mpp"
Set appMSProject = New MSProject.Application
appMSProject.FileOpenEx Name:=strPrjFile
Set prjPrj = appMSProject.ActiveProject
'''Do something in here with the prjPrj
'Close the file, in my case w/o saving
appMSProject.FileCloseEx pjDoNotSave
'Destroy the objects
Set prjPrj = Nothing
Set appMSProject = Nothing
FYI - In this example I'm doing background work so I don't show the app. I also use "early binding".
Here's an MSDN example that does show the app with more info on early -vs- late binding - https://msdn.microsoft.com/en-us/library/office/ff865152.aspx

check if excel instance already exist Vb.net

how can i check if an instance of a workbook already exist? During my programm i would be able to close excel without the application crash...
When i'm using the instance in the programm i want to check if the instance exist when not, i open it again...
thanks
Dim neue_excelinstanz As excelapp.Application
Dim neue_workbook As excelapp.Workbook
Dim neue_worksheet As excelapp.Worksheet
neue_excelinstanz = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
neue_workbook = CType(neue_excelinstanz.Workbooks.Open("C:\Users\global.xlsx"), Microsoft.Office.Interop.Excel.Workbook)
neue_worksheet2 = CType(neue_workbook.Worksheets(2), Microsoft.Office.Interop.Excel.Worksheet)
Would it maybe just be an If block? Im not sure how to code in German, but:
If neue_workbook Is Nothing Then
neue_workbook = CType(neue_excelinstanz.Workbooks.Open("C:\Users\Nicolas Grichting\Desktop\Vmc_vapeur\global.xlsx"), Microsoft.Office.Interop.Excel.Workbook)
End If
I would maybe also use threading.sleep to give it time to load before having the program try it again. But this depends on how youre using this.

Leaving a Project file open after retrieving it with GetObject

What is the right way to leave an MS Project file opened with GetObject() open and visible to the user after the end of a macro in a different app?
The information I found online suggests that setting the Application.UserControl property to True before the objects go out of scope should allow the user to continue using the opened file. However, for MS Project at least, the Application.UserControl property appears to be read-only. Is there a way to work around this?
A simplified example showing the problem:
Sub AddTasks()
Dim proj As Object
' Already have the file path from another part of the workflow
Set proj = GetObject("C:\projtest.mpp")
' perform some calculations and add new tasks to project
proj.Tasks.Add "additional task"
' Leave Project open and visible for the user
proj.Application.Visible = True
proj.Application.UserControl = True ' Gives "Type Mismatch" error
' without the UserControl line, runs ok, but Project closes after the end of the macro
End Sub
Instead of using GetObject, could you create an instance of the application and open the project file in the instance?
Sub AddTasks()
Dim msProj as Object
Set msProj = CreateObject("Project.Application")
msProj.FileOpen "C:\projtest.mpp"
'do stuff to project file here
msProj.Visible = True
End Sub
Something like the above (I can't test the above code because I don't have MSProject, but similar code works for MSWord)
For Project UserControl just indicates if the user started the application or not; it appears to be read-only because it is. I've not done what you're asking for with Project, although here is a similar example for Word trying to see and find running instances of Excel. Perhaps this helps a little:
can-vba-reach-across-instances-of-excel