Solved

Row Set Focus

Posted on 2011-09-22
16
333 Views
Last Modified: 2012-05-12
Hello Experts.

I have a form with a continous subform.  the main form has a refresh button, which builds an SQL SELECT based on criteria and does mySubForm.Requery

Whenever refresh is hit, my client complains that his cursor "jumps" to the first row and wonders if it can remain on the same row.

This "jump" is obviously caused by .Requery, but I'm wondering is there a way that i can findout wwhich control had last focus and the recID, then set focus back after Requery?

For Example:

Private sub txt_GotFocus 'on subform

   mainFRM.txtRow = me.ID
   mainFRM.txtControl = "txt"

end sub

private sub cmdRefresh.'on main forn
 
  'build SQL
  mySubForm.RecordSet = strSQL
    mySubForm.Requry

   mySubForm.row.setfocus = me.txtRow  '???
  mySubForm.Controls(Me.txtControl).SetFocus

end sub

i guess, all i need is how can i choose the row (line marked by ???), and of course, if the above can be more sufficient

thank you.
0
Comment
Question by:APD_Toronto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
16 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36584679
Try using a bookmark:


private sub cmdRefresh.'on main form
    dim lngID as long
    dim rst as dao.recordset
    lngID = Me.MysubformControlName.Form.MyIDField  '<--*** you need to adjust this for your own subform and ID name
 
  'build SQL
  mySubForm.RecordSet = strSQL
    mySubForm.Requry

   'mySubForm.row.setfocus = me.txtRow  '???
 ' mySubForm.Controls(Me.txtControl).SetFocus

    Set rst = Me.MysubformControlName.Form.RecordsetClone  '<-- adjust with your own subform name
    rst.FindFirst "IDField = " & lngID       '<--- replace IDField with your actual ID field name
    If rst.NoMatch Then
        MsgBox "No entry found.", vbInformation
    Else
        Me.MysubformControlName.Form.RecordsetClone.Bookmark = rst.Bookmark  '<-- adjust with your own subform name

    End If


end sub

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36584685
That should return you to the record you were previously editing.  I can't guarantee that the UI effect will be perfect.  You can't stop the jump to the first record when requerying.  So this will effectively have a jump to the first record followed by a jump to the bookmarked record -- so there may be a flash/flicker associated with it.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36584917
<is there a way that i can findout wwhich control had last focus>

It is difficult to tell when you are running your requery code because there is no event listed there.
(Just be aware that if you run the code on a click event of the button, then the last control to have the focus might just be the button,...not necessarily the last "textbox" you were in...)

But in a general sense you could use something like this:

Dim strCtrlName As String
    strCtrlName = Screen.ActiveControl.Name
    Me.Requery
    'mbizup's code
    DoCmd.GoToControl strCtrlName


JeffCoachman

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 61

Expert Comment

by:mbizup
ID: 36585926
Learned here the other night that there is also a Screen.PreviousControl which gives you  the control that was in focus last.  That might also prove useful for something like this.
0
 

Author Comment

by:APD_Toronto
ID: 36587769
I implented the following, but i still must scroll trough my subform to get to the last viewed record


strSQL = strSQL & " ORDER BY tblReservations.fldPickupTime, tblLocations.fldLocation;"
  
    'Obtain last rec focus
        Dim lngLastRec As Long
        Dim rst As DAO.Recordset
        
        lngLastRec = CLng(Me.frmReservSF.Form.fldReservID)
    
    Me.frmReservSF.Form.RecordSource = strSQL
    strSQLReport = strSQL
    Call getCBOs
    
    'Go to bookmark
        Set rst = Me.frmReservSF.Form.RecordsetClone
        rst.FindFirst "fldReservID = " & lngLastRec
        
        If rst.NoMatch Then
            'Do Nothing
        Else
            Me.frmReservSF.Form.RecordsetClone.Bookmark = rst.Bookmark
    
        End If

        Set rst = Nothing

