Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't find form reference in VBA code 2

Status
Not open for further replies.

larrydavid

Programmer
Jul 22, 2010
174
US
Hello,

I have a subform open which has a combobox. AfterUpdate() on this combo box, a modal popup form appears for user input. On the Form_Close() event, I am trying to set the values to controls on the subform:

Private Sub Form_Close()
If IsNull(Me.cboEntity) = True And IsNull(Me.cboLOC) = True And IsNull(Me.cboCostCenter) = True And IsNull(Me.cboExpCode) = True Then
MsgBox ("All fields are required!")
Else
Forms![frm_Test]!txtEntity = Me.cboEntity.Column(1)
Forms![frm_Test]!txtLOC = Me.cboLOC.Column(1)
Forms![frm_Test]!txtCostCenter = Me.cboCostCenter.Column(1)
Forms![frm_Test]!txtExpCode = Me.cboExpCode.Column(1)
DoCmd.Close
End If
End Sub

But I am getting this error when "X"ing out of the form:
Run-time error '2450'
Test Application can't find the form 'frmTest' referred to in a macro expressions of Visual Basic code.

Any help greatly appreciated as always.

Thanks,
Larry
 
The syntax is:
Forms![name of mainform]![name of subform control].Form![name of control] = ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PH. Actually, what I'm trying to do is:

Private Sub Form_Close()
Dim lngID As Object
Dim rst As DAO.Recordset

Set lngID = Me.ID
Set rst = Forms![frm_Test]![frm_SubTest].Form.RecordsetClone

If IsNull(Me.cboEntity) = True And IsNull(Me.cboLOC) = True And IsNull(Me.cboCostCenter) = True And IsNull(Me.cboExpCode) = True Then
MsgBox ("All fields are required!")
Else
'requery the subform
Forms![frm_Test]![frm_SubTest].Form.Requery

'now find the record you added/modified
rst.FindFirst "ID = " & lngID

If Not rst.NoMatch Then
'it was found. move to it.
Forms![frm_Test]![frm_SubTest].Form.Bookmark = rst.Bookmark
End If

rst.Close
Set rst = Nothing
'DoCmd.Close
End If
End Sub

But now, I get the same error but instead of saying it can't find the FORM now it can't find the FIELD referred to in my expression. But I want to do a RecordsetClone, so wouldn't that just be done on the form as a whole and not a particular control?

Thanks,
Larry

 
Which line of code raises the error ?

Anyway, I'm suspicious about lngID As Object ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This can be written a lot simpler.
Code:
Private Sub Form_Close()
  Dim frm as access.form
  Set Frm = Forms![frm_Test]![frm_SubTest].Form
 'I assume this should be OR. You want all fields to be required
 If IsNull(Me.cboEntity) OR IsNull(Me.cboLOC) OR IsNull(Me.cboCostCenter) OR IsNull(Me.cboExpCode) Then
    MsgBox ("All fields are required!")
 Else
   'requery the subform
   Frm.Requery 'not sure why you need this
   'now find the record you added/modified
   Frm.recordset.FindFirst "ID = " & me.lngID
End If
End Sub
 
Thank you both so much! You're suggestions helped greatly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top