BOF EOF enabling cmd buttons

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hello All. I know this is a really simple question and it has probably been
answered 1000 times before.

I am trying to program some safeguards into my (navigation) command buttons
eg Not letting the user click next when there are no further records

I'm trying to use the code:

Dim Rst as RecordSource
Dim db as database
set DB to Current DB

IF rst.EOF then Me.cmdNext.enabled = False
Else me.cmdNext.enabled = True
End If

IF rst.BOF Then me.cmdPrevious.enabled = False
Else me.cmdPrevious.enabled = True
End IF

where am I going wrong?
 
It would help to know what problem you're having with that code...

1) Since you're using DAO, it's advisable to declare rst as:

Dim Rst As DAO.Recordset

2) To instantiate your database object, you need:

Set DB = CurrentDb()

3) Where are you instantiating rst? (you need an OpenRecordset statement
somewhere...)

4) Your syntax is wrong for your If statements:

IF rst.EOF then
Me.cmdNext.enabled = False
Else
me.cmdNext.enabled = True
End If

IF rst.BOF Then
me.cmdPrevious.enabled = False
Else
me.cmdPrevious.enabled = True
End IF

although the following looks cleaner to me:

Me.cmdNext.Enable = (rs.EOF = False)
Me.cmdPrevious.Enable = (rs.BOF = False)

or

Me.cmdNext.Enable = Not rs.EOF
Me.cmdPrevious.Enable = Not rs.BOF
 
It looks to me that you forgot to instantiate the Recordset. Perhaps, you
meant to have something like:

Set rst = Me.RecordsetClone

i.e. instantiate rst to be a copy of the Form's Recordset?



If you want to replace the built-in Navigation Buttons with your cmdPrevious
and cmdNext, you can use the Form_Current Event with code like:

Me.cmdPrevious.Enabled = (Me.CurrentRecord >1)
Me.cmdNext.Enabled = (Me.CurrentRecord < Me.RecordsetClone.RecordCount)
 
Problem - The next button will go to the next record but will not change the
enabled property to enabled=false when the rst.EOF.

Code

Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb()

Set rst = db.OpenRecordset("tblheritableProperty")
DoCmd.GoToRecord , , acNext
Me.cmdNextRecord.Enabled = Not rst.EOF

I've put this code in the onClick event of the cmdnext button and I've also
tried it on the onCurrent event of the form.

Please help me.
 
Back
Top