Results 1 to 5 of 5
  1. #1
    mike_980 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    58

    My code is faulty for disabling command button

    I have a form which navigates through a record set, I have custom navigation buttons, one for "next record" one for "previous record". I have it set so that when the first or last record is reached, one of the buttons disables to prevent the error messages etc.



    The problem seems to be with the disabling of the next record button, sometimes it works perfectly but sometimes when I first open the form and click "next record" the button immediately disables itself making the form completely ususable.

    here is the section of code that fires when the button is clicked.

    Code:
    Private Sub Command8_Click()
    
    With Recordset
    If .AbsolutePosition = .RecordCount - 1 Then
    Command8.Enabled = False
    
    Else
    
    DoCmd.GoToRecord , , acNext
    
    End If
    
    
    If Crop_ID >= 1 Then
    
    Dim Crop_ID_select As String
    Me.Combo6.SetFocus
    Crop_ID_select = DLookup("Crop", "qry_combo_manage_destinations2")
    Me.Combo6 = Crop_ID_select
    
    Else
    Me.Combo6.Value = ""
    
    
    End If
    End With
    End Sub

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    It might be better if you would attach a copy of your database ---remove anything private/confidential first.

  3. #3
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    Instead of disabling the button, how about notifying the user ?
    Code:
    Private Sub Command29_Click()
    10       On Error GoTo Command29_Click_Error
    
    20    DoCmd.GoToRecord , , acPrevious
    
    30       On Error GoTo 0
    40       Exit Sub
    
    Command29_Click_Error:
    50    If Err.Number = 2105 Then
    60    MsgBox "This is the first record"
    70    Else
    80        MsgBox "Error Line: " & Erl & vbNewLine & "Error " & Err.Number & " (" & Err.description & ") in procedure Command29_Click of VBA Document Form_Form1"
    90    End If
    End Sub
    Code:
    Private Sub Command30_Click()
    10       On Error GoTo Command30_Click_Error
    
    20    DoCmd.GoToRecord , , acNext
    
    30       On Error GoTo 0
    40       Exit Sub
    
    Command30_Click_Error:
    50    If Err.Number = 2105 Then
    60    MsgBox "This is the last record"
    70    Else
    80        MsgBox "Error Line: " & Erl & vbNewLine & "Error " & Err.Number & " (" & Err.description & ") in procedure Command30_Click of VBA Document Form_Form1"
    90    End If
    End Sub

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    I think Access warns the user if DoCmd.GoToRecord does not find the record. You could modify an error trap to give a custom msg.

    Also, a recordsetclone and recordcount on the form's current event should work for conditional formatting of your controls.

  5. #5
    mike_980 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    58
    Thanks amrut! for some reason I never thought about that method! works perfectly thankyou!

    ItsMe- thanks for your input too, that would probably have worked too

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Quick VBA Command Button code help :)
    By noaccessguru in forum Forms
    Replies: 1
    Last Post: 10-01-2013, 08:35 PM
  2. Simple Code for a Command Button
    By dvgef2 in forum Reports
    Replies: 5
    Last Post: 06-21-2013, 05:04 PM
  3. Command Button VBA Code
    By nyymattingly23 in forum Programming
    Replies: 13
    Last Post: 01-04-2013, 03:08 PM
  4. Replies: 5
    Last Post: 08-06-2009, 11:47 PM
  5. Command button code
    By lfolger in forum Forms
    Replies: 3
    Last Post: 03-25-2008, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums