How to bypass MSAccss AutoExec macro and bypass Startup form - vba

How do I bypass the MSAccess autoexec macro and startup form for a deep-legacy code upgrade of a large MS Access 2003 application with hundreds of forms and reports?
It is an upgrade from Access 2003 to Access 2016, 2019 or 365.
This is a mission critical system kept alive and on crutches for 15 years without any VBA code updates.
Files in the application
Multiple MSAccess files in MDB and ACCDB format
No MSAccess files in MDE or ACCDE formats with compiled VBA code
No MSAccess other files wuch as mdw security files
I run a dos command for the database - PATH_TO_MSACCESS.exe DB_NAME.mdb
I'm using MSAccess.exe 32 bit from Office 365.
Note that there are compatibility and VBA compiler errors if you run on a 64 bit MSAccess.exe if the VBA calls Windows operating system Win32 API methods. This app calls a few (5) Win32 API calls. Technical, MS Access 64 bit will treat some 32 bit data sent in/returned from the Win32 API as 64 bit causing errors.
The most difficult part is that many of the web pages and nearly all Microsoft pages related to this have been deleted from the web.
Tried but did not work
Holding down shift key when you open the MSAccess database
Hitting F11 to open the Navigation Pane in Access (does not open). If Navigation Pane opens I could edit the AutoExec macro or the startup form's Form_Open code
Tried, not perfect, and works
Run a macro which does not exist on MSAccess.exe command line, hit escape multiple times on the error messages, the click on the MSAccess ribbon to get to the VBA code. Messy, but it gets me into the VBA code.
Added a "Stop" as the first line of the macro named "autoexec" and also as the first line of the startup form's "Form_Open()" method. I had to add an empty "Form_Open()" event handler for the form
Current status:
The application runs OK on a machine with MS Access version before 2016
It fails multiple ways when only 32 bit MS Access 365/2019 is installed on the machine.
I have been finding and fixing things like bad configuration file entries, incorrect installation path, etc. but need to debug the VBA startup code and initial form load in the VBA debugger.
I cannot directly get into the VBA debugger on the first line of the AutoExec macro or start up form's Form_Open function. MSAccess always runs the autoexec macro and shows the startup form.
I can get into the VBA by running MSaccess.exe command line and specifying that it runs a macro which does not exist.
Here are possible solutions based on Google searching broken out by Access version since the code/database settings in question could be specific to any Access version from 95 to 2010.
Access 2007: Opening an MS-Access database from the command line without running any of the startup vba code?
Hold down shift key when opening MDB database
Access XP
Open access database without executing scripts or forms
Hold down shift key when opening the Access database
Remove AutoExec macro
Remove the startup form setting from the database
Access 2007:
Emulating a SHIFT key press when using VBA to open an ms-access database secured by an mdw file?
Slightly different case where the Access database is secured by a MDW security file
Same answers
Access XP/2003/2007?
How to skip Autoexec macro when opening MSAccess from MSAccess?
Method One:
Original URL is dead, Internet Archive Wayback machine has an archived copy: https://web.archive.org/web/20101204113950/http://www.mvps.org/access/api/api0068.htm
Send Shift key to Access via code to bypass startup macro if the [AllowbypassKey] is not set
Method Two:
Extract the Autoexec macro from the database, replace it with a blank AutoExec macro
Uses DoCmd.DatabaseTransfer acImport and DoCmd.DatabaseTransfer acExport
Method Three:
Rename the AutoExec macro using VBA code
OpenCurrentDatabase ("Your database")
DoCmd.Rename "Autoexec", acMacro, "tmp_Autoexec"
CloseCurrentDatabase
MS Access keyboard short cuts for getting at the VBA code or objects in an Access database. From https://support.microsoft.com/en-us/office/keyboard-shortcuts-for-access-70a673e4-4f7b-4300-b8e5-3320fa6606e2
I haven't tried the MSAccess keyboard short cuts to see if they let me open and view the Access VBA code, toolbars, table/form dedign viewer, or switch to code editing mode. I've included them here for completeness.
F2 - Switch between Edit mode (with insertion point displayed) and Navigation mode in the Datasheet or Design view
F4 - Open properties pane for an object
F5 - Switch to Form view from the form Design view
F6 - Switch between panes in the MS Access interface
F10 (?) unhide the ribbon
F11 - Show or hide the Navigation Pane
Alt-X, Alt-X,1 - Open the External Data tab in the ribbon
Alt-Y - Open the Database Tools tab in the ribbon
Alt-J,T - Open the Table tab in the ribbon
Alt-X,2 - Open the Add-ins tab in the ribbon
Control-F1 - Expand/collapse the ribbon
Alt-F11 - Switch to/from the VBA editor
Show or hide the MSAccess ribbon toolbar in VBA code. Included here for completeness. This application hides the ribbon bar on application startup.
MSAccess - Minimize the Toolbar Ribbon OnLoad()?
MSAccess 2010 onwards. The acToolbarNo is in the VBA code for this application
DoCmd.ShowToolbar "Ribbon", acToolbarNo 'Hides the full toolbar
DoCmd.ShowToolbar "Ribbon", acToolbarYes 'Show
MSACcess 2010, 2013
CommandBars.ExecuteMso "MinimizeRibbon"
Before MSAccess 2010
SendKeys "^{F1}", False
Special case: You may get an error on the Access startup form if it has a record source which has an error. this is not the case for my application but included here completeness
difficulty tracing microsoft access VBA code
Special case: You get an infinite loop of dialog prompts or errors from the startup form. Hold down the "Control-Break" key while clicking on OK for the error message to break out of the loop of errors. https://bettersolutions.com/vba/debugging/index.htm
It may be possible to break out of the main startup form to the MS Access object explorer by right clicking on the startup form's title bar or right click on the startup form's body.
Right clicking on the startup form's title bar has these menu commands
Save
Close and Close All
Form View
Layout View
Design View
Right clicking on the startup form's body has these menu commands
Form View
Layout View
Design View
Cut, Copy, Past (disabled)
Form Properties (disabled)
Properties (disabled)
Close

