Access
Cambiar el nombre de macros en bases de datos externas de Microsoft Access usando VBA
Pensé en simplemente compartir una función que ideé para ayudar a alguien en UtterAccess en caso de que pudiera ayudar a alguien más.
La pregunta que se hizo originalmente fue:
Me gustaría poder desactivar la macro autoexec en una base de datos externa cambiándole el nombre usando VBA.
Básicamente, preguntamos cómo podemos cambiar el nombre de una macro en una base de datos externa.
Bueno, esto es muy sencillo de lograr y sólo requiere un par de líneas de código.
'--------------------------------------------------------------------------------------- ' Procedure : RenameMacroInExternalDB ' Author : Daniel Pineault, CARDA Consultants Inc. ' Website : ' Purpose : Rename the specified macro in an external database ' Copyright : The following is release as Attribution-ShareAlike 4.0 International ' (CC BY-SA 4.0) - ' Req'd Refs:None required ' ' Input Variables: ' ~~~~~~~~~~~~~~~~ ' sDb : Fully qualified path and filename of the external database in ' which the macro resides ' sCurrentMacroName : Current Macro name which is to be renamed ' sNewMacroName : New name to be assigned to the Macro in question ' ' Usage: ' ~~~~~~ ' RenameMacroInExternalDB("C:\Temp\Clients.accdb", "AutoExec", "AutoExecDisabled") ' ' Revision History: ' Rev Date(yyyy-mm-dd) Description ' ************************************************************************************** ' 1 2025-01-03 UA Help '--------------------------------------------------------------------------------------- Sub RenameMacroInExternalDB(ByVal sDb As String, _ ByVal sCurrentMacroName As String, _ ByVal sNewMacroName As String) On Error GoTo Error_Handler Dim oAccess As Access.Application ' Create a new instance of Access Set oAccess = New Access.Application ' Open the external database oAccess.OpenCurrentDatabase sDb ' Rename the macro oAccess.DoCmd.Rename sNewMacroName, acMacro, sCurrentMacroName Error_Handler_Exit: On Error Resume Next 'Cleanup oAccess.CloseCurrentDatabase oAccess.Quit Set oAccess = Nothing Exit Sub Error_Handler: MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _ "Error Source: RenameMacroInExternalDB" & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Description: " & Err.Description & _ Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _ , vbOKOnly + vbCritical, "An Error has Occurred!" Resume Error_Handler_Exit End Sub
Al revisar este código, probablemente pueda deducir que el mismo enfoque podría usarse fácilmente para cambiar el nombre de cualquier objeto, y no se limita a las macros.