主窗体选作业号后子窗体双击预览报表报错求助
Hey there, let's tackle this compile error you're hitting. That "Method or Data Member Not Found" message usually means VBA can't locate the main form's job list box when your subform's double-click event runs. Here's how to fix it step by step:
1. Double-Check Control Names (Critical!)
First, make sure you're using the actual Name property of your main form's list box, not its display caption. For example:
- If your list box shows "作业号" as the visible text, its backend Name might be
lstJobNumbers(check the Property Sheet in Design View, under the "Other" tab). - Typos here are the #1 cause of this error—even a single wrong character will break the reference.
2. Use the Correct Parent Form Reference Syntax
From the subform, you need to explicitly reference the main form (its parent) to access the list box. You have two reliable options:
Option A: Use Me.Parent (Flexible, No Hardcoded Form Name)
This is the best approach because it works even if you rename the main form later:
Private Sub ReportName_DblClick(Cancel As Integer) Dim selectedJob As Variant ' Get the selected value from the main form's list box selectedJob = Me.Parent!lstJobNumbers.Value ' Validate we have a selection before previewing If Not IsNull(selectedJob) Then ' Replace with your report name and matching field condition DoCmd.OpenReport "rptJobDetails", acViewPreview, , "JobID = '" & selectedJob & "'" Else MsgBox "请先从主窗体选中一个作业号!" End If End Sub
Option B: Use Forms!MainFormName!ListBoxName (Hardcoded, Explicit)
If you prefer to spell out the main form name directly:
selectedJob = Forms!frmMain!lstJobNumbers.Value
Just replace frmMain with your actual main form's Name property.
3. Verify the List Box's Bound Column
Ensure the list box's Bound Column property matches the column that holds your job ID/name. For example:
- If your list box has two columns (JobID in column 1, JobName in column 2) and you want the JobID, set Bound Column to 1.
- The
Valueproperty of the list box will return the value from this bound column.
4. Debug to Pinpoint the Issue
If you're still stuck, add debug lines to test the reference:
Private Sub ReportName_DblClick(Cancel As Integer) ' Test if we can access the parent form Debug.Print "Parent Form Name: " & Me.Parent.Name ' Test if we can access the list box Debug.Print "List Box Name: " & Me.Parent!lstJobNumbers.Name ' Rest of your code... End Sub
Run the code, open the Immediate Window (Ctrl+G in VBA Editor), and check if these lines output valid names. If they throw the same error, your control name or form name is definitely wrong.
内容的提问来源于stack exchange,提问作者Reg97