the other thing to check? Are you using a shortcut? if it has the /runtime swtich in it, then the shift key will be ignored NO MATTER what you do, and even if no shfit key by-pass code (to disable) shift key means the shift key will STILL be ignored. So, you want to ensure that you not launching/using a shortcut.
you also want to check/ensure/find out/be aware if the application has workgroup security. Again, in 99 out of 100 cases, the shortcut will show this.
next up:
is this a mdb, or mde file? The mde file is a compiled version. No source code exists, and you can't modify the mde. So, again, ensure that you have a mdb file for the front end, not a mde. If you don't have that mdb, then you are in big trouble - you don't have the source code.
You have all this info in your post, but you leave out the most important issues.
So, is this a mde, or mdb? You need to know this.
Is there a worgroup security file (mdw) specifed in the link that is typical used to launch the application. If workgroup secuirty is involed, then the logon id you use might get you past shift key, but then that user might not have been given design rights, so at that point, shify key by-pass will be of zero use to get into the code.
I mean, launch your copy of access 2016 or whatever. Then try to import the objects from that database. This way you don't have to use or ever worry about shfit key, but are doing a simple import of the forms, reports and code into a brand new fresh database.
So, another question:
Don't bother launching the application - create a blank new database, and then import from the existing - can you do this? (doing this does NOT copy the shift key setting of the original database).

MSAccess command line lets you tell it what macro to execute on startup.
I ran the following cmd.exe command line which generates multiple errors and allows you to get into the Access database with the navigator and get into the VBA code. Not the best solution but one possibility.
MSAccess.exe DB /X ADEEERETDEREAR
DB is the full path to the Access database
ADEERETDEREAR is a macro which does not exist