Open in new window

0
 

Author Comment

by:APD_Toronto
ID: 36587788
FYI, When i break at line 17:

?lngLastRec
 16065

?rst.NoMatch
False
0
 

Author Comment

by:APD_Toronto
ID: 36710555
Can any one help me wrap this up?

Also, at line 7 above, i get an error if there are 0 records.  How can i isolate this?

Thank you
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36710822
Sorry - I missed your earlier email notifs.

Try this:


strSQL = strSQL & " ORDER BY tblReservations.fldPickupTime, tblLocations.fldLocation;"
 
    'Obtain last rec focus
        Dim lngLastRec As Long
        Dim rst As DAO.Recordset
       
        lngLastRec = CLng(Me.frmReservSF.Form.fldReservID)
   
    Me.frmReservSF.Form.RecordSource = strSQL
    strSQLReport = strSQL
    Call getCBOs
   
    'Go to bookmark
        Set rst = Me.frmReservSF.Form.RecordsetClone
        if rst.Recordcount = 0 then
                  msgbox "No records"
                  exit sub
        end if
        rst.FindFirst "fldReservID = " & lngLastRec
       
        If rst.NoMatch Then
            'Do Nothing
        Else
            ' Changed this
            Me.frmReservSF.Form.Bookmark = rst.Bookmark
   
        End If

        Set rst = Nothing
0
 

Author Comment

by:APD_Toronto
ID: 36710937
No prob

can you tell me what you changed?

Also, if the subform has 0 records, i get an error at
lngLastRec = CLng(Me.frmReservSF.Form.fldReservID)

how do i check for the number of records?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36711361
I changed this, which was using the RecordsetClone (basically a copy of the records):

       << Me.frmReservSF.Form.RecordsetClone.Bookmark = rst.Bookmark >>

To this, which uses the forms actual records:

            Me.frmReservSF.Form.Bookmark = rst.Bookmark


Regarding the error, what is the actual error number, and what do you want to do if there are no records?
0
 

Author Comment

by:APD_Toronto
ID: 36711542
the error is

?err.Description
You entered an expression that has no value.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36711682
This *might* work for you...

I'm using NZ() to substitute a 0 for null if there are no records (I believe the ID would be Null in this case), and exiting gracefully if the record ID is null.

strSQL = strSQL & " ORDER BY tblReservations.fldPickupTime, tblLocations.fldLocation;"
 
    'Obtain last rec focus
        Dim lngLastRec As Long
        Dim rst As DAO.Recordset
       
        lngLastRec = CLngNZ((Me.frmReservSF.Form.fldReservID),0)  '<-- ** use NZ to handle Nulle
   
    Me.frmReservSF.Form.RecordSource = strSQL
    strSQLReport = strSQL
    Call getCBOs
   
if lngLastRec = 0 then Exit Sub   '<*** Exit gracefully if there are no records found
    'Go to bookmark
        Set rst = Me.frmReservSF.Form.RecordsetClone
        rst.FindFirst "fldReservID = " & lngLastRec
       
        If rst.NoMatch Then
            'Do Nothing
        Else
            Me.frmReservSF.Form.RecordsetClone.Bookmark = rst.Bookmark
   
        End If

        Set rst = Nothing
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 36711708
If that doesn't work, what I was asking for was the actual error number:

?err.Number

We could use special handling for that error to smooth things out.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36711715
I have to run out for a few hours, but will check back in tonight (US East Coast)
0
 

Author Comment

by:APD_Toronto
ID: 36712330
CLngNz didnt help, but thanks to your reminder, i did

on error resume next
        lngLastRec = CLng(Me.frmReservSF.Form.fldReservID)  '<-- ** use NZ to handle Nulle
   
    Me.frmReservSF.Form.RecordSource = strSQL
    strSQLReport = strSQL
    Call getCBOs
   
if err.number = 2427 then Exit Sub

thanks
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36713980
Glad that worked out :)
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question