Selected Records in Subform

gwkenny

Well-known Member
Joined
Aug 13, 2002
Messages
565
I know how to get selected records in my main form, what I would like to know if there is a way to get the selected records in the subform?

I can't seem to find anything that will work.

Me.ListData_Subform.SelTop doesn't work. Me.ListData_Subform.CurrentRecord doesn't work.

I forgot to mention that the subform is in datasheet view.

I can access the controls, so I can find out what the main record is, but if the User selected several records, I can't seem to find a way for me to find out what records the User selected using VB.

Thank in advance for any help you can provide!!!

g-
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
If it's a bound form, you should be able to access the fields of the selected record simply by referencing the fields directly.


Me.fieldname.value


Best way to catch this is to reference a unique keyfield, such as an autonumber primary key field.

Mike
 
Upvote 0
Thanks for the response!!!

My problem is that I if the User selected several records, I can't seem to find a way for me to find out what records the User selected using VB.

I can access the controls, but when I do, that automatically shrinks a multi-record selection down to only one record.

I'd like to be able to identify all the records the User selected in the subform to perform operations on. Much like .Seltop and .Sellength properties of "main" form.

Pretty stumped on this. Might be able to cycle through all the records of the .InSelection property works, but that's a helluva lot of work.

Thanks for any ideas!!!

g-
 
Upvote 0
Never used .InSelection but I'm wondering why it would be alot of work?
You should look at For Each...Next loops
Gives you the ability to specify a container and loop through everything in it without having to specify each of them.

My favorite technique is to loop thru to get the names and use them elsewhere as needed.

Mike
 
Upvote 0
Seems like Microsoft didn't code in a lot of the properties in subforms as they have in "main" forms.

It would be a lot of work IF the inselection property works because the subform can have up to 90K records. A lot more when it is in use. To cycle through 90K+ records to find if two are selected would take some time and is probably impractical from a user perspective.

I should be used to this as I used to program VB across different office apps and different objects would have different properties (especially between PowerPoint and Word, yuck).

So, if anyone has any ideas on how to tell what records (multi-selection) are chosen on a subform via VB, I'm all ears :)

g-
 
Upvote 0
I can't take much credit for this... I found the solution after doing a google search. I've only modified it to work on a subform, the trick is that you have to capture the selTop and SelHeight values before you run the button click event.


'form declarations
Private mlngSelTop As Long
Private mlngSelheight As Long

Private Sub cmdSubFormSelected_Click()
Dim strX As String
Dim intI As Integer

' substitute my_subform with your subform name
With Me.my_subform.Form.RecordsetClone
.MoveFirst
.Move mlngSelTop - 1
intI = 1
Do Until intI > mlngSelheight
'building string of selected records
'substitute control_name with your field name
strX = strX & intI & ": " & !control_name & vbNewLine
.MoveNext
intI = intI + 1
Loop
End With
'Me.my_subform.Form.SelTop = mlngSelTop
'Me.my_subform.Form.SelHeight = mlngSelheight
MsgBox strX

End Sub

Private Sub my_subform_Exit(Cancel As Integer)
mlngSelTop = Me.my_subform.Form.SelTop
mlngSelheight = Me.my_subform.Form.SelHeight
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,691
Messages
6,161,309
Members
451,696
Latest member
Senthil Murugan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top