Solved DoCmd. GoToRecord does not work correctly

zelarra821

Registered User.
Local time
Tomorrow, 00:39
Joined
Jan 14, 2019
Messages
847
Good Morning.

I'm trying to get when I open a record to go to a new record, but showing the three previous records, because otherwise it just shows me the new record.

I have tried with

Code:
Dim Registros As Variant
Registros = DCount("*", "MyTable")
DoCmd.GoToRecord , , acGoTo, Registros - 3
DoCmd.GoToRecord , , acNewRec

It works fine on all forms, except, coincidentally, the ones with eleven records, which shows me the new record.

Can anyone help me find what I am doing wrong?

Thank you!
 
You need to check the number of records, no point moving around otherwise.

If me Registros > 3 then ' You might want to adjust this for the number available on the form?
DoCmd.GoToRecord acActiveDataObject, , acGoTo, Registros - 3
end if
 
Thanks for answering.

I did not want to send the whole function, because I apply this in several forms, and to simplify the database code a bit.

Code:
Function PersonalizarVista(Tabla As String, Optional HayCriterio As Boolean = False, Optional CampoCriterio As String, Optional Criterio As Variant)
    Dim Registros As Variant
    On Error GoTo err_lbl
    If HayCriterio = False Then
        Registros = DCount("*", Tabla)
    Else
        Registros = DCount("*", Tabla, CampoCriterio & "=" & Criterio)
    End If
    Select Case Registros
        Case Is >= 6
            DoCmd.GoToRecord , , acGoTo, Registros - 3
        Case 2 To 5
            DoCmd.GoToRecord , , acGoTo, Registros - 1
        Case Is = 1, Is = 0
    End Select
    DoCmd.GoToRecord , , acNewRec
err_lbl:
    Select Case Err.Number
        Case 3075
            Exit Function
    End Select
End Function

But it doesn't work either.
 
I think your select case statement is incorrect and I would also dim your count as a long not a Variant.
A Dcount will always return a number.

I would also add Option Explicit to the top of ALL your code modules. It would highlight some errors

Code:
Dim Registros As Long

 Select Case Registros
        Case Is > 5
            DoCmd.GoToRecord , , acGoTo, Registros - 3
        Case 2 To 5
            DoCmd.GoToRecord , , acGoTo, Registros - 1

    End Select
 
can you use the Recordset:
Code:
    With Me.Recordset
        .MoveFirst
        Do Until .EOF
            Me.Bookmark = .Bookmark
            .MoveNext
        Loop
        .AddNew
    End With
 
I think your select case statement is incorrect and I would also dim your count as a long not a Variant.
A Dcount will always return a number.

I would also add Option Explicit to the top of ALL your code modules. It would highlight some errors

Code:
Dim Registros As Long

Select Case Registros
        Case Is > 5
            DoCmd.GoToRecord , , acGoTo, Registros - 3
        Case 2 To 5
            DoCmd.GoToRecord , , acGoTo, Registros - 1

    End Select
Let's see, if I have used Variant it has been because, according to Access help, it is the type of value that a DCount returns.

Anyway, I have made the changes you have proposed, and it still does not work.
 
can you use the Recordset:
Code:
    With Me.Recordset
        .MoveFirst
        Do Until .EOF
            Me.Bookmark = .Bookmark
            .MoveNext
        Loop
        .AddNew
    End With
Hi thanks. With that method, I "save" the first record, but not three above the new record.
 
adding Me.Painting = False to the code, minimized the "scrolling" effect:
Code:
    Me.Painting = False
    With Me.Recordset
        .MoveFirst
        Do Until .EOF
            Me.Bookmark = .Bookmark
            .MoveNext
        Loop
        .AddNew
    End With
    Me.Painting = True
 
what does that mean? plz explain.
I want you to show me the last three plus the new record. Look at the picture.

ScreenShot001.jpg


Also, that code can I use or adapt to use in a subform from a main form?
 
yes, you can use that on subform.
but need some modification:
Code:
Me![theSubformName].SetFocus
Me![theSubformName].Form.Painting = False
With Me![theSubformName].Form.Recordset
    .MoveFirst
    Do Until .EOF
        Me![theSubformName].Form.Bookmark = .Bookmark
        .MoveNext
    Loop
    .AddNew
End With
Me![theSubformName].Form.Painting = True
 
Awesome. And what about showing the last three and the new register? Is it possible?
 
What happens that makes your original method not work. An error or what does it display?

I have used something similar many times on a continuous form when adding new records and it works fine.
 
This does not have the new record, but this is all I used to get the last five?
Code:
Private Sub Form_Open(Cancel As Integer)
    Me.RecordSource = "qryEmails"
    Me.sfrmEmails.SourceObject = "cfrmEmails"
    Set Me.sfrmEmails.Form.Recordset = Me.Recordset
    DoCmd.RunCommand acCmdRecordsGoToLast
    DoCmd.GoToRecord acDataForm, Me.Name, acPrevious, 5 ' Needed for a continuous form as only last record shows.
    DoCmd.RunCommand acCmdRecordsGoToLast
    DoCmd.Maximize
End Sub
 
Option Explicit
I think your select case statement is incorrect and I would also dim your count as a long not a Variant.
A Dcount will always return a number.

I would also add Option Explicit to the top of ALL your code modules. It would highlight some errors

Code:
Dim Registros As Long

Select Case Registros
        Case Is > 5
            DoCmd.GoToRecord , , acGoTo, Registros - 3
        Case 2 To 5
            DoCmd.GoToRecord , , acGoTo, Registros - 1

    End Select
It does not give any error. I have only changed the one of the module where I have the code, because it is not affected by the code of another module. I have yet to change it in the other modules, because it did give me an error (not associated with this) and I have to see it slowly.
 
for test:
Code:
    Dim lngCount As Long
    SendKeys "{PGDN 10}", True
    DoEvents
    lngCount = Me.Recordset.RecordCount
    DoCmd.GoToRecord , , acGoTo, (lngCount - 2)
 
Code:
    Dim f As subform
    Dim lngCount As Long
    Set f = Me![theSubformName]
    f.SetFocus
    SendKeys "{PGDN 10}", True
    DoEvents
    lngCount = f.Form.Recordset.RecordCount
    DoCmd.GoToRecord , , acGoTo, (lngCount - 2)
    Set f = Nothing
 
Code:
    Dim f As subform
    Dim lngCount As Long
    Set f = Me![theSubformName]
    f.SetFocus
    SendKeys "{PGDN 10}", True
    DoEvents
    lngCount = f.Form.Recordset.RecordCount
    DoCmd.GoToRecord , , acGoTo, (lngCount - 2)
    Set f = Nothing
Thank you very much, but I wouldn't want to have to use sendkeys. I was using it in another database and I didn't like the behavior. I'll keep looking for something else.
 

Users who are viewing this thread

Back
Top Bottom