Access 2007?
How to disable Macro and Start-Up values while opening the MS Access DB
Access 2003?
Bypasss shift key. These link to Zip files projects available for download
https://web.archive.org/web/20071214172548/http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html
https://web.archive.org/web/20071214172548/http://www.members.shaw.ca/AlbertKallal/msaccess/shiftkey.zip
https://web.archive.org/web/20071214172548/http://www.members.shaw.ca/AlbertKallal/msaccess/shiftkey2000.zip
Access 2007:
remove autoexec macro from MS Access 2007
Create new macro and then rename it in the Access UI to autoexec, say yes to the prompt to overwrite the existing AutoExec macro
Access 2010?
Opening an MS-Access database from the command line without running any of the startup vba code?
Access ?
Disable F11 Key in MS Access to prevent opening the Navigation Pane
Open the Access database, let the main form be shown
Hit F11 to show the navigation pane
A guess that one could modify the autoexec macro and/or the startup form from the navigation pane
Access ?
https://bytes.com/topic/access/answers/211664-programatically-set-startup-form
A guess that you could use VBA in one Access database to open the target database
Get the name of the startup form
Change the startup form's name or maybe blank out the startup form's name
VBA code similar to CurrentDB.Properties("StartupForm") = "MyForm"
Another guess would be to blank out the startup form's name in the database properties
Same may work for the autoexec macro

Access 2010?
Reset startup form to nothing in VBA code
Code from 2012 is here: https://www.tek-tips.com/viewthread.cfm?qid=1673392
First way
Dim strOriginalForm as String
Dim db as Database
Sub RemoveStartup()
Set db = OpenDatabase(yourdatabase)
strOriginalForm = db.Properties("StartUpForm")
db.Properties("StartUpForm") = "(none)"
db.Close
set db = Nothing
End Sub
Sub ResetStartup()
Set db = OpenDatabase(yourdatabase)
db.Properties("StartUpForm") = strOriginalForm
db.Close
Set db = Nothing
End Sub
Second way
Set prp = db.CreateProperty("AllowByPassKey", dbBoolean, True)
db.Properties.Append prp
Third way
Delete the property using - database.properties.delete propertyname
A more complete example from the same page exists.

I have not tried to import the Access objects into a new database. (Thanks Albert Kallal for the information)
This would allow me to look at the VBA code. It may not work as a replacement for the original database with all of the settings internal to the database.
How to import the Access objects from another Access database:
https://support.microsoft.com/en-us/office/import-database-objects-into-the-current-access-database-23aea08b-7487-499d-bdce-0c76bedacfdd
Access 365 steps (likely works for Access 2016)
External Data tab in ribbon
Click New Data Source -> From Database -> Access in the Import & Link ribbon group
Get External Data - Access Database window is shown
Browse for the MSAccess database MDB or ACCDB file in the File Name Field
The Import Objects window is shown
Select the tables, queries, forms, reports macros, modules to import
In the Options button dialog, you can select menus, toolbars, etc. to import
Click on OK
For Names duplicated, Access will append a 1,2,3 to the end of an imported object's name

Access 2010?
Reset startup form to nothing in VBA code
Code from 2012 is here: https://www.tek-tips.com/viewthread.cfm?qid=1673392
Fourth way as mentioned above
A more complete example from the same page.
Code from 2012 is here: https://www.tek-tips.com/viewthread.cfm?qid=1673392
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
Dim blnAutoexec As Boolean
strPath = GetOpenFile()
'Get the db without opening in application
Set db = getDb(strPath)
'Get startupform
startUpform = getStartUp(db)
'Turn off the start up form
TurnOffStartUp db
'Check for and auto exec. If exists import and replace
If hasAutoexec(db) Then
blnAutoexec = True
ImportAutoExec (strPath)
End If
Set app = New Access.Application
'Open safely
app.OpenCurrentDatabase (strPath)
'Read command bars
Set custBars = getCustBars(app)
Set custShortCutBars = getCustShortCutBars(app)
Set custNonShortCutBars = getCustNonShortCutBars(app)
app.CloseCurrentDatabase
Set db = app.CurrentDb
Set db = getDb(strPath)
'Return start up form
TurnOnStartUp db, startUpform
db.Close
'Return auto exec
If blnAutoexec Then
ReturnAutoExec (strPath)
End If
Debug.Print "all custom bars:"
'All bars
For i = 1 To custBars.Count
Debug.Print custBars(i)
Next i
'Do something with the command bars
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
Public Sub ImportAutoExec(strPath As String)
On Error GoTo errLbl
DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, acMacro, "AutoExec", "AutoExecBackup"
DoCmd.TransferDatabase acExport, "Microsoft Access", strPath, acMacro, "TempAutoExec", "AutoExec"
Exit Sub
errLbl:
If Err.Number = 7874 Then
Debug.Print "Auto Exec macro does not exist"
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub
Public Sub ReturnAutoExec(strPath As String)
On Error GoTo errLbl
DoCmd.TransferDatabase acExport, "Microsoft Access", strPath, acMacro, "AutoExecBackup", "AutoExec"
DoCmd.DeleteObject acMacro, "AutoExecBackup"
Exit Sub
errLbl:
If Err.Number = 7874 Then
Debug.Print "Auto Exec macro does not exist"
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub
Public Function hasAutoexec(db As DAO.Database) As Boolean
Dim rs As DAO.Recordset
Dim strSql As String
strSql = "SELECT MSysObjects.Name FROM MSysObjects WHERE MSysObjects.Name = 'AutoExec' AND MSysObjects.Type = -32766"
Set rs = db.OpenRecordset(strSql)
If Not (rs.EOF And rs.BOF) Then
hasAutoexec = True
End If
End Function

