SetBt()
Do in MS Access?This function is designed to highlight the active button in a form-based interface and update a few related controls based on which button is clicked.
π§ Step-by-Step Breakdown:
Detect which button was clicked The function captures the name of the active control (the button the user just clicked) and extracts its last digit to figure out which button number it is—like B1, B2, ..., B7.
Safety checks It first checks if the user actually clicked something (i.e.,
ActiveControl
isn't empty) and that the extracted number is between 1 and 7.Reset all buttons' appearance It loops through all 7 buttons named
B1
toB7
and sets their background color to match a neutral color box calledBox0
. This effectively removes the highlight from all buttons.Highlight the clicked button It sets the background color of the active button to match
Box1
, which serves as a highlight color.Set focus and caption update Then it moves the focus to a corresponding control named
P1
toP7
, and updates a label (LB0
) to display the caption of the button that was clicked.
✅ Key Takeaways
Dynamic Highlighting: Only the button that was clicked gets visually highlighted, improving user experience.
Clean State Management: All buttons are reset to a neutral background before highlighting the selected one.
Smart Focus Handling: After interaction, focus shifts to a related control (
P1
–P7
) to streamline the user's next action.Real-Time Feedback: The label (
LB0
) updates instantly to reflect the selected button’s caption, providing clarity.Error-Resistant Logic: Built-in checks prevent errors if no control is active or if the button name doesn’t follow expected rules.
Dim i As Integer
' Ensure ActiveControl is not null before proceeding
If Not Me.ActiveControl Is Nothing Then
Ax = Val(Right(Me.ActiveControl.Name, 1)) ' Use .Name instead of direct reference
' Ensure Ax is within valid bounds
If Ax >= 1 And Ax <= 7 Then
' Loop through buttons and reset their BackColor
For i = 1 To 7
Me.Controls("B" & i).BackColor = Me.Box0.BackColor
Next
' Update ActiveControl properties safely
Me.ActiveControl.BackColor = Me.Box1.BackColor
Me.Controls("P" & Ax).SetFocus
Me.LB0.Caption = Me.Controls("B" & Ax).Caption
End If
End If
End Function
Comments
Post a Comment