Command Button Enable/Disable

S

silva

I am using conditions to enable and disable record selection command buttons
in a form. I'm doing this to run additional scripts when navigating through
records, which I can't do simply using the standard record selection bar at
the bottom of the form window. It was working perfectly at first and then I
suppose when I made a change somewhere it doesn't work properly when the form
is first opened. It is set to diable First/Previous Record buttons when at
the first record, disable Last/Next Record buttons at the last record, and
all buttons are enabled for all records in between. Currently, when the form
loads, it disables the Last/Next Record buttons and enables the
First/Previous Record buttons, the exact opposite of what it should do. I
still have the built in record selector activated, so I use it to go to the
next record. After I do that, then it works properly from that point on, so
long as I don't completely close out the form. Here is the code used for
disabling/enabling the buttons:

If Form.CurrentRecord > 1 And Form.CurrentRecord <
Form.RecordsetClone.RecordCount Then
Next_Record_Button.Enabled = True
Previous_Record_Button.Enabled = True
Last_Record_Button.Enabled = True
First_Record_Button.Enabled = True
ElseIf Form.CurrentRecord = Form.RecordsetClone.RecordCount Then
Next_Record_Button.Enabled = False
Previous_Record_Button.Enabled = True
Last_Record_Button.Enabled = False
First_Record_Button.Enabled = True
ElseIf Form.CurrentRecord = 1 Then
Next_Record_Button.Enabled = True
Previous_Record_Button.Enabled = False
Last_Record_Button.Enabled = True
First_Record_Button.Enabled = False
End If

If there is something wrong with the code, please let me know. If there may
be some other solution to my problem, well, yeah, could use the help.
 
B

Beetle

You could try something like this in the Current event of your form (insert
your own command button names)

Private Sub Form_Current()

Dim rst As DAO.Recordset, IntCount As Integer

Set rst = Me.RecordsetClone

rst.MoveLast

IntCount = rst.RecordCount

With Me
.cmdFirstRecord.Enabled = Me.CurrentRecord <> 1 And Not Me.NewRecord
.cmdPreviousRecord.Enabled = Me.CurrentRecord <> 1 And Not Me.NewRecord
.cmdNextRecord.Enabled = Me.CurrentRecord <> IntCount And Not Me.NewRecord
.cmdLastRecord.Enabled = Me.CurrentRecord <> IntCount And Not Me.NewRecord
End With

End Sub

The "And Not Me.NewRecord" is optional if you want to prevent a user from
trying to navigate while they are on a new record.

HTH
_________

Sean Bailey
 
S

silva

I'm sorry, I should have posted how it now looks. My code was previously in
the sub that you specified, so I had that right. Here's the code as it sits
now, completely functional:

Private Sub Form_Current()

Dim rst As DAO.Recordset, IntCount As Integer
Set rst = Me.RecordsetClone
rst.MoveLast
IntCount = rst.RecordCount

If Form.CurrentRecord > 1 And Form.CurrentRecord < IntCount Then
Next_Record_Button.Enabled = True
Previous_Record_Button.Enabled = True
Last_Record_Button.Enabled = True
First_Record_Button.Enabled = True
ElseIf Form.CurrentRecord = IntCount Then
Next_Record_Button.Enabled = False
Previous_Record_Button.Enabled = True
Last_Record_Button.Enabled = False
First_Record_Button.Enabled = True
ElseIf Form.CurrentRecord = 1 Then
Next_Record_Button.Enabled = True
Previous_Record_Button.Enabled = False
Last_Record_Button.Enabled = True
First_Record_Button.Enabled = False
End If

End Sub

When I tried that "with" loop you suggested, VBA didn't like it. So I made
these minor adjustments and it works great. I believe it's the code that does
the initialization. Again, thanks!

_______________________________________________________________
 

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

Top