Related

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

Access autocad object properties without opening it by VBA

I have been using folder browser for VBA, I could paste the code of it, but bottom line is that I get returned file name as a string.
Is there any way to access drawing properties (i.e number of layouts) without open?
Public Sub TestFileDialog()
dwgname = FileBrowseOpen("C:", "*", ".dwg", 1) 'dwgname is typeof string
End Sub
Its only the first step (use of FileBrowseOpen function is shown, but also i can use FolderBrowse and collect all .dwg inside of folder),actually i had in mind to batch export all layouts of selected .dwgs to currenty open one. Is there any chance for that?
To effectively read a .dwg file you'll need to open AutoCAD, otherwise the information is not accessible. Some properties may be, such as author, but not number of layouts...
But you can use AutoCAD Console (accoreconsole.exe) to run a headless AutoCAD and use APIs to read any information you need. This is really fast for reading lot's of files and the user will not see it running (but it needs to be installed anyway).
http://aucache.autodesk.com/au2012/sessionsFiles/3338/3323/handout_3338_CP3338-Handout.pdf
you could stay in VBA and use ObjectDBX
it leads to a very similar approach as accoreconsole.exe on in .NET does, i.e you won't see any drawing open in UI since it works on the database itself
It requires adding library reference (Tools->References) to "AutoCAD/ObjectDBX Common XX.Y Type Library", where "XX.Y" is "19.0" for AutoCAD 2014
a minimal functioning code is
Sub main()
Dim myAxDbDoc As AxDbDocument
Dim FullFileName As String
FullFileName = "C:\..\mydrawing.dwg" '<== put here the full name of the file to be opened
Set myAxDbDoc = AxDb_SetDrawing(FullFileName)
MsgBox myAxDbDoc.Layers.Count
End Sub
Function AxDb_SetDrawing(FullFileName As String) As AxDbDocument
Dim DBXDoc As AxDbDocument
Set DBXDoc = Application.GetInterfaceObject("ObjectDBX.AxDbDocument.19") '<== place correct AutoCAD version numeber ("19" works for AutoCAD 2014)
On Error Resume Next
DBXDoc.Open FullFileName
If Err <> 0 Then
MsgBox "Couldn't open" & vbCrLf & vbCrLf & FullFileName, vbOKOnly + vbCritical, "AxDB_SetDrawing"
Else
Set AxDb_SetDrawing = DBXDoc
End If
On Error GoTo 0
End Function
Still, you must have one AutoCAD session running from which make this sub run! But you should have it since talked about "currently open" drawing

Outlook 2013 VBA: store user defined settings

Currently at the office we have Outlook 2003. We will be migrating to Outlook 2013.
In Outlook 2003 we have a commandbar that as example saves a mail item to a user specified folder or moves the item to the desired team.
In a userform the end-user can set his settings to his desired folder or select the team he is currently on. In this settings form there are multiple input field the user can fillout.
Whenever he clicks a button on the commandbar, outlook checks his settings to see on what team he is on, his desired save folder is, etc.
This userdefined settings are stored and called on by it's tags
(Application.ActiveExplorer.CommandBars("Toolbar").Controls.Item(1).tag)
As far i found on the internet Outlook 2013 does not support commandbars. I can instal the commandBar, but as soon as you restart outlook the bar is gone and the settings are gone.
Is there a way to save/store the settings made by the end-user in a userform so the scripts saves the mail item based on his settings to the correct folder or team?
I've tried to find a solution but haven't found it yet, or do not know where to look.
Hope you can guide me into the right direction to look for a solution.
(note: I know a little bit of VBA, can read and write it, but found it hard to explain how it works. If i left out some critical information in the question please let me know.)
Outlook doesn't allow to customize the Ribbon UI using VBA. The only thing you can do is to assign a macro to QAT button (or add controls manually in Outlook).
You need to develop an add-in to be able to customize the Ribbon UI (aka Fluent UI). See Walkthrough: Creating a Custom Tab by Using the Ribbon Designer for more information.
Read more about the Fluent UI in the following series of articles in MSDN:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
Is there a way to save/store the settings made by the end-user in a userform so the scripts saves the mail item based on his settings to the correct folder or team?
Using the Tag property is not the best way to store the user settings. Of course, you can standard ways for storing settings on the PC - files (XML, text or your own binary format), windows registry and etc.
But the Outlook object model provides hidden items for that. The GetStorage method of the Folder class returns a StorageItem object on the parent Folder to store data for an Outlook solution. See Storing Data for Solutions for more information.
As promised a few code samples wich i used to store and get the settings.
Maybe not the best way to do it, but it solved my problem to store the settings and maybe it could help someone else.
First of all I made a little check to see if the settings are already there.
Function Hidden_Settings_Aanwezig() As Boolean
Dim oNs As Outlook.Namespace
Dim oFL As Outlook.folder
Dim oItem As Outlook.StorageItem
On Error GoTo OL_Error
Set oNs = Application.GetNamespace("MAPI")
Set oFld = oNs.GetDefaultFolder(olFolderInbox)
Set oItem = oFld.GetStorage("Hidden Settings", olIdentifyBySubject)
If oItem.Size <> 0 Then
Hidden_Settings_Aanwezig = True
Else
Hidden_Settings_Aanwezig = False
End If
Exit Function
OL_Error:
MsgBox (Err.Description)
Err.Clear
End Function
If not, the following code creates the settings based on tekstboxes and checkboxes on a userform with the following code
Function Maak_Settings_Hidden()
Dim oNs As Outlook.Namespace
Dim oFld As Outlook.folder
Dim oSItem As Outlook.StorageItem
On Error GoTo OL_Error
Set oFld = Application.Session.GetDefaultFolder(olFolderInbox)
Set oSItem = oFld.GetStorage("Hidden Settings", olIdentifyBySubject)
'repeat the next to lines for every setting you want to store
oSItem.UserProperties.Add "Export Folder", olText
oSItem.UserProperties("Export Folder").Value = TextBox1.Text
oSItem.Save
Exit Function
OL_Error:
MsgBox (Err.Description)
Err.Clear
End Function
The functions above are called on with the following code:
If Hidden_Settings_Aanwezig = True Then
Call Get_Hidden_Settings_Startup
Else
Maak_Settings_Hidden
End If
To use one of the settings i use the following code.
In the main sub I use the following line:
DestFolder = Get_Hidden_Settings("Export Folder")
To call on this function:
Function Get_Hidden_Settings(Setting) As String
Dim oNs As Outlook.Namespace
Dim oFL As Outlook.folder
Dim oItem As Outlook.StorageItem
On Error GoTo OL_Error
Set oNs = Application.GetNamespace("MAPI")
Set oFld = oNs.GetDefaultFolder(olFolderInbox)
Set oItem = oFld.GetStorage("Hidden Settings", olIdentifyBySubject)
If oItem.Size <> 0 Then
Get_Hidden_Settings = oItem.UserProperties(Setting)
End If
Exit Function
OL_Error:
MsgBox (Err.Description)
Err.Clear
End Function
If I understand your problem correctly, what I would do is the following:
1) Export your VBA stuff into a *.bas files (for modules) and *.frx (for user forms) This is done in the VBA editor, File --> Export. You do this for each item (module and user form). Save these files e.g. on a memory stick, or whereever it suits you.
2) Import these files in Outlook 2013 into the VBA editor (same way, but --> File --> Import of course) e.g. by loading them from your memory stick.
This should make your VBA code available in your new Outlook 2013 environment.
3) Your command bars will not be available. But you can easily create something else: In the Office 2013 (etc.) products, you can add stuff to the "Ribbon". E.g. you can create a new tab called "My self-made tools", and you can place buttons there that call your VBA procedures. There you will find buttons for "Create new..."
To do so: --> File --> Optiobs --> Customize Ribbon --> Macros
Note: In a standard installation of Office 2013 (etc.) you will not have access to the VBA editor. To make the editor available, go through --> File --> Options --> Customize Ribbon and set a tick mark in the field for "Develooper tools". This will make a tab of that name appear in the "Ribbon".

