Disabling Command Buttons

KevW

Registered User.
Local time
Today, 10:50
Joined
Nov 11, 2005
Messages
41
I am trying to disable the next button on a form called frmCompany

In the forms Current event procedure I am putting the following code.

Private Sub Form_Current()

' If this form is on a new record, The Next button button should be disabled

'to do this to shift the focus first. If there is
' a value in CompnayID we must enable the button.

If Me.NewRecord = True Then
cmdPrevious.SetFocus
cmdNext.Enabled = False
Else
cmdNext.Enabled = True

End If


End Sub

But instead of the Next command button being disabled and shown as greyed the button is as normal and if I click it I get the following error displayed with the

Private Sub cmdNext_Click()
On Error GoTo cmdNext_Click_Err

DoCmd.GoToRecord , "", acNext


cmdNext_Click_Exit:
Exit Sub

cmdNext_Click_Err:
MsgBox Error$
Resume cmdNext_Click_Exit

End Sub

Where is the mistake in my code, the grey button should be greyed out and this is not the case.

Am I missing something really obvious
 
Searching the forum is a great way to discover and learn the answers to your Access programming questions.

There is more than one way to do what you want. Try this...

Code:
Private Sub bNext_Click()
On Error GoTo Err_bNext_Click

    If Me.Dirty Then
        MsgBox "Please Save This Record!" & vbCrLf & vbLf & "You can not advance from this record until you either 'Save' the changes made to this record or 'Undo' your changes.", vbExclamation, "Save Required"
    ElseIf Me.CurrentRecord < Me.RecordsetClone.RecordCount Then
        DoCmd.GoToRecord , , acNext
    End If

Exit_bNext_Click:
    Exit Sub

Err_bNext_Click:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_bNext_Click
    
End Sub
 
Yes, GHudson's is a great idea.

bit I'm confused also, why your code is not working?
I can only assume, you showed part of your code, and there's something in the previous logic, that's throwing it off?
I have something similiar, I use regularly for disabling my own Navigation Buttons,

Private Sub Form_Current()
On Error GoTo xxx

Dim rec As DAO.Recordset

Set rec = Me.RecordsetClone()
If Form.NewRecord <> True Then
rec.Bookmark = Me.Bookmark
End If

Me.txtTotalRecords = rec.RecordCount 'these show current & total records
Me.txtCurrentRecord = rec.AbsolutePosition + 1

'error if new record
If Me.NewRecord = False Then
cmdNext.Enabled = True
rec.MovePrevious
cmdPrevious.Enabled = Not (rec.BOF)
rec.MoveNext
rec.MoveNext
cmdNext.Enabled = Not (rec.EOF)
rec.MovePrevious
rec.Close
ElseIf Me.NewRecord = True Then
cmdFirst.SetFocus
cmdNext.Enabled = False
End If

rec.Close: Set rec = Nothing
xxx:
If Err.Number <> 3167 Then
Resume Next
End If
End Sub

Go it from VBA for Beginners, WROX publications
 
I simply use:

Me.RecordsetClone.MoveLast

Me.cmdPrevious.Enabled = Me.CurrentRecord > 1
Me.cmdNext.Enabled = Me.RecordsetClone.RecordCount > Me.CurrentRecord

In the On_Current event ???

Dave
 

Users who are viewing this thread

Back
Top Bottom