Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480

    Navigating Records on unbound form with VBA

    I am facing an odd issue, I have a form that reads records from a query.. It loads the first record into the form, without issue.



    Code:
    Sub FormLoaded()
    Dim r As DAO.Recordset
    
    
    Set r = CurrentDb.OpenRecordset("Results") ' Query we want
    
    
     Forms("frmmainnew").lbladdUser1bad.Visible = False
      Forms("frmmainnew").lbladdUser1bad.Visible = False
    'load first Row into form
      Forms("frmmainnew").txtaddUser1.Value = r![f13] & " " & r![f14]
      Forms("frmmainnew").txtphone1.Value = r![F11]
      Forms("frmmainnew").txtRoles.Value = r![F15]
      Forms("frmmainnew").Text305.Value = r![F19]
      Forms("frmmainnew").Text308.Value = r![F16]
      Forms("frmmainnew").Text311.Value = r![F3]
      Forms("frmmainnew").Text299.Value = r![F41]
      Forms("frmmainnew").Text316.Value = r![F2]
      'Forms("frmmainnew").Text311.Value = Replace(r![F3], ",", "") & ";"
      'Forms("frmmainnew").Text311.Value = Replace(r![F3], ", ", ";")
      
     ' Forms("frmmainnew").Text311.Value = Replace(r![F3], ", ", ";")
      r.Close
      Set r = Nothing
    End Sub
    Now I have have 2 buttons at the bottom of the form, one for next record and another for previous record...


    Code for next record

    Code:
    Sub NextRecordbtn()
    Dim r As DAO.Recordset
    
    
    Set r = CurrentDb.OpenRecordset("Results") ' Query we want
    r.MoveNext
    
    
    If r.EOF = True Then
    r.MoveFirst
    End If
    
      Forms("frmmainnew").lbladdUser1bad.Visible = False
      Forms("frmmainnew").lbladdUser1bad.Visible = False
    'load first Row into form
      Forms("frmmainnew").txtaddUser1.Value = r![f13] & " " & r![f14]
      Forms("frmmainnew").txtphone1.Value = r![F11]
      Forms("frmmainnew").txtRoles.Value = r![F15]
      Forms("frmmainnew").Text305.Value = r![F19]
      Forms("frmmainnew").Text308.Value = r![F16]
      Forms("frmmainnew").Text311.Value = r![F3]
      Forms("frmmainnew").Text299.Value = r![F41]
      Forms("frmmainnew").Text316.Value = r![F2]
      r.Close
      Set r = Nothing
    End Sub
    The problem I am having is weird... I know that I have 3 records that result in the query named "Results", the next record button will bring the form from record 1, to record 2... however it will not move from the 2nd to the 3rd record...I am sure it is something super simple, but I cannot figure it out for the life of me.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is .EOF True when you are on the Last Record? Why are you using an Unbound form?

  3. #3
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    If I run this...

    r.MoveLast
    If r.EOF Then
    msgbox "last recrod"
    End If

    it does recognize the last record as EOF and is true. However, when using r.movenext it will never move to the last record.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The code you published says "If .EOF Then .MoveFirst End If"
    Again...why do you feel you need an Unbound Form. Is it just a test?

  5. #5
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    The code published does not ever get to the end of the file. It gets to the record before the end of file, or EOF -1 if you will. I don't really know why I'm using an unbound form, might not have even been worth mentioning.
    I guess to sum it up.. r.movenext will not move the the last record. I have debugged it, but the code runs thru without actually moving to the last record...I don't know what I'm doing wrong at this point.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    So you single stepped your NextRecordbtn() code?

  7. #7
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Yea, it went thru, but did not actually move to the next record.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Did it jump over this part?
    If r.EOF = True Then
    r.MoveFirst
    End If

  9. #9
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Yea, it jumps over it. does r.movenext not have the ability to move to the last record?

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I just verified that r.MoveNext is able to move to the Last Record. It bothers me that you are creating a NEW Recordset every time you invoke that function. When you create a Recordset it is set to the 1st record. Your NextRecordbtn() should just stay on the 1st record.

  11. #11
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    How do I correct that?

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    How about binding this form to the Query/Table? It will eliminate all of this code. Working with Unbound forms adds quite a bit of overhead for the programmer.

  13. #13
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I was thinking the same thing as RG, you're closing the dataset every time you retrieve a recordset so you have no reference in your data WHERE in the recordset you were 'before' so it doesn't know where to go 'next'. Is the data in your recordset sorted in a specific way? For instance if your table has a PK and you are ALWAYS sorting data based on that pk you can, instead of pulling from a recordset that has ALL your records pull from a dataset that has 1 record, that being the PK immediately prior to, or subsequent to the record you're currently viewing. But this would only work if you can reliably sort your data the same way every time.

  14. #14
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Another solution if you must use an unbound form is to open the Recordset into a Public variable and keep it open while the form is open and then close it when you close the form.

  15. #15
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    So the code below would not be used? I guess I don't understand why it will move to the next record, as long as the next record is not the last record.


    Code:
    Sub NextRecordbtn()
      Dim r As DAO.Recordset
    
    
    Set r = CurrentDb.OpenRecordset("Query")  ' Query we want
    
    
    
    
    If Not r.EOF Then
      r.MoveNext
    End If
    
    
    If r.EOF Then
      r.MoveFirst
    End If
    
    
      Forms("frmmainnew").lbladdUser1bad.Visible = False
      Forms("frmmainnew").lbladdUser1bad.Visible = False
    
    
      Forms("frmmainnew").txtaddUser1.Value = r![f13] & " " & r![f14]
      Forms("frmmainnew").txtphone1.Value = r![F11]
      Forms("frmmainnew").txtRoles.Value = r![F15]
      Forms("frmmainnew").Text305.Value = r![F19]
      Forms("frmmainnew").Text308.Value = r![F16]
      Forms("frmmainnew").Text311.Value = r![F3]
      Forms("frmmainnew").Text299.Value = r![F41]
      Forms("frmmainnew").Text316.Value = r![F2]
    r.Close
    Set r = Nothing
    End Sub

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Trouble selecting records from unbound box
    By premis in forum Access
    Replies: 6
    Last Post: 07-31-2012, 03:35 PM
  2. Unbound Form, but wish to save records...how?
    By LostInAccess in forum Forms
    Replies: 4
    Last Post: 07-18-2012, 09:55 AM
  3. Navigating from Form to SubForm
    By Gryphen957 in forum Forms
    Replies: 1
    Last Post: 11-19-2010, 10:08 AM
  4. No navigating through records??
    By cvansickle in forum Forms
    Replies: 2
    Last Post: 10-27-2010, 04:05 PM
  5. Help record navigating in forms
    By edo in forum Forms
    Replies: 0
    Last Post: 08-12-2008, 10:45 AM

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