Reading, Writing and controlling Autocad using external VBA

I'm using MS-Access 2010 and Autocad 2012 64bit and work in manufacturing.
I want to be able to at the very least, populate fields in a title block, better still I would like to use data in my access database to write data into a sheet set (the current system works by reading the sheet set values such as sheet title and number into my title block).
The following code is all I have at the moment and it will open autocad and write the date into the command line.
Private Sub OpenAutocad_Click()
Dim CadApp As AcadApplication
Dim CadDoc As AutoCAD.AcadDocument
On Error Resume Next
Set CadApp = GetObject(, "AutoCAD.Application")
If Err.Number <> 0 Then
Set CadApp = CreateObject("AutoCAD.Application")
End If
On Error GoTo 0
CadApp.Visible = True
CadApp.WindowState = acMax
Set CadDoc = CadApp.ActiveDocument
CadDoc.Utility.Prompt "Hello from Access, the time is: " & TheTime
Set CadApp = Nothing
End Sub
I have no idea where to go from here. What are the commands to control the sheet set manager and change data, and can the .dst file be edited without even opening up autocad? is there a list of all available autocad vba commands and functions?
If you are declaring CadApp as AcadApplication you must have added a reference to AutoCAD.
That means you should be able to see the object model using your Object Browser in your VBA IDE. No?
There is also a very helpful site www.theswamp.org which has a whole section devoted to AutoCAD VBA.
If I understand your question correctly, you want to automate filling attributes in a drawing title blocks (such as title, drawer, part number, etc) right from MS Access.
Your code can access the Autocad command line already, but Autocad doesn't seem to have the exact command for filling drawing attribute. (command list)
So looks like you need to fill the attributes programatically using the COM API.
The following question appears to be relevant with yours and the accepted answers does provide a sample code:
Is it possible to edit block attributes in AutoCAD using Autodesk.AutoCAD.Interop?
Note that in that question the asker was developing a standalone application in C# .NET, where as you will be using VB Automation from MS Access. Shouldn't be too different since the Component Object Model (COM) being used is the same.
What are the commands to control the sheet set manager and change data and can the .dst file be edited without even opening up autocad?
(sorry can't post more than 2 links)
docs.autodesk.com/ACD/2010/ENU/AutoCAD%202010%20User%20Documentation/files/WS1a9193826455f5ffa23ce210c4a30acaf-7470.htm
No mention about data change, though.
is there a list of all available autocad vba commands and functions?
Yes.
%ProgramFiles%\Common Files\Autodesk Shared\acad_aag.chm - Developer's Guide
%ProgramFiles%\Common Files\Autodesk Shared\acadauto.chm - Reference Guide
Online version:
help.autodesk.com/cloudhelp/2015/ENU/AutoCAD-ActiveX/files/GUID-36BF58F3-537D-4B59-BEFE-2D0FEF5A4443.htm
help.autodesk.com/cloudhelp/2015/ENU/AutoCAD-ActiveX/files/GUID-5D302758-ED3F-4062-A254-FB57BAB01C44.htm
More references here:
http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112
:) Half the way gone ;)
If you has a open autocad with a loaded drawing you can access the whole thing directly.
Sub block_set_attribute(blo As AcadBlockReference, tagname, tagvalue)
Dim ATTLIST As Variant
If blo Is Nothing Then Exit Sub
If blo.hasattributes Then
tagname = Trim(UCase(tagname))
ATTLIST = blo.GetAttributes
For i = LBound(ATTLIST) To UBound(ATTLIST)
If UCase(ATTLIST(i).TAGSTRING) = tagname Or UCase(Trim(ATTLIST(i).TAGSTRING)) = tagname & "_001" Then
'On Error Resume Next
ATTLIST(i).textString = "" & tagvalue
Exit Sub
End If
Next
End If
End Sub
Sub findtitleblock(TITLEBLOCKNAME As String, attributename As String,
attributevalue As String)
Dim entity As AcadEntity
Dim block As acadblcck
Dim blockref As AcadBlockReference
For Each block In ThisDrawing.BLOCKS
For Each entity In block
If InStr(LCase(entity.objectname), "blockref") > 0 Then
Set blockref = entity
If blockref.effectivename = TITLEBLOCKNAME Then
Call block_set_attribute(blockref, attributename, attributevalue)
exit for
End If
End If
End If
Next
Next
End Sub
call findtitleblock("HEADER","TITLE","Bridge column AXIS A_A")
So assume you has a title block which has the attribute TITLE then it will set the Attribute to the drawing name. it mioght also possible you has to replace the thisdrawing. with your Caddoc. I usually control Access and Excel form autocad and not vice versa ;)
consider also to use "REGEN" and "ATTSYNC" if "nothing happens"
thisdrawing.sendcommens("_attsync" 6 vblf )

