473,378 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Error with Forward & Backward Buttons on Access Form

121 100+
I have a form where I put my own Back and Forward buttons on the form.
I used the codes:

Expand|Select|Wrap|Line Numbers
  1.     DoCmd.GoToRecord , , acNext
  2.     DoCmd.GoToRecord , , acPrevious
But when I get to the last record or the first record, I get an error.
Is there a way to detect if it is a t the last record? Or some other way to fix this.

I tired using the on the on error.... step but it messes with my other code for the button.
May 7 '07 #1
5 13134
MMcCarthy
14,534 Expert Mod 8TB
Try putting your own error message in ...
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNextRecord_Click()
  2. On Error GoTo Err_cmdNextRecord_Click
  3.  
  4.     DoCmd.GoToRecord , , acNext ' move to next record
  5.  
  6. Exit_cmdNextRecord_Click:
  7. Exit Sub
  8. Err_cmdNextRecord_Click:
  9.         MsgBox "This is the last record", , "Last Record"
  10.         Resume Exit_cmdNextRecord_Click
  11. End Sub
  12.  
  13. Private Sub cmdPreviousRecord_Click()
  14. On Error GoTo Err_cmdPreviousRecord_Click
  15.  
  16.     DoCmd.GoToRecord , , acPrevious ' move to previous record
  17.  
  18. Exit_cmdPreviousRecord_Click:
  19. Exit Sub
  20. Err_cmdPreviousRecord_Click:
  21.         MsgBox "There is no record prior to this", , "First Record"
  22.         Resume Exit_cmdPreviousRecord_Click
  23. End Sub
  24.  
Mary
May 7 '07 #2
missinglinq
3,532 Expert 2GB
Another approach I sometimes use is to "wrap" back around. If you're on the Last Record and hit Next it wraps around to the First Record. If you're on the First Record and hit Previous it wraps back around to the Last Record.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNextRecord_Click()
  2. On Error GoTo Err_cmdNextRecord_Click
  3.    DoCmd.GoToRecord , , acNext
  4. Exit_cmdNextRecord_Click:
  5.     Exit Sub
  6.  
  7. Err_cmdNextRecord_Click:
  8.     DoCmd.GoToRecord , , acFirst
  9. End Sub
  10.  
  11.  
  12. Private Sub cmdPreviousRecord_Click()
  13. On Error GoTo Err_cmdPreviousRecord_Click
  14.     DoCmd.GoToRecord , , acPrevious
  15. Exit_cmdPreviousRecord_Click:
  16.     Exit Sub
  17.  
  18. Err_cmdPreviousRecord_Click:
  19.     DoCmd.GoToRecord , , acLast    
  20. End Sub
May 7 '07 #3
ADezii
8,834 Expert 8TB
I have a form where I put my own Back and Forward buttons on the form.
I used the codes:

Expand|Select|Wrap|Line Numbers
  1.     DoCmd.GoToRecord , , acNext
  2.     DoCmd.GoToRecord , , acPrevious
But when I get to the last record or the first record, I get an error.
Is there a way to detect if it is a t the last record? Or some other way to fix this.

