Solved

Row Set Focus

Posted on 2011-09-22
16
301 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
  • 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

920 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now