Avatar billede nyholm Novice
11. oktober 2005 - 12:21 Der er 12 kommentarer og
1 løsning

Stop if last record

In forms I have buttons for going to previous and next record.
When standing in the last record and clicking next, I do not want to go to new record. In the recordset there is the field Nr with fieldtype Count. I have tryed with:
If IsNull(DLookup("[Nr]", "Tablename", "[Nr] = Forms!Formname!Nr + 1")) Then
    Beep
    msg "This is the last record!", vbCritical, "Last record!"
    Exit Sub
    End If
but this only works if no records has been deleted. If standing in record 31 and record 32 has been deleted, the terms are met even if there are more records than 32.
What code can I use for my button to prevent going beyond the last record?
Please help!
Avatar billede inglenook Praktikant
11. oktober 2005 - 12:25 #1
rs.movefirst

do while not rs.eof
your code...
rs.movenext
loop
Avatar billede inglenook Praktikant
11. oktober 2005 - 12:28 #2
sorry....

Next button:
if not rs.eof then
rs.movenext
end if

Previous button:
if not rs.bof
rs.moveprevious
end if
Avatar billede nyholm Novice
11. oktober 2005 - 12:46 #3
Why can't I compile the code? I get the error "Compileringsfel, odefinierad variabel" and rs is highlited.
Avatar billede inglenook Praktikant
11. oktober 2005 - 12:47 #4
what do you call your recordset ?
Avatar billede inglenook Praktikant
11. oktober 2005 - 12:51 #5
rs should be replaced with the name of your recordset.....
Avatar billede nyholm Novice
11. oktober 2005 - 12:53 #6
??The name of the table is Bilar, which also is the name of the form.
Avatar billede inglenook Praktikant
11. oktober 2005 - 12:55 #7
can you show me the original code for each button ?
Avatar billede nyholm Novice
11. oktober 2005 - 12:57 #8
The original code for next is
DoCmd.GoToRecord , , acNext
Avatar billede inglenook Praktikant
11. oktober 2005 - 13:11 #9
hmm, try this:

Next button:
If not Me.RecordSet.EOF Then
DoCmd.GoToRecord , , acNext
end if

Previous button:
If not Me.RecordSet.BOF Then
DoCmd.GoToRecord , , acPrevious
end if
Avatar billede 2dbornot2db Nybegynder
11. oktober 2005 - 13:46 #10
Følgende plejer jeg at bruge

dim rs as DAO.Recordset

set rs = Me.RecordsetClone

Derefter kan du køre inglenooks kode:

Next button:
if not rs.eof then
rs.movenext
end if

Previous button:
if not rs.bof
rs.moveprevious
end if
Avatar billede mugs Novice
11. oktober 2005 - 14:53 #11
Er denne afprøvet:

Private Sub Form_Current()
If Me.NewRecord Then
DoCmd.GoToRecord acForm, "Grupper", acPrevious, 1
MsgBox "Du står på sidste post."
End If
End Sub
Avatar billede 2dbornot2db Nybegynder
11. oktober 2005 - 16:55 #12
Eller denne, jeg kalder på frm's onCurrent. Den kræver:
Knap frem, knap tilbage, knap gå-til-sidste, knap gå-til-første, label til visning af nummer på valgt post.

Dim strRecordCount As String


cmdNew.Enabled = Me.AllowAdditions

'Ved ingen poster
  If Me.RecordsetClone.RecordCount = 0 Then
    cmdFirst.Enabled = False
    cmdNext.Enabled = False
    cmdPrevious.Enabled = False
    cmdLast.Enabled = False
    cmdDelete.Enabled = True
    lblPostNr.Caption = "<Ingen data>"
    Exit Sub
ElseIf Me.NewRecord Then
    cmdFirst.Enabled = True
    cmdPrevious.Enabled = True
    cmdNext.Enabled = False
    cmdLast.Enabled = False
    cmdNew.Enabled = True
    cmdDelete.Enabled = True
    lblPostNr.Caption = "<Ny post>"
    lblPostNr.FontBold = False
    Exit Sub
 
Else
    Me.RecordsetClone.MoveLast
    Me.RecordsetClone.MoveFirst
    Me.RecordsetClone.Bookmark = Me.Bookmark
    cmdFirst.Enabled = True
    cmdPrevious.Enabled = True
    cmdNext.Enabled = True
    cmdLast.Enabled = True
    cmdNew.Enabled = True
    cmdDelete.Enabled = True
    cboPost.Enabled = True

    strRecordCount = Me.RecordsetClone.AbsolutePosition + 1 & " af " & Me.RecordsetClone.RecordCount
    lblPostNr.Caption = strRecordCount

    'Next, we must see if we are on the first record
    'If so, we should disable the <Previous> button
    Me.RecordsetClone.MovePrevious
    cmdFirst.Enabled = Not (Me.RecordsetClone.BOF)
    cmdPrevious.Enabled = Not (Me.RecordsetClone.BOF)
    Me.RecordsetClone.MoveNext
   
    'And then check whether we are on the last record
    'If so, we should disable the <Next> button
    Me.RecordsetClone.MoveNext
    cmdNext.Enabled = Not (Me.RecordsetClone.EOF)
    cmdLast.Enabled = Not (Me.RecordsetClone.EOF)
    Me.RecordsetClone.MovePrevious
end if
Avatar billede nyholm Novice
11. oktober 2005 - 19:47 #13
I thank You all for intrest in my question, but I want to keep things as simple as possible. After thinking it throu i come upp with the following solution:
    If DCount("Nr", "Table", "Nr >Forms!Formname!Nr") = 0 Then
    msg "Detta är sista posten", vbCritical, "Sista posten!"
    Else
        DoCmd.GoToRecord , , acNext
    End If
No record can have a higher nbr than the last record.
I tryed different ways to use EOF but for some reason it did'nt work.
Thanks again.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester