Solved DoCmd. GoToRecord does not work correctly

That is, the error is in the FName.Name. And it is logical, in the parent form there are no records, because it is independent.
 
Fixed up:

Code:
Function PersonalizarVista(FName As Form, Tabla As String, Optional HayCriterio As Boolean = False, Optional CampoCriterio As String, Optional Criterio As Variant)
    Dim Registros As Long
    On Error GoTo err_lbl
    If HayCriterio = False Then
        Registros = DCount("*", Tabla)
    Else
        Dim Criterio1 As String
        Criterio1 = CampoCriterio & " = " & Criterio
        Registros = DCount("*", Tabla, Criterio1)
    End If
    FName.Controls("OtrosRegistros").SetFocus
    DoCmd.RunCommand acCmdRecordsGoToLast
    Select Case Registros
        Case Is > 5
            If HayCriterio = True Then
                DoCmd.GoToRecord acActiveDataObject, , acPrevious, 3
            Else
                DoCmd.GoToRecord acDataForm, FName.Name, acPrevious, 3
            End If
        Case 2 To 5
            If HayCriterio = True Then
                DoCmd.GoToRecord acActiveDataObject, , acPrevious, 1
            Else
                DoCmd.GoToRecord acDataForm, FName.Name, acPrevious, 1
            End If
    End Select
    DoCmd.RunCommand acCmdRecordsGoToNew
err_lbl:
    Select Case Err.Number
        Case 0
        Case 3075
            MsgBox Err.Number & " " & Err.Description
            Exit Function
    End Select
End Function

First, I pass the focus to the subform, and then with acActiveDataObject I tell it that it is the active object that contains the record.

Thank you very much everyone for the help.
 
So what happens when you have no subform control "OtrosRegistros" ?
 
I pass a Boolean variable (HayCriterio) to the function, which tells me whether or not there is a subform. That's why you see the conditionals. And the option variables after HayCriterio are typical of forms with subforms.
 
Code:
Function PersonalizarVista(FName As Form, Tabla As String, Optional HayCriterio As Boolean = False, Optional CampoCriterio As String, Optional Criterio As Variant, Optional Subform As String)
    Dim Registros As Long
    On Error GoTo err_lbl
    If HayCriterio = False Then
        Registros = DCount("*", Tabla)
    Else
        Dim Criterio1 As String
        Criterio1 = CampoCriterio & " = " & Criterio
        Registros = DCount("*", Tabla, Criterio1)
        FName.Controls(Subform).SetFocus
    End If
    DoCmd.RunCommand acCmdRecordsGoToLast
    Select Case Registros
        Case Is > 5
            If HayCriterio = True Then
                DoCmd.GoToRecord acActiveDataObject, , acPrevious, 3
            Else
                DoCmd.GoToRecord acDataForm, FName.Name, acPrevious, 3
            End If
        Case 2 To 5
            If HayCriterio = True Then
                DoCmd.GoToRecord acActiveDataObject, , acPrevious, 1
            Else
                DoCmd.GoToRecord acDataForm, FName.Name, acPrevious, 1
            End If
    End Select
    DoCmd.RunCommand acCmdRecordsGoToNew
err_lbl:
    Select Case Err.Number
        Case 0
        Case 3075
            MsgBox Err.Number & " " & Err.Description
            Exit Function
    End Select
End Function

That is the final function, and working perfectly.
 
I pass a Boolean variable (HayCriterio) to the function, which tells me whether or not there is a subform. That's why you see the conditionals. And the option variables after HayCriterio are typical of forms with subforms.
Ok, but the setfocus happens regardless of any conditions.?
 
Nice solution to the problem. Glad you have it sorted out.
Good luck with the next stage!
 

Users who are viewing this thread

Back
Top Bottom