Not recognizing sub form control name (1 Viewer)

jwal

Registered User.
Local time
Today, 14:14
Joined
Dec 1, 2015
Messages
13
I have a main form with a subform. When I click on a command button located on the subform I get an Error 2450: cannot find the referenced form. The name of the subform is subfrmToolReassignment_Grouped, the source object is subfrmToolReassignment_Grouped and the name is subfrmToolReassignment_Grouped. What am I missing? :confused:
 

JHB

Have been here a while
Local time
Today, 19:14
Joined
Jun 17, 2012
Messages
7,732
The sub forms control name in the main form.
 

jwal

Registered User.
Local time
Today, 14:14
Joined
Dec 1, 2015
Messages
13
The control name is the same.
 

MarkK

bit cruncher
Local time
Today, 11:14
Joined
Mar 17, 2004
Messages
8,178
If you have code that causes an error, it is almost impossible to troubleshoot without seeing the code.
 

jwal

Registered User.
Local time
Today, 14:14
Joined
Dec 1, 2015
Messages
13
Main form On Open
Code:
Private Sub Form_Open(Cancel As Integer)
Forms!frmToolReassignment_Combined!subfrmToolReassignment_Grouped.Form.cmdReassign.SetFocus
Code:


Subform Command Button

Code:
Public Sub cmdReassign_Click()
On Error GoTo ErrHandler
Dim strOpenArgs As String

strOpenArgs = Me.txtToolGroupID & "," & Me.txtEmployee_Name
DoCmd.OpenForm "popfrmReassignGroupedTools", , , "ToolGroupID=" & Me.txtToolGroupID, , acWindowNormal, strOpenArgs​
Code:
 

MarkK

bit cruncher
Local time
Today, 11:14
Joined
Mar 17, 2004
Messages
8,178
What happens in the error handler? What happens in the form "popfrmReassignGroupedTools" when it tries to open? What line causes the error?

The less information we have, the less we can help.
 

jwal

Registered User.
Local time
Today, 14:14
Joined
Dec 1, 2015
Messages
13
Thanks.

SubForm

Code:
Public Sub cmdReassign_Click()
On Error GoTo ErrHandler
Dim strOpenArgs As String

    strOpenArgs = Me.txtToolGroupID & "," & Me.txtEmployee_Name
    DoCmd.OpenForm "popfrmReassignGroupedTools", , , "ToolGroupID=" & Me.txtToolGroupID, , acWindowNormal, strOpenArgs
        
ExitHere:
    Exit Sub

ErrHandler:
    MsgBox Err.Number & ": " & Err.Description, vbExclamation
    Resume ExitHere
    
End Sub

popfrmReassignGroupedTools
Code:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo ErrHandler
       
    If Not IsNull(Me.OpenArgs) Then
        
        Me.txtToolCategoryQty = Forms!subfrmToolReassignment_Grouped.txtToolCategoryQty
        Me.txtToolLocation = Forms!subfrmToolReassignment_Grouped.[txtEmployee Name]
        
    End If

ExitHere:
    Exit Sub

ErrHandler:
    MsgBox Err.Number & ": " & Err.Description, vbExclamation
    Resume ExitHere
    
End Sub
 

MarkK

bit cruncher
Local time
Today, 11:14
Joined
Mar 17, 2004
Messages
8,178
So, looking at the code, it seems you are passing data to the newly opening form using OpenArgs AND you are trying to reference the calling form for the SAME data.

What I would do is only use OpenArgs, and don't use the form!subform.form!controlname references, which are easily subject to change and which make the new form totally dependent on the first form, etc...

Consider code like...
Code:
Public Sub cmdReassign_Click()
On Error GoTo ErrHandler
    Dim strOpenArgs As String

    strOpenArgs = Me.txtToolGroupID & "," & Me.txtEmployee_Name & "," & Me.txtTooCategoryQty
    DoCmd.OpenForm "popfrmReassignGroupedTools", , , "ToolGroupID=" & Me.txtToolGroupID, , acWindowNormal, strOpenArgs
        
ExitHere:
    Exit Sub

ErrHandler:
    MsgBox Err.Number & ": " & Err.Description, vbExclamation
    Resume ExitHere
    
End Sub
See how I've added an item to the strOpenArgs?

And changing the other block, we do...
Code:
Private Sub Form_Open(Cancel As Integer)
[COLOR="Green"]    'removed error handling for brevity's sake[/COLOR]
    dim vArgs
    If Not IsNull(Me.OpenArgs) Then
        vArgs = Split(me.OpenArgs, ",")
        
        Me.txtToolCategoryQty = vArgs(2)
        Me.txtToolLocation = vArgs(1)
    Else
        MsgBox "This form requires structured OpenArgs to function correctly!", vbCritical, "See Form_Open() for more info!"
        DoCmd.Close acForm, Me.Name
    End If
End Sub
See how we only use OpenArgs here? No reliance on specifically named form!subform.form!controlname references. In this way, other consumers might open this form also, if they provide OpenArgs in the proper format. And it solves your reference problem by simply not doing it that way.

hth
Mark
 

MarkK

bit cruncher
Local time
Today, 11:14
Joined
Mar 17, 2004
Messages
8,178
Also, see how posting all your code make all the difference solving the problem? :)
 

jwal

Registered User.
Local time
Today, 14:14
Joined
Dec 1, 2015
Messages
13
Works perfectly except now my "OK" cmd on popfrmReassignGroupedTools doesn't work. It had previously worked. Could you look at that code for me? When I click "OK" nothing happens. The bolded portion is where the code stops running and it exits.

Code:
Private Sub cmdOK_Click()
On Error GoTo ErrHandler
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim lngEmpID As Long
Dim intQty  As Integer
Dim strSQL As String
Dim strEmployee As String
Dim intToolGroupID As Integer
Dim intComma As Integer
Dim strOpenArgs As String
Dim i As Integer

    strOpenArgs = Me.OpenArgs
    
    intComma = InStr(strOpenArgs, ",")
    strEmployee = Mid(strOpenArgs, intComma + 1, 99)
    intToolGroupID = Left(strOpenArgs, (intComma - 1))
    
    If IsNull(Me.cboSelEmployee) Then
        GoTo ExitHere
    End If
    
    If IsNull(Me.txtQuantityAssign) Then
        GoTo ExitHere
    End If
    
    lngEmpID = Me.cboSelEmployee
    intQty = Me.txtQuantityAssign
        
    Set dbs = CurrentDb
    strSQL = "SELECT TOP " & intQty & " qryToolReassignment_GroupedExpanded.* " & _
        "FROM qryToolReassignment_GroupedExpanded " & _
        "WHERE ([ToolGroupID]=" & intToolGroupID & ") AND ([Employee Name]='" & strEmployee & "');"

    Set rst = dbs.OpenRecordset(strSQL)
    
[B]    If rst.EOF Then
        GoTo ExitHere
    End If[/B]
    
    For i = 0 To intQty - 1
        With rst
            .Edit
            .Fields("EmployeeID") = lngEmpID
            .Update
        End With
        rst.MoveNext
    Next
        
    DoCmd.Close acForm, Me.Name
    
    Forms("frmToolReassignment_Combined").Form!subfrmToolReassignment_Grouped.Requery
   '    Forms("subfrmToolReassignment_Grouped").Requery
 
ExitHere:
    On Error Resume Next
    Set rst = Nothing
    Set dbs = Nothing
    Exit Sub

ErrHandler:
    MsgBox Err.Number & ": " & Err.Description, vbExclamation
    Resume ExitHere
    
End Sub
 

Users who are viewing this thread

Top Bottom