Subform: Accessing last record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robtyketto
    New Member
    • Nov 2006
    • 108

    Subform: Accessing last record

    Greetings,

    I have a main form. Upon filling in a combo box it then displays a subform (based on a query that uses values in the combo box on where clause)

    THe subform has the paramater Data Entry = 'No' as I want users to see all existing records and all additions/deletions and edits.

    However I want to move to the last record to default, allowing the user to add a record without navigating themselves to the last record.

    A popular request, so found code (see below)

    Code:
    Private Sub cboDeliveryName_AfterUpdate()
    
      Me.qryStudentsResultsDeliverysubform1.Enabled = True
      Me.qryStudentsResultsDeliverysubform1.Visible = True
      Me.qryStudentsResultsDeliverysubform1.SetFocus
       
        With Me.qryStudentsResultsDeliverysubform1.Form.RecordsetClone
           If Not .BOF Then
                .MoveLast
                MsgBox "In here"
                Me.qryStudentsResultsDeliverysubform1.Form.Bookmark = .Bookmark
          End If
        End With
        
    End Sub
    When testing the subform is populated with 1 or more rows but the debug message is NEVER displayed. If i remove the IF condition it complains with message "3021 : No current record"

    I cant even print the current record when im in the subform to see whats happening.

    Can anyone please advice and assist?

    Thanks
    Rob
  • mshmyob
    Recognized Expert Contributor
    • Jan 2008
    • 903

    #2
    To jumpt to the last record in a subform after some event you could simplify your code by just doing this:

    [CODE=vb]
    Me.subformContr olName.SetFocus
    DoCmd.GoToRecor d , , acLast
    [/CODE]

    Note: Going to the last record is NOT the same as adding a new record.

    Originally posted by robtyketto
    Greetings,

    I have a main form. Upon filling in a combo box it then displays a subform (based on a query that uses values in the combo box on where clause)

    THe subform has the paramater Data Entry = 'No' as I want users to see all existing records and all additions/deletions and edits.

    However I want to move to the last record to default, allowing the user to add a record without navigating themselves to the last record.

    A popular request, so found code (see below)

    Code:
    Private Sub cboDeliveryName_AfterUpdate()
    
      Me.qryStudentsResultsDeliverysubform1.Enabled = True
      Me.qryStudentsResultsDeliverysubform1.Visible = True
      Me.qryStudentsResultsDeliverysubform1.SetFocus
       
        With Me.qryStudentsResultsDeliverysubform1.Form.RecordsetClone
           If Not .BOF Then
                .MoveLast
                MsgBox "In here"
                Me.qryStudentsResultsDeliverysubform1.Form.Bookmark = .Bookmark
          End If
        End With
        
    End Sub
    When testing the subform is populated with 1 or more rows but the debug message is NEVER displayed. If i remove the IF condition it complains with message "3021 : No current record"

    I cant even print the current record when im in the subform to see whats happening.

    Can anyone please advice and assist?

    Thanks
    Rob

    Comment

    • robtyketto
      New Member
      • Nov 2006
      • 108

      #3
      Thanks.

      Added DoCmd.GoToRecor d , , acLast to code.

      If the subform is populated with 1 ROW then the cursor sits in the new record.

      If there is more than 1 ROW then it sits in the first row.
      Very strange behaviour.

      Rob

      Comment

      • mshmyob
        Recognized Expert Contributor
        • Jan 2008
        • 903

        #4
        Show the whole code now.

        Originally posted by robtyketto
        Thanks.

        Added DoCmd.GoToRecor d , , acLast to code.

        If the subform is populated with 1 ROW then the cursor sits in the new record.

        If there is more than 1 ROW then it sits in the first row.
        Very strange behaviour.

        Rob

        Comment

        • robtyketto
          New Member
          • Nov 2006
          • 108

          #5
          Data entry is set to "No" on subform.

          Code from combo box on main form:-

          Code:
          Private Sub cboDeliveryName_AfterUpdate()
            
              Me.qryStudentsResultsDeliverysubform1.Enabled = True
              Me.qryStudentsResultsDeliverysubform1.Visible = True
              Me.qryStudentsResultsDeliverysubform1.SetFocus
               
              '-- Set Focus to the SubFormControl 1st
              Me.qryStudentsResultsDeliverysubform1.SetFocus
              '-- Now set Focus to the control on SubForm
              Me.qryStudentsResultsDeliverysubform1.Form!txtStudentId.SetFocus
              '-- And now move to the last record
              DoCmd.GoToRecord , , acLast
              
          End Sub

          Comment

          • mshmyob
            Recognized Expert Contributor
            • Jan 2008
            • 903

            #6
            Try moving to the last record and THEN set focus on your control (ie: switch your last 2 lines around). See if that changes anything.

            Originally posted by robtyketto
            Data entry is set to "No" on subform.

            Code from combo box on main form:-

            Code:
            Private Sub cboDeliveryName_AfterUpdate()
              
                Me.qryStudentsResultsDeliverysubform1.Enabled = True
                Me.qryStudentsResultsDeliverysubform1.Visible = True
                Me.qryStudentsResultsDeliverysubform1.SetFocus
                 
                '-- Set Focus to the SubFormControl 1st
                Me.qryStudentsResultsDeliverysubform1.SetFocus
                '-- Now set Focus to the control on SubForm
                Me.qryStudentsResultsDeliverysubform1.Form!txtStudentId.SetFocus
                '-- And now move to the last record
                DoCmd.GoToRecord , , acLast
                
            End Sub

            Comment

            • robtyketto
              New Member
              • Nov 2006
              • 108

              #7
              Same issues.

              I've even tried to add a combo box to allow the user to select if they wish to add or not using

              Forms!frmModule Results!qryStud entsResultsDeli very_subform1.D ataEntry = False

              Forms!frmModule Results!qryStud entsResultsDeli very_subform1.D ataEntry = True

              I cant even get that right!

              I will also use proper naming conventions for my form and not include spaces next time.Using access 2007.

              Comment

              • mshmyob
                Recognized Expert Contributor
                • Jan 2008
                • 903

                #8
                Can you upload your database file to me.

                Just click on edit/delete at the bottom of your last message and you can add an attachment as a ZIP file.

                Originally posted by robtyketto
                Same issues.

                I've even tried to add a combo box to allow the user to select if they wish to add or not using

                Forms!frmModule Results!qryStud entsResultsDeli very_subform1.D ataEntry = False

                Forms!frmModule Results!qryStud entsResultsDeli very_subform1.D ataEntry = True

                I cant even get that right!

                I will also use proper naming conventions for my form and not include spaces next time.Using access 2007.

                Comment

                Working...