Solved DoCmd. GoToRecord does not work correctly (1 Viewer)

zelarra821

Registered User.
Local time
Today, 12:29
Joined
Jan 14, 2019
Messages
809
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.
 

zelarra821

Registered User.
Local time
Today, 12:29
Joined
Jan 14, 2019
Messages
809
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:29
Joined
Sep 21, 2011
Messages
14,268
So what happens when you have no subform control "OtrosRegistros" ?
 

zelarra821

Registered User.
Local time
Today, 12:29
Joined
Jan 14, 2019
Messages
809
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.
 

zelarra821

Registered User.
Local time
Today, 12:29
Joined
Jan 14, 2019
Messages
809
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:29
Joined
Sep 21, 2011
Messages
14,268
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.?
 

Minty

AWF VIP
Local time
Today, 11:29
Joined
Jul 26, 2013
Messages
10,371
Nice solution to the problem. Glad you have it sorted out.
Good luck with the next stage!
 

Users who are viewing this thread

Top Bottom