Solved

Row Set Focus

Posted on 2011-09-22
16
317 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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