Record Selectors - Events

dom_donald

Registered User.
Local time
Today, 12:22
Joined
Apr 30, 2004
Messages
22
Quite simply, I have a form with a sub-form showing data in a datasheet view. On the main form I have a button. On pressing this button I want to do two things

1) check that a row has been selected in the datasheet view
2) if a row has been selected, I wish to open a new form and populate the unbound text fields with data from the selected row, using VB.

Firstly, id' prefer to read the data from the selected row at the time the button is pressed (rather than store the data each time a record is selected).

For (1) i need a property to determine if a record has been selected. And now my main difficulty - I need to know the event that occurs when the row is selected by pressing the record selectors (and the selector is depressed and the row is highlighted). It's only under those conditions that I want to allow the user to open the other form based on the selected data. At the moment I can't find this specific method (and it's not OnCurrent because the row doesn't have to be highlighted for this to occur).

HELP :) cheers
 
The event that occurs when the row is selected by pressing the record selectors is the form's OnClick Event. In the code for the OnClick Event you need to compare the form's SelWidth Property to the form's RecordsetClone's field count. If the two values match, you can execute the chosen action. If they do not, simply exit the event.

Since, also, it is possible to highlight more than one line at a time, it can also be useful to know how to enumerate all selected lines.

The following sample code, pasted into your form, will respond to you clicking the record selector and highlighting the row(s) in your datasheet form, by popping up a MessageBox with the value from the first field for each selected record in the form's recordset:
Code:
Private Sub Form_Click()

    Dim rst As Recordset, idx As Long
    Set rst = Me.RecordsetClone
    If Me.SelWidth = rst.Fields.Count Then
        rst.MoveFirst
        rst.Move Me.SelTop - 1
        For idx = 1 To Me.SelHeight
          MsgBox rst.Fields(0)
          rst.MoveNext
        Next idx
    End If

End Sub

A more specific illustration, if you simply choose to pull back the first selected value, and the form has a field named CompanyName that you wish to enumerate:
Code:
Private Sub Form_Click()

    Dim rst As Recordset
    Set rst = Me.RecordsetClone
    If Me.SelWidth = rst.Fields.Count Then
        rst.MoveFirst
        rst.Move Me.SelTop - 1
        MsgBox rst!CompanyName
    End If

End Sub
 
Many thanks for the reply - I had a feeling there'd be a bit of faffing around :) i'll give it ago just as soon as my boss isn't looking... this is just a little side project you see :p
 
Bingo! Just what I wanted thanks.. as it turns out I don't want to allow multiple selections, but that's an easy tweak..

:)
 

Users who are viewing this thread

Back
Top Bottom