Solved

Row Set Focus

Posted on 2011-09-22
16
288 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
Comment Utility
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
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
FYI, When i break at line 17:

?lngLastRec
 16065

?rst.NoMatch
False
0
 

Author Comment

by:APD_Toronto
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:APD_Toronto
Comment Utility
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
Comment Utility
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
Comment Utility
the error is

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

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
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
Comment Utility
I have to run out for a few hours, but will check back in tonight (US East Coast)
0
 

Author Comment

by:APD_Toronto
Comment Utility
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
Comment Utility
Glad that worked out :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views 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 Access…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

12 Experts available now in Live!

Get 1:1 Help Now