Determine record in VB code

G

Gord

I'm new to database, so bear with me. I'm self teaching from a couple of
books.

I've got a subform control on a form. This subform has its recordsource set
to a query and displays these records in datasheet view. This query is based
on one table.

When the user clicks on a field/column in the datasheet, how does one detect
in VB which record has been selected? Determining the field is easy as there
are numerous events associated with the text box for that field that can be
coded to capture what's going on. I need to know somehow on which row/record
the user is accessing. I assume Access must create its own recordset when
displaying a query or opening a table in datasheet view. So I would think
that it is this recordset that I need to have access to.

Along the same lines, is it possible to detect (in code) which record a user
has clicked on when they are viewing a table in datasheet view? Basically
the same question as above, but with no form involved.

Hope this makes sense.

Thanks
 
K

Ken Snell \(MVP\)

If you have a primary key for each record (which you should), you can use
the form's Current event to read the value of the primary key:

Private Sub Form_Current()
Dim varPrimaryKeyValue As Variant
varPrimaryKeyValue = Me.PrimaryKeyField.Value
End Sub
 
G

Gord

Thanks Ken.

The 'On Current' suggestion got me in the right direction. I can get the
current record's bookmark to keep track of what record the user is on. Using
the VB help, I also discovered that the recordset I need to refer to is the
form's. i.e. 'form.recordset.whateverproperty'.

Thanks again.

Gord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top