Access中用全局变量CHOSEN_OPERATOR查找窗体记录集报错求助
Hey there, let's break down why your code isn't working and walk through the fixes step by step:
1. Fix the Recordset Initialization & FindFirst Usage
Your original code tries to reference Forms("form_list_of_columns").rstformat—but forms don't have a rstformat property out of the box. You need to properly grab the form's bound recordset first, and also specify the field name in your FindFirst condition (it can't just take the variable alone).
Here's the corrected version:
Dim rstFormat As DAO.Recordset ' Grab the form's bound recordset Set rstFormat = Forms("form_list_of_columns").Recordset With rstFormat ' Structure the condition to target the op_choice field .FindFirst "[op_choice] = '" & CHOSEN_OPERATOR & "'" ' Check if a match was found If .NoMatch Then MsgBox "No matching operator found!" Else ' Sync the form to the found record using bookmarks Forms("form_list_of_columns").Bookmark = .Bookmark End If End With ' Clean up the recordset object Set rstFormat = Nothing
Note: If CHOSEN_OPERATOR is a numeric value, remove the single quotes around it in the FindFirst string.
2. Correct DoCmd.FindRecord Implementation
The "object undefined" error here usually happens because you didn't set the focus correctly before running the find. You need to target the op_choice field explicitly:
' Activate the target form first Forms("form_list_of_columns").SetFocus ' Move focus to the op_choice field so FindRecord searches it Forms("form_list_of_columns").Controls("op_choice").SetFocus ' Run the find with proper parameters DoCmd.FindRecord CHOSEN_OPERATOR, , True, , True, acAll
The last parameter acAll ensures it searches the entire recordset, not just from the current position.
3. Quick Pre-Checks to Avoid Headaches
- Make sure you've referenced the DAO library: In the VBA Editor, go to Tools > References and check "Microsoft DAO x.x Object Library" (critical for Recordset operations).
- If your form is unbound, you'll need to open the underlying table/query directly as a Recordset instead of using the form's Recordset property.
- Double-check that
CHOSEN_OPERATORisn't empty or null—an empty value will cause the find to fail silently.
内容的提问来源于stack exchange,提问作者isabelle juffermans