How to open macros from other databases - VBA, MS Access 2003

I've been tasked with creating an Access 2003 application that will act as a focal point for all other databases (6 in total).
These 6 databases will each contain a macro used to build data bound for a table called DispenseExport.
Originally each of these databases had their own version of this table but now we have to have them all write to the one application - which I've affectionately codednamed Omega.
Omega will contain the DispenseExport table that all the others will write to. It will also be the database that calls the macros from the others to write to it.
So - what is the best way to accomplish this?
I already have a sample sub to call a macro from another database (and works) but the problem here is that it opens the database as any normal user would - Omega will sit on a server and needs to bypass this - by possibly using the SHIFT-KEY access method, if best?
How do I do that programmatically in VBA, assuming it's the best way to do so?
Here's what I have so far:
Option Compare Database
Option Explicit
'Sub to call a macro from another database
Public Sub CallMacro()
On Error GoTo ErrHandler:
Debug.Print "Opening Macro"
'/x tells the database to run the macro as soon as it is opened
Call Shell("msaccess.exe C:\path-to-database\database.mdb /x mcrTestCall", 0)
Debug.Print "Completed Macro"
ErrHandler:
If Err.Number <> 0 Then
'Create a message box with the error number and description
MsgBox ("Sorry but error number " & Err.Number & " occurred; " & vbCrLf & Err.Description)
End If
End Sub
UPDATE
Still haven't found the answer but believe that I'm closing in. I need to find out how to emulate HOLDING DOWN SHIFT - any ideas?
Thanks
This will avoid shell call (Access 2007, also Access 2003?):
'
' variables:
' strDbName: database filename
' strMacro: macro name
'
Sub CallMacro()
'
Dim strDbName, strMacro
Dim objApp
'
strDbName = "C:\path-to-database\database.mdb"
strMacro = "mcrTestCall"
'
Set objApp = CreateObject("Access.Application")
'
objApp.OpenCurrentDatabase strDbName
'
' Run Macro:
'
objApp.DoCmd.RunMacro strMacro
'
objApp.Quit
Set objApp = Nothing
'
End Sub
To skip startup form or AutoExec, there is no simple solution, but we have a workaround in http://access.mvps.org/access/api/api0068.htm, by simulating the Shift key, using API.
There is a cleaner way to do this without opening the remote database. Build a query within that database that does what you want, then call it from the other database without ADO, DAO or opening it. Below is a link explaining how:
Remote Queries
Since links go away, the gist of it is this:
1. Build the query you want to call to return what you need in the other database
2. In the remote calling database, open a query without selecting a table (I use Query Design)
3. Right click on the query background and select 'Properties'
4. Replace the Database setting of (Current) with the full path to the database with the query
5. Build a query to call the original query as the datasource
6. Output the data by a "SELECT INTO" or DoCmd.TransferSpreadsheet, etc