473,520 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error with Forward & Backward Buttons on Access Form

121 New Member
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 13151
MMcCarthy
14,534 Recognized Expert Moderator MVP
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 Recognized Expert Specialist
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 Recognized Expert Expert
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
clickingwires
24 New Member
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 Recognized Expert Expert
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
11672
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. tCetecM1CUST (SQL Table that contains the Customer Information) tAccountingDetail (SQL Table that contains the information in the form) frmAccountingEntry...
1
1679
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 a state preventing it from being opened either locked. SQLSetConnectAttr failure of the pilot " (Nota : translated approximatively from the french...
1
2294
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. The error messages appear when runnign the apend query manually or when closing the form using X, but not when using command buttons. Does...
1
10469
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 trying to export to Excel using a command in an Access Form. RowID strFY AccountID CostElementWBS 1 2008 1 ...
12
3383
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 through the web page currently in view. This will work beautifully in the case I need to get back to page previously viewed on that site. I have a...
1
2074
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 and close bound control then open the next doc. Each time I open the doc an instance of winword is opened in the background, however, when I close...
16
11779
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 that most Windows programs have, so I'm suprised it's not easier to implement in Access/VBA. Through Google, I found two VB6 examples and one...
7
13144
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 only." I have a button on the form which opens the Excel worksheet, which then basically serves as the input form for the Access database. What I...
0
7206
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7444
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5749
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5127
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4790
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3284
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1652
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
510
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.