Open Access form from Excel

jcaptchaos2

Well-known Member
Joined
Sep 24, 2002
Messages
1,032
Office Version
  1. 365
Platform
  1. Windows
Not sure if I should post this here or on the excel board but here is my question.

I am using this code to try and open a form in access from excel, it opens the database and the form that automatically opens when the database but not the form I want. Is there a way to only open the form I want and not the main form?

Code:
Sub OpenAccess2()
Dim ac As Object
On Error Resume Next
Set ac = GetObject(, "Access.Application")
If ac Is Nothing Then
Set ac = GetObject("", "Access.Application")
ac.OpenCurrentDatabase "N:\Fishbowl Databases\Labor Collection Min.accdb"
ac.DoCmd.Openform "Earned Hours_Cast"
ac.UserControl = True
Set ac = Nothing
End If
AppActivate "Microsoft Access"
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I'm not sure I really understood your question, but what I took it to mean was that you want to "override" the form that is displayed on start up and show a different form instead. I also assumed that you didn't want to alter this property so that it always opens the new form, just when the subroutine is ran? (otherwise you might as well just set the property in Access itself).

Anyway, my knowledge of the Access Object Model isn't the greatest, but I've amended your code to add these 3 lines in

Code:
Dim str As String
str = ac.CurrentDb.Properties("StartUpForm")
ac.DoCmd.Close acForm, str

like this


Code:
Sub OpenAccess2()
Dim ac As Object
Dim str As String
On Error Resume Next
Set ac = GetObject(, "Access.Application")
If ac Is Nothing Then
Set ac = GetObject("", "Access.Application")
ac.OpenCurrentDatabase "N:\Fishbowl Databases\Labor Collection Min.accdb"
str = ac.CurrentDb.Properties("StartUpForm")
ac.DoCmd.Close acForm, str
ac.DoCmd.Openform "Earned Hours_Cast"
ac.UserControl = True
Set ac = Nothing
End If
AppActivate "Microsoft Access"
End Sub

Hope this helps

Simon
 
Upvote 0
Simon,

Thanks for the code but for some reason the form still stays open and does not close. Here is what I'm using

Code:
Sub OpenAccess2()
Dim ac As Object
Dim str As String
On Error Resume Next
Set ac = GetObject(, "Access.Application")
If ac Is Nothing Then
Set ac = GetObject("", "Access.Application")
ac.OpenCurrentDatabase "N:\Fishbowl Databases\Labor Collection Min.accdb"
str = ac.CurrentDb.Properties("Shop Floor Data Entry")
ac.DoCmd.Close acForm, str
ac.DoCmd.OpenForm "Earned Hours_Cast"
ac.UserControl = True
Set ac = Nothing
End If
AppActivate "Microsoft Access"
End Sub
 
Upvote 0
Hi JC,

The "StartUpForm" part of

Code:
[COLOR=#574123]str = ac.CurrentDb.Properties("StartUpForm")[/COLOR]

is an actual property relating to the form selected from the Office button>Access Options>Current Database>Display Form so it should pick up "Shop Floor Data Entry"

I suspect why this failed is that
Code:
ac.DoCmd.Close acForm, str
requires a reference to the Access Object Library to function, the acForm needs to be replaced with 2. I've amended your code to show you how to close a specific form or you can use the StartUpForm property if you wish. Hopefully this makes sense

Code:
Sub OpenAccess2()Dim ac As Object
Dim str As String
On Error Resume Next
Set ac = GetObject(, "Access.Application")
If ac Is Nothing Then
Set ac = GetObject("", "Access.Application")
ac.OpenCurrentDatabase "N:\Fishbowl Databases\Labor Collection Min.accdb"
'str = ac.CurrentDb.Properties("Shop Floor Data Entry")
ac.DoCmd.Close 2, "Shop Floor Data Entry" 'or str
ac.DoCmd.OpenForm "Earned Hours_Cast"
ac.UserControl = True
Set ac = Nothing
End If
AppActivate "Microsoft Access"
End Sub

Simon

 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,736
Members
449,466
Latest member
Peter Juhnke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top