Hello again MajP
I have an update. It consists of three pieces of code one is yours one I found on web and one is mine but they are too complicated for me to combine into one single sub.
So maybe your skills could achieve this one single sub for me.
Here is my entire code the portion of the sub highlighted in red is what needs to be edited to include the necessary bits of the two additional bits of code. All my code is contained on the single form where it executes and if possible I would like this to remain the same.
Option Compare Database
Option Explicit
Private db As Database
Private Sub cmdFindDb_Click()
Dim dlg As CommDlg
Set dlg = New CommDlg
With dlg
.hwnd = Me.hwnd
.Filter = "Databases|*.md?"
.Title = "Find Database"
.StartDir = "c:\my documents"
.ModeOpen = True
.Action
Me!strDb = .FileName
End With
End Sub
Private Sub cmdGetProperties_Click()
If (IsNull(strDb) Or Not Len(strDb) > 0) = 0 Then
Set db = OpenDatabase(strDb)
Call Me.GetDBProperties
cmdSetProperties.Enabled = True
Set db = Nothing
'Populate display form list.
strStartUpForm.RowSource = "SELECT Name FROM [" & strDb & "].MSysObjects WHERE " _
& "([Type] = -32768);"
'Populate menu bar and shortcut menu bar list.
Dim app As Object
Set app = CreateObject("Access.Application")
app.OpenCurrentDatabase strDb
Dim m As String
Dim sm As String
Dim sCmdBar As CommandBar
For Each sCmdBar In app.CommandBars
Select Case True
Case sCmdBar.BuiltIn = False And sCmdBar.Type = 1
m = m & sCmdBar.Name & ";"
Case sCmdBar.BuiltIn = False And sCmdBar.Type = 2
sm = sm & sCmdBar.Name & ";"
End Select
Next
strStartUpMenuBar.RowSource = m
strStartUpShortcutMenuBar.RowSource = sm
app.Quit
Set app = Nothing
Else
MsgBox "Can't find database."
End If
End Sub
Private Sub cmdIcon_Click()
Dim dlg As CommDlg
Set dlg = New CommDlg
With dlg
.hwnd = Me.hwnd
.Filter = "Icon Files|*.ico"
.Title = "Find Icon"
.StartDir = "c:\my documents"
.ModeOpen = True
.Action
Me!strAppIcon = .FileName
End With
End Sub
Private Sub cmdSetProperties_Click()
If Not Len(strDb) > 0 Then Exit Sub
Set db = OpenDatabase(strDb)
Call Me.SetDBProperties
Set db = Nothing
End Sub
Sub SetDBProperties()
Call ChangeProperty("AppTitle", dbText, strAppTitle)
Call ChangeProperty("StartUpForm", dbText, strStartUpForm)
Call ChangeProperty("StartUpMenuBar", dbText, strStartUpMenuBar)
Call ChangeProperty("StartupShortcutMenuBar", dbText, strStartUpShortcutMenuBar)
Call ChangeProperty("AppIcon", dbText, strAppIcon)
Call ChangeProperty("StartUpShowDBWindow", dbBoolean, blnStartUpShowDBWindow)
Call ChangeProperty("StartUpShowStatusBar", dbBoolean, blnStartUpShowStatusBar)
Call ChangeProperty("AllowShortcutMenus", dbBoolean, blnAllowShortcutMenus)
Call ChangeProperty("AllowFullMenus", dbBoolean, blnAllowFullMenus)
Call ChangeProperty("AllowBuiltInToolbars", dbBoolean, blnAllowBuiltInToolbars)
Call ChangeProperty("AllowToolbarChanges", dbBoolean, blnAllowToolbarChanges)
Call ChangeProperty("AllowBreakIntoCode", dbBoolean, blnAllowBreakIntoCode)
Call ChangeProperty("AllowSpecialKeys", dbBoolean, blnAllowSpecialKeys)
Call ChangeProperty("AllowBypassKey", dbBoolean, blnAllowSpecialKeys)
End Sub
Sub GetDBProperties()
strAppTitle = Nz(GetProperty("AppTitle"), "")
strStartUpForm = Nz(GetProperty("StartUpForm"), "")
strStartUpMenuBar = Nz(GetProperty("StartUpMenuBar"), "")
strStartUpShortcutMenuBar = Nz(GetProperty("StartUpShortcutMenuBar"), "")
strAppIcon = Nz(GetProperty("AppIcon"), "")
blnStartUpShowDBWindow = Nz(GetProperty("StartUpShowDBWindow"), True)
blnStartUpShowStatusBar = Nz(GetProperty("StartUpShowStatusBar"), True)
blnAllowShortcutMenus = Nz(GetProperty("AllowShortcutMenus"), True)
blnAllowFullMenus = Nz(GetProperty("AllowFullMenus"), True)
blnAllowBuiltInToolbars = Nz(GetProperty("AllowBuiltInToolbars"), True)
blnAllowToolbarChanges = Nz(GetProperty("AllowToolbarChanges"), True)
blnAllowBreakIntoCode = Nz(GetProperty("AllowBreakIntoCode"), True)
blnAllowSpecialKeys = Nz(GetProperty("AllowSpecialKeys"), True)
blnAllowBypassKey = Nz(GetProperty("AllowBypassKey"), True)
End Sub
Private Function ChangeProperty(strPropName As String, varPropType As Variant, varPropValue As Variant) As Boolean
Dim prp As Property
On Error GoTo Change_Err
If Len(varPropValue) > 0 Then
db.Properties(strPropName) = varPropValue
Else
db.Properties.Delete strPropName
End If
ChangeProperty = True
Change_Bye:
Set prp = Nothing
Exit Function
Change_Err:
Select Case Err
Case 3265 'Item not found in this collection.
'Do nothing.
Resume Next
Case 3270 'Prop not found.
With db
Set prp = .CreateProperty(strPropName, varPropType, varPropValue)
.Properties.Append prp
End With
Resume Next
Case Else
'Unknown error.
ChangeProperty = False
Resume Change_Bye
End Select
End Function
Private Function GetProperty(PropName As String) As Variant
Dim prop As Property
On Error GoTo GetProperty_err
Set prop = db.Properties(PropName)
GetProperty = prop.Value
GetProperty_end:
Exit Function
GetProperty_err:
GetProperty = Null
Resume GetProperty_end
End Function
What I need is the necessary bits of the following code (which takes care of autoexec macro) added into my above portion of code highlighted in red
Private Declare Function SetKeyboardState _
Lib "user32" _
(lppbKeyState As Any) _
As Long
Private Declare Function GetKeyboardState _
Lib "user32" (pbKeyState As Any) _
As Long
Private Declare Function GetWindowThreadProcessId _
Lib "user32" _
(ByVal hWnd As Long, _
lpdwProcessId As Long) _
As Long
Private Declare Function AttachThreadInput _
Lib "user32" _
(ByVal idAttach As Long, _
ByVal idAttachTo As Long, _
ByVal fAttach As Long) _
As Long
Private Declare Function SetForegroundWindow _
Lib "user32" _
(ByVal hWnd As Long) _
As Long
Private Declare Function SetFocusAPI _
Lib "user32" Alias "SetFocus" _
(ByVal hWnd As Long) _
As Long
Private Const VK_SHIFT = &H10
Private Const VK_LSHIFT = &HA0
Private Const VK_RSHIFT = &HA1
Function fGetRefNoAutoexec( _
ByVal strMDBPath As String) _
As Access.Application
On Error GoTo ErrHandler
Dim objAcc As Access.Application
Dim TIdSrc As Long, TIdDest As Long
Dim abytCodesSrc(0 To 255) As Byte
Dim abytCodesDest(0 To 255) As Byte
If (Len(Dir$(strMDBPath, vbNormal)) = 0) Then
Err.Raise 53
End If
Set objAcc = New Access.Application
With objAcc
.Visible = False
' attach to process
TIdSrc = GetWindowThreadProcessId( _
Application.hWndAccessApp, ByVal 0)
TIdDest = GetWindowThreadProcessId( _
.hWndAccessApp, ByVal 0)
If CBool(AttachThreadInput(TIdSrc, TIdDest, True)) Then
Call SetForegroundWindow(.hWndAccessApp)
Call SetFocusAPI(.hWndAccessApp)
' Set Shift state
Call GetKeyboardState(abytCodesSrc(0))
Call GetKeyboardState(abytCodesDest(0))
abytCodesDest(VK_SHIFT) = 128
Call SetKeyboardState(abytCodesDest(0))
' Open a mdb with Autoexec
Call .OpenCurrentDatabase(strMDBPath, False)
Dim m As String
Dim sm As String
Dim sCmdBar As commandbar
For Each sCmdBar In objAcc.CommandBars
Select Case True
Case sCmdBar.BuiltIn = False And sCmdBar.Type = 1
m = m & sCmdBar.Name & ";"
Case sCmdBar.BuiltIn = False And sCmdBar.Type = 2
sm = sm & sCmdBar.Name & ";"
End Select
Next
strStartUpMenuBar.RowSource = m
strStartUpShortcutMenuBar.RowSource = sm
' Revert back keyboard state
Call SetKeyboardState(abytCodesSrc(0))
End If
' release
Call AttachThreadInput(TIdSrc, TIdDest, False)
Call SetForegroundWindow(Application.hWndAccessApp)
Call SetFocusAPI(Application.hWndAccessApp)
End With
Set fGetRefNoAutoexec = objAcc
Set objAcc = Nothing
Exit Function
ErrHandler:
If (TIdDest) Then Call AttachThreadInput(TIdSrc, TIdDest, False)
Call SetForegroundWindow(Application.hWndAccessApp)
With Err
.Raise .Number, .Source, .Description, .HelpFile, .HelpContext
End With
End Function
And the necessary bits of your code below (which takes care of starttup form) added into my portion of code highlighted in red
Public Sub GetCBs()
Dim db As DAO.Database
Dim strPath As String
Dim startUpform As String
Dim app As Access.Application
Dim custBars As Collection
Dim custShortCutBars As Collection
Dim custNonShortCutBars As Collection
Dim i As Integer
strPath = strDb 'GetOpenFile()
Set db = getDb(strPath)
startUpform = getStartUp(db)
TurnOffStartUp db
Set app = New Access.Application
app.OpenCurrentDatabase (strPath)
Set custBars = getCustBars(app)
Set custShortCutBars = getCustShortCutBars(app)
Set custNonShortCutBars = getCustNonShortCutBars(app)
app.CloseCurrentDatabase
Set db = app.CurrentDb
Set db = getDb(strPath)
TurnOnStartUp db, startUpform
db.Close
Debug.Print "all custom bars:"
'All bars
For i = 1 To custBars.Count
Debug.Print custBars(i)
Next i
Debug.Print "all shortcut bars:"
'Short cut only
For i = 1 To custShortCutBars.Count
Debug.Print custShortCutBars(i)
Next i
'Not short cut
Debug.Print "Non shortCut"
For i = 1 To custNonShortCutBars.Count
Debug.Print custNonShortCutBars(i)
Next i
End Sub
Public Function getDb(strPath As String) As DAO.Database
Set getDb = DBEngine(0).OpenDatabase(strPath)
End Function
Public Function getCustBars(app As Access.Application) As Collection
' all bars
Dim col As New Collection
Dim cb As Object
For Each cb In app.CommandBars
If cb.BuiltIn = False Then
col.Add (cb.Name)
End If
Next cb
Set getCustBars = col
End Function
Public Function getCustShortCutBars(app As Access.Application) As Collection
' only short cut bars
Dim col As New Collection
Dim cb As commandbar
For Each cb In app.CommandBars
If cb.BuiltIn = False Then
If cb.Type = msoBarTypePopup Then
col.Add (cb.Name)
End If
End If
Next cb
Set getCustShortCutBars = col
End Function
Public Function getCustNonShortCutBars(app As Access.Application) As Collection
' Menu bars that are not shortcut bars
Dim col As New Collection
Dim cb As commandbar
For Each cb In app.CommandBars
If cb.BuiltIn = False Then
If cb.Type <> msoBarTypePopup Then
col.Add (cb.Name)
End If
End If
Next cb
Set getCustNonShortCutBars = col
End Function
Public Function getStartUp(db As DAO.Database) As String
Dim prp As DAO.Property
For Each prp In db.Properties
If prp.Name = "startupform" Then
getStartUp = prp.Value
Exit For
End If
Next
End Function
Public Sub TurnOffStartUp(db As DAO.Database)
Dim prp As DAO.Property
For Each prp In db.Properties
If prp.Name = "startupform" Then
prp.Value = "(None)"
Exit For
End If
Next
End Sub
Public Sub TurnOnStartUp(db As DAO.Database, strFrm As String)
Dim prp As DAO.Property
For Each prp In db.Properties
If prp.Name = "startupform" Then
prp.Value = strFrm
Exit For
End If
Next
End Sub