下面是一个函数,我们可以通过标签来确定与哪个控件相关联,使用的时候先将下面函数放到模块中。
'------------------------------------------------------------------------
' Procedure : GetLabelAssoCtrl
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Req'd Refs: Late Binding -> none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' oLbl : Label object to retrieve the associated control name of
' oFrm : Form object containing the label control
'
' Return:
' ~~~~~~~
' The function return the name of the associated control, and "" if it isn't associated
' with any control.
'
' Usage:
' ~~~~~~
' GetLabelAssoCtrl(me.lbl_FirstName, me)
' Returns -> txt_FirstName
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************
' 1 2023-03-31
'----------------------------------------------------------------
Function GetLabelAssoCtrl(oLbl As Access.Label, oFrm As Access.Form) As String
On Error GoTo Error_Handler
Dim sAssoCtrl As String
sAssoCtrl = oLbl.Parent.Name
If oFrm.Name = sAssoCtrl Then sAssoCtrl = ""
GetLabelAssoCtrl = sAssoCtrl
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: GetLabelAssoCtrl" & 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 Function