PSLine2000Documentation/Forms/Util Select picker.md

3.8 KiB

Util Select picker

Analysis generated on: 4/1/2025 4:03:39 PM

Record Source

Controls

Control Name Reference
PartNumber PartNumber (from Queries/Util Select 0 Q1)
Flag Flag (from Queries/Util Select 0 Q1)

VBA Code

Option Compare Database

Private Sub cmdFindPart_Click()
On Error GoTo Err_cmdFindPart_Click


    s$ = txtSearch
    PartNumber.SetFocus
    DoCmd.FindRecord s$, acAnywhere, False, , True
    Flag = "1"
    txtSearch.SetFocus
Exit_cmdFindPart_Click:
    Exit Sub

Err_cmdFindPart_Click:
    MsgBox Err.Description
    Resume Exit_cmdFindPart_Click
    
End Sub

What it does

VBA Code Description: Find Record Command

Overview

This VBA code snippet is used to execute a "Find Record" command in an Access database application. It allows users to search for specific records based on user input and provides feedback through error handling and automatic focus shifting.

Step-by-Step Explanation

  1. Option Compare Database: This line specifies that the comparison of strings should be case-insensitive, which is useful when searching for partial matches.
  2. Private Sub cmdFindPart_Click(): This declares a private subroutine named cmdFindPart_Click, which will be triggered when a command button with the label "Find Part" is clicked.
  3. On Error GoTo Err_cmdFindPart_Click: This line sets up error handling, specifying that if an error occurs within the subroutine, control should jump to the labeled block of code (Err_cmdFindPart_Click).
  4. s$ = txtSearch: Retrieves the text from a search input field (txtSearch) and stores it in the variable s$.
  5. PartNumber.SetFocus: Automatically focuses on a form or control named "PartNumber".
  6. DoCmd.FindRecord s$, acAnywhere, False, , True: This line performs a find record operation in the current database table, searching for records that match the input string (s$) anywhere in the fields. The acAnywhere parameter indicates that the search should be performed anywhere in the field values. The other parameters (False, , , True) specify additional options, such as ignoring case and performing an exact match.
  7. Flag = "1": Sets a variable named Flag to the value "1". This variable can be used elsewhere in the code to indicate that the find record operation was successful.
  8. txtSearch.SetFocus: Automatically focuses on the search input field (txtSearch) after the find record operation completes.
  9. Exit_cmdFindPart_Click: Exits the subroutine, allowing the program to continue executing.

Error Handling Block

  1. Err_cmdFindPart_Click: If an error occurs within the subroutine (i.e., On Error GoTo Err_cmdFindPart_Click), this labeled block of code is executed.
  2. MsgBox Err.Description: Displays an error message box with a description of the error, retrieved from the Err.Description property.
  3. Resume Exit_cmdFindPart_Click: Resumes execution at the next line of code after the error handling block, specifically the Exit_cmdFindPart_Click label.

Best Practices and Security Considerations

  • This code assumes that the search input field (txtSearch) is a text control, and the "PartNumber" form or control is also accessible within the application.
  • The use of On Error GoTo Err_cmdFindPart_Click and Resume Exit_cmdFindPart_Click provides basic error handling, but may not be sufficient for all error scenarios. Additional error handling techniques, such as try-catch blocks, should be considered in more complex applications.
  • The code does not perform any input validation or sanitization on the user-provided search string. This could potentially lead to security issues or unexpected behavior if malicious data is entered into the search field.