Row Set Focus

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.
APD TorontoAsked:
Who is Participating?
 
mbizupCommented:
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
 
mbizupCommented:
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
 
mbizupCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Jeffrey CoachmanMIS LiasonCommented:
<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
 
mbizupCommented:
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
 
APD TorontoAuthor Commented:
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
 
APD TorontoAuthor Commented:
FYI, When i break at line 17:

?lngLastRec
 16065

?rst.NoMatch
False
0
 
APD TorontoAuthor Commented:
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
 
mbizupCommented:
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
 
APD TorontoAuthor Commented:
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
 
mbizupCommented:
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
 
APD TorontoAuthor Commented:
the error is

?err.Description
You entered an expression that has no value.
0
 
mbizupCommented:
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
 
mbizupCommented:
I have to run out for a few hours, but will check back in tonight (US East Coast)
0
 
APD TorontoAuthor Commented:
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
 
mbizupCommented:
Glad that worked out :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.