Navigation buttons - custom errors

SteveC24

Registered User.
Local time
Today, 23:16
Joined
Feb 1, 2003
Messages
444
Hi,

I have now set up my navigation buttons for my forms, and have added them in and they are working great. I would like to know how I can make it so that if you are in record 1, when you click on "previous" it doesn't come up with the horrible access error message. Instead I would like my own message box to appear.

Thanks for any help you can give.
 
You could make it such that if you are on record 1 then the "Previous" button is disabled.

Col
:cool:
 
I do the disabling too


Private Sub Form_Current()
'Disable navigation buttons on end and beginning
Me.RecordsetClone.MoveLast
If (Me.RecordsetClone.RecordCount) < 1.5 Then 'There is only one record

cmdFirst.Enabled = True
cmdlast.Enabled = True
cmdPrevious.Enabled = False
cmdPreviousFast.Enabled = False
cmdNext.Enabled = False

ElseIf Me.RecordsetClone.RecordCount = Me.CurrentRecord Then 'Your at the last record
cmdPrevious.Enabled = True
cmdPreviousFast.Enabled = True
cmdNext.Enabled = False
cmdlast.Enabled = False
cmdFirst.Enabled = True

ElseIf Me.CurrentRecord = 1 Then 'Your at the first record
cmdPrevious.Enabled = False
cmdPreviousFast.Enabled = False
cmdFirst.Enabled = False
cmdNext.Enabled = True
cmdlast.Enabled = True

Else 'Your somewhere inbetween
cmdPrevious.Enabled = True
cmdPreviousFast.Enabled = True
cmdNext.Enabled = True
cmdFirst.Enabled = True
cmdlast.Enabled = True

End If

end sub
 
The clue was in the post:

The form's OnCurrent() event.
 
Thanks very much Chewy, that works really well, and looks great!

The only problem is, that when a form with that code in is opened, and it doesn't have any records, it comes up with an error.

I get:

----------------
Run Time Error '3021'

No Current Record.


END DEBUG HELP
-----------------------------------

When I click on Degug, it highlights the:

Me.RecordsetClone.MoveLast

line.

Please help!

Thanks very much for all your help so far, you lot are great!
 
chewy said:
If (Me.RecordsetClone.RecordCount) < 1.5 Then 'There is only one record

If Me.RecordsetClone.RecordCount = 1 Then
 
OK, that has worked, but if there are no records in the form, then the first and previous buttons are disabled, but the next and last buttons still do.

I do not need them to be there if there are no records, as I have a "new record" button.

Below is the code I am currently using:

-----------------------------------------------------------------------
Private Sub Form_Current()
'Disable navigation buttons on end and beginning
If Me.RecordsetClone.RecordCount = 1 Then 'There is only one record

cmdfirst.Enabled = False
cmdlast.Enabled = False
cmdPrevious.Enabled = False
cmdNext.Enabled = False

ElseIf Me.RecordsetClone.RecordCount = Me.CurrentRecord Then 'You're at the last record
cmdPrevious.Enabled = True
cmdNext.Enabled = False
cmdlast.Enabled = False
cmdfirst.Enabled = True

ElseIf Me.CurrentRecord = 1 Then 'You're at the first record
cmdPrevious.Enabled = False
cmdfirst.Enabled = False
cmdNext.Enabled = True
cmdlast.Enabled = True

Else 'Your somewhere in between
cmdPrevious.Enabled = True
cmdNext.Enabled = True
cmdfirst.Enabled = True
cmdlast.Enabled = True

End If

End Sub
-------------------------------------------------------------------------

Thanks very much,
 
I prefer to use DoCmd.GoToRecord , , acNext etc on the button click events, you don't get Error messages then.
Else If (RecordsetClone.RecordCount) = 0
cmdPrevious.Enabled = False
cmdfirst.Enabled = False
cmdNext.Enabled = False
cmdlast.Enabled = False
 
This is the code I use; I find it more efficient...

Code:
Private Sub Form_Current()    
    Dim recClone As Recordset
    Dim intNewRecord As Integer
    
    ' Make a clone of the recordset underlying the form so we can move
    ' around that without affecting the form's recordset
    Set recClone = Me.RecordsetClone
    
    ' If this is a new record then disable the <NEXT> button and enable
    ' the <PREV> button and then exit the procedure
    intNewRecord = IsNull(Me.[YourFieldHere])
    
    If intNewRecord Then
        cmdPrevious.Enabled = True
        cmdNext.Enabled = False
        cmdLast.Enabled = False
        Exit Sub
    End If
    
    ' If we reach here, we know we are not in a new record so need to
    ' see if there are no records. If this is the case then we must
    ' disable both the <PREV> and <NEXT> buttons.
    
    If recClone.RecordCount = 0 Then
        cmdPrevious.Enabled = False
        cmdNext.Enabled = False
        cmdFirst.Enabled = False
        cmdLast.Enabled = False
    Else
    
    ' Synchronise the current pointer in the two recordsets
    recClone.Bookmark = Me.Bookmark
    
    ' If there are records, see if we are on the first record.
    ' If so, we should disable the <PREV> buttons.
    
    recClone.MovePrevious
    cmdPrevious.Enabled = Not (recClone.BOF)
    cmdFirst.Enabled = Not (recClone.BOF)
    recClone.MoveNext
    
    ' And then check whether we are on the last record.
    ' If so, we should disable the <NEXT> buttons.
    
    recClone.MoveNext
    cmdNext.Enabled = Not (recClone.EOF)
    cmdLast.Enabled = Not (recClone.EOF)
    recClone.MovePrevious
    
    End If
    
    ' Finally, we close the cloned recordset
    recClone.Close

End Sub
 
In that code, Mile-o-phile, what should I put in the:

(Me.[YourFieldHere])

What field is this?

Thanks,
 
Any field in your recordset, preferably your primary key.
 
Now I get this error:

---------------
Run-time error '13':

Type mismatch

------------

When I press the DEBUG button, it highlights:

Set recClone = Me.RecordsetClone

HELP!
 
What version of Access are you using? That code works in '97 although I believe 2000 and 2002 work slightly different in creating a clone.
 
I have looked in Help, and it has this:


Creates a duplicate Recordset object that refers to the original Recordset object.

Syntax

Set duplicate = original.Clone

The Clone method syntax has these parts.


duplicate = An object variable identifying the duplicate Recordset object you're creating.
original = An object variable identifying the Recordset object you want to duplicate.

I hope you know what that is on about!
 
try fixing this line:

Set rsClone = Me.RecordSetClone

to this:

Set rsClone = Me.RecordSet.Clone
 
Right, that is that sorted, thanks!

BUT, naturally, because this is me, it has now got another problem. When I open the form, it comes up with:

---------------------
Run-time error '91'

Object variable or With block variable not set
-------------------------

When clicking on DEBUG, it highlights:

If recClone.RecordCount = 0 Then

Thanks!
 
HAHAHAHHAA!!!!!!!

I am SOOO chuffed with myself, I looked through the code, and discovered the recClone that was throughout the code needed to be changed to rsClone.

With that, it works!

It is probably a VERY small achievement in most people's books, but I am WELL chuffed.

Thanks SO much for all your help Mile-o-Phile - you've been grand!
 

Users who are viewing this thread

Back
Top Bottom