How to return to same record on datasheet

K

KARL DEWEY

I have a main form without record source and subform in datasheet view. I
store information in unbound textboxes on the main form from the datasheet
when I click a command button on the main form.

How do I get the cursor to go back to the same record of the datasheet when
I refresh everything?
If I could store the record counter then I could use GoToRecord n.

Thanks for your time.
 
6

'69 Camaro

Hi, Karl.
How do I get the cursor to go back to the same record of the datasheet when
I refresh everything?

I suspect that you are requerying, not refreshing, the Record Source,
because refreshing doesn't move the cursor on the subform. In any event, you
can use the subform's Bookmark property to get back to where you started from.

In the following example (admittedly a useless one, but it puts the cursor
back on the correct record on the subform after the requery), subContacts is
the name of the subform, txtContactPhone is the text box on the subform
(whose value is being copied to the text box, txtPhone, on the main form),
and txtID is the text box holding the primary key of the Record Source of the
subform.

' * * * * Start Code * * * *

Private Sub OKBtn_Click()

On Error GoTo ErrHandler

Dim recSet As DAO.Recordset
Dim nSaveID As Long
Dim fOpenedRecSet As Boolean

Me!txtPhone.Value = Me!subContacts.Form.txtContactPhone.Value
nSaveID = Me!subContacts.Form.txtID.Value ' Save the primary
key.
Me!subContacts.Form.Requery

Set recSet = Me!subContacts.Form.RecordsetClone
fOpenedRecSet = True

If (Not (recSet.BOF And recSet.EOF)) Then
recSet.FindFirst "ID = " & nSaveID

If (Not (recSet.NoMatch)) Then
Me!subContacts.Form.Bookmark = recSet.Bookmark
Else
MsgBox "Cannot find record matching " & vbCrLf & """ID = " & _
Me!txtID.Value & """", vbExclamation + vbOKOnly, _
"Record Not Found!"
End If
End If

CleanUp:

If (fOpenedRecSet) Then
recSet.Close
fOpenedRecSet = False
End If

Set recSet = Nothing

Exit Sub

ErrHandler:

MsgBox "Error in OKBtn_Click( ) in " & Me.Name & " form." & _
vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp

End Sub

' * * * * End Code * * * *

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)

- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Top