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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Is this a timing issue? 5
Code wont trigger 5
Enable custom navigation buttons 1
.BOF and .EOF are lying! 11
Set rst = dbs.OpenRecordset(strSql) 5
Validate form data before saving record? 3
BOF and EOF 2
SQL Server 9

Back
Top