I tired using the on the on error.... step but it messes with my other code for the button.
The professional way to accomplish what you are requesting is by the creation of "Smart Navigation" Buttons which change their "Enabled State" depending on the Current Record Pointer. This system involves the creation of 5 Command Buttons, code in the Current Event() of the Form, and the RecordsetClone property to test potential moves. The User will never see an Error because of an inadvertant move, because it will never happen. A little bit of effort, and the close following of these instructions, will give your Form a truly professional look:
  1. Create a Command Button whose Caption = First, and whose Name is cmdFirst. Place this code in the Click() Event.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdFirst_Click()
    2.   DoCmd.GoToRecord , , acFirst
    3. End Sub
  2. Create a Command Button whose Caption = Previous, and whose Name is cmdPrevious. Place this code in the Click() Event.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdPrevious_Click()
    2.   DoCmd.GoToRecord , , acPrevious
    3. End Sub
  3. Create a Command Button whose Caption = Next, and whose Name is cmdNext. Place this code in the Click() Event.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdNext_Click()
    2.   DoCmd.GoToRecord , , acNext
    3. End Sub
  4. Create a Command Button whose Caption = Last, and whose Name is cmdLast. Place this code in the Click() Event.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdLast_Click()
    2.   DoCmd.GoToRecord , , acLast
    3. End Sub
  5. Create a Command Button whose Caption = Add Record, and whose Name is cmdAddNew. Place this code in the Click() Event.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdAddNew_Click()
    2.   DoCmd.GoToRecord , , acNewRec
    3. End Sub
  6. Place this code in the Current() Event of the Form, it is well documented so that you can get an idea as to what is goiong on. I even added code to modify the Form's Caption depending on which Record is current. This was basically to show how the AbsolutePosition and RecordCount properties of a Recordset work. If you have any questions at all, please feel free to ask. I realize that this may be a little confusing and I'm not really sure of your skill level.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Current()
    2. Dim recClone As Recordset, intNewRecord As Integer, Msg As String
    3.  
    4. 'Make a duplicate copy of the Form's Recordset Object using RecordsetClone().
    5. 'It will create a seperate copy of the Recordset so that you can navigate or
    6. 'Manipulate a Form's Records independently of the Form itself. Maneuvers will
    7. 'not be reflected in the Form's Recordset.
    8. Set recClone = Me.RecordsetClone()
    9.    'necessary for initial Record Count for Form Caption
    10.     recClone.MoveLast
    11.     recClone.MoveFirst
    12.  
    13. 'If this is a New Record then disable the <Next> and <New> Buttons and enable
    14. 'the others. Must Exit the Sub-Routine at this point!!!!!!!!!!
    15. 'intNewRecord = IsNull(Me![Name])      'Name is a required Field   OR
    16. intNewRecord = Me.NewRecord
    17.  
    18. If intNewRecord Then                'Zero Records - disable all Buttons
    19.   cmdFirst.Enabled = True
    20.   cmdNext.Enabled = False
    21.   cmdPrevious.Enabled = True
    22.   cmdFirst.Enabled = True
    23.   cmdAddNew.Enabled = False        'Not using in this Form
    24.     Exit Sub
    25. End If
    26.  
    27. 'If we reach this Point, we are not at a New record, so enable the <New Record> Button
    28. cmdAddNew.Enabled = True
    29.  
    30. 'If there are no Records, disable all Buttons except <New Record>
    31. If recClone.RecordCount = 0 Then
    32.   cmdFirst.Enabled = False
    33.   cmdNext.Enabled = False
    34.   cmdPrevious.Enabled = False
    35.   cmdFirst.Enabled = False
    36. Else    'Must synchronize the Current Pointer in both Recordsets. Obviously Records exist
    37.   recClone.Bookmark = Me.Bookmark   'Resync after each Record Navigation
    38.   Msg = "Injuries for Years 1993 to 2000   (Record " & Str$(recClone.AbsolutePosition + 1) & " of "
    39.   Msg = Msg & recClone.RecordCount & ")"
    40.     Me.Caption = Msg
    41.  
    42.   'There are Records, let's see if we're on the First Record. If we are, disable the
    43.   '<First> and <Previous> Buttons
    44.   recClone.MovePrevious
    45.   cmdFirst.Enabled = Not (recClone.BOF)
    46.   cmdPrevious.Enabled = Not (recClone.BOF)
    47.   recClone.MoveNext
    48.  
    49.   'See if we're on the <Last Record> and if we are, disable the <Last>
    50.   'and <Next> Buttons
    51.   recClone.MoveNext
    52.   cmdLast.Enabled = Not (recClone.EOF)
    53.   cmdNext.Enabled = Not (recClone.EOF)
    54.   recClone.MovePrevious
    55. End If
    56.  
    57. 'Don't forget to Close the Recordset
    58. recClone.Close
    59. End Sub
May 8 '07 #4
The only way that I could get this to work was by changing


Dim recClone As Recordset

to

Dim recClone as DAO.Recordset
Nov 9 '07 #5
ADezii
8,834 Expert 8TB
The only way that I could get this to work was by changing


Dim recClone As Recordset

to

Dim recClone as DAO.Recordset
That is because you probably have References to both the DAO and ADO Type Libraries. Thanks for bring this to light, since you should always make Explicit References to the Libraries as in:
Expand|Select|Wrap|Line Numbers
  1. Dim MyRS As DAO.Recordset
  2. Dim MyRS As ADODB.Recordset
Nov 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Josh Strickland | last post by:
I am attempting to create an Access database which uses forms to enter data. The issue I am having is returning the query results from the Stored Procedure back in to the Access Form. ...
1
by: Fred F. | last post by:
Here is my problem. From an ACCESS form, I run a C++ process reaching the same database in read/write mode via the ODBC driver. An error occurs: " The database has been placed by an unknown user in...
1
by: bjbounce2002 | last post by:
Hello, I am using forms with command buttons to close form or run action queries. The error messages such as "Null value in required field" or "duplicate value in primary key" are suppressed....
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
12
Dököll
by: Dököll | last post by:
Hello again! I loaded some web pages in an access form. For now, adding Next and Previous buttons work to get me to the next or previous web sites. Searching for a code that deals with paging...
1
by: nassersh | last post by:
I am using a bound control to embed word documents within an access form. I have created two buttons on the form One to embed aand display and one to close. I embed one doc and then save the doc...
16
by: John | last post by:
I am looking for VBA code that will work with Access 2003 to enable dragging and dropping a file/folder name from Windows XP Explorer into an Access form's text box. This is a common functionality...
7
by: kpresidente | last post by:
Hello all, I'm trying to transfer the value of a control on an Access form to an Excel worksheet using VBA. The Access form is a single form with all the controls disabled, so that data is "read...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.