As part of a demo database I was working on, I thought it would be useful it the user could click on a button and it would bring them directly to the procedure of interest.
So, if this can be useful to anyone else, here is the simple procedure to accomplish this.
Option 1
This function requires that you specify both the procedure name you are searching for, as well as the module in which it is found.
'---------------------------------------------------------------------------------------
' Procedure : VBE_GotoProc
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Open the VBE and goto to the specified procedure
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sModuleName : Name of the Module that contains the procedure to navigate to
' sProcName : Name of the procedure to navigate to
'
' Usage:
' ~~~~~~
' Call VBE_GotoProc("Module1", "ListWin32Processors")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2018-02-17 Initial Release
' 2 2018-02-18 Changed ProcStartLine to ProcBodyLine
' Changed from Sub to Function so it can be called
' from a Macro
'
' Useful References:
' ~~~~~~~~~~~~~~~~~~
' https://msdn.microsoft.com/en-us/vba/access-vba/articles/module-procbodyline-property-access
'---------------------------------------------------------------------------------------
Public Function VBE_GotoProc(ByVal sModuleName As String, ByVal sProcName As String)
On Error GoTo Error_Handler
Dim lProcLineNo As Long
With Application.VBE
.MainWindow.Visible = True 'Open the VBE so it is visible to the user
.ActiveVBProject.VBComponents(sModuleName).Activate 'Activate the module
lProcLineNo = .ActiveCodePane.CodeModule.ProcBodyLine(sProcName, 0) 'Locate the procedure
.ActiveCodePane.CodeModule.CodePane.SetSelection lProcLineNo, 1, lProcLineNo, 1 'Goto that line
End With
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: VBE_GotoProc" & 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 Function
Option 2
Unlike Option 1 in which you need to specify the module name to search for the procedure, Option 2 only requires the procedure name. It will iterate through all the Components, one by one, searching for the procedure.
'---------------------------------------------------------------------------------------
' Procedure : VBE_GotoProc
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Open the VBE and goto to the specified procedure
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sProcName : Name of the procedure to navigate to
'
' Usage:
' ~~~~~~
' Call VBE_GotoProc("ListWin32Processors")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2018-02-18 Initial Release
' Variation on VBE_GotoProc that iterates through all
' all the code modules searching for the specified
' Proc.
'
' Useful References:
' ~~~~~~~~~~~~~~~~~~
' https://msdn.microsoft.com/en-us/vba/access-vba/articles/module-procbodyline-property-access
'---------------------------------------------------------------------------------------
Public Function VBE_GotoProc(ByVal sProcName As String)
On Error GoTo Error_Handler
Dim VBComp As Object
Dim lProcLineNo As Long
With Application.VBE
.MainWindow.Visible = True 'Open the VBE so it is visible to the user
For Each VBComp In .ActiveVBProject.VBComponents
lProcLineNo = VBComp.CodeModule.ProcBodyLine(sProcName, 0) 'Locate the procedure
Exit For 'If we are here, it was found!
SkipVBComp:
Next VBComp
VBComp.Activate 'Activate the module
VBComp.CodeModule.CodePane.SetSelection lProcLineNo, 1, lProcLineNo, 1 'Goto that line
End With
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
If Err.Number = 35 Then
'Not found in the component, so let's move on to the next one.
GoTo SkipVBComp
Else
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: VBE_GotoProc" & 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 If
End Function
Ctrl+G
write procedure/function name
ShiftF2
Yes, this will work if you are manually in the database, but I was after a programmatic solution that could make the whole think transparent for the end-user, but always good to know and share with other developers.
Thanks for the article.Really thank you! Great.
Can this work with a public procedure in a form?
Try it out. It worked fine for me when I tested it.
A module is a module to the code, whether it be an Object module or a Standard module, it searches them all equally.
Thank you
Was looking for this. 4 stars.