Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...What a great service! This is the best site I've ever seen!!! It totally restores my faith in humanity when people take time out to help other people..."

Geography

Where in the world do Tek-Tips members come from?

Multiple Selection (shift-select) Checkboxes

Rzrbkpk (TechnicalUser)
8 Dec 11 9:36
I have a table that populates in a subform with the following fields:

Filepath (memo)
File (text)
Select (True/False - Checkbox)

What I'm trying to accomplish is to allow the user to multi-select checkboxes without having to click (or spacebar) on each record. So that if there are 30 records, they could click on record 1 checkbox, hold down shift and click on record 15 checkbox and all checkboxes between would be selected/checked. I've seen this function in other programs, but I'm not sure if it's a Windows API thing or just custom code. Any help would be appreciated.
MajP (TechnicalUser)
8 Dec 11 10:36
This works, but is a little primitive.  If I had time I would write it so it could be used anywhere.  It is hardwired to the form, and you would have to write code to make this available to all your forms.

put in a standardmodule

CODE

Sub CreateSimpleShortcutMenu()
    Dim cmbShortcutMenu As Office.CommandBar
    'need reference Microsoft Office 1X.0 Object Library
    ' Create a shortcut menu named "SimpleShortcutMenu".
    Dim cmdBar As CommandBar
    Dim newcontrol As CommandBarControl
    For Each cmdBar In Application.CommandBars
      If cmdBar.Name = "SimpleShortcutMenu" Then Exit Sub
    Next cmdBar
    
    Set cmbShortcutMenu = CommandBars.Add("SimpleShortcutMenu", _
                        msoBarPopup, False, False)
                        
    ' Add your custom selection
    cmbShortcutMenu.Controls.Add Type:=msoControlButton, Id:=605

    
    Set newcontrol = cmbShortcutMenu.Controls.Add(Type:=msoControlButton)
    newcontrol.Caption = "Select All"
    newcontrol.OnAction = "SelectAll"
    
    Set newcontrol = cmbShortcutMenu.Controls.Add(Type:=msoControlButton)
    newcontrol.Caption = "UnSelect All"
    newcontrol.OnAction = "UnSelectAll"
    
    Set cmbShortcutMenu = Nothing
End Sub


Public Sub SelectAll()
  Dim rs As DAO.Recordset
  Dim frm As Access.Form
  Dim recordStart As Integer
  Dim recordEnd As Integer
  Dim I As Integer
  Const FormName = "frmProducts"
  Const SelectField = "Selected"
  Set frm = Forms(FormName)
  Set rs = frm.RecordsetClone
  recordStart = frm.SelTop - 1
  recordEnd = recordStart + frm.SelHeight - 1
  rs.MoveFirst
  For I = recordStart To recordEnd
    rs.AbsolutePosition = I
    'MsgBox rs!ProductName
    rs.Edit
    rs.Fields(SelectField) = True
    rs.Update
  Next I
  frm.Requery
  frm.Recordset.AbsolutePosition = recordStart
End Sub
Public Sub UnSelectAll()
    Dim rs As DAO.Recordset
  Dim frm As Access.Form
  Dim recordStart As Integer
  Dim recordEnd As Integer
  Dim I As Integer
  Const FormName = "frmProducts"
  Const SelectField = "Selected"
  Set frm = Forms(FormName)
  Set rs = frm.RecordsetClone
  recordStart = frm.SelTop - 1
  recordEnd = recordStart + frm.SelHeight - 1
  rs.MoveFirst
  For I = recordStart To recordEnd
    rs.AbsolutePosition = I
    'MsgBox rs!ProductName
    rs.Edit
    rs.Fields(SelectField) = False
    rs.Update
  Next I
  frm.Requery
  frm.Recordset.AbsolutePosition = recordStart
End Sub
put in the form

CODE

Private Sub Form_Load()
 CreateSimpleShortcutMenu
 Me.ShortcutMenuBar = "SimpleShortcutMenu"
End Sub

Now you can select the records and right click to select/deselect
MajP (TechnicalUser)
8 Dec 11 10:41
You have to click on the record selector on the right side of the form in order to select multiple records.  This would be a lot more complicated to allow the user to click a first and last checkbox.

Rzrbkpk (TechnicalUser)
8 Dec 11 16:06
Thank you for your response MajP. I modified the necessary items to match my forms/fields, but I'm getting a run-time error '3265' (Item not found in this collection). The piece of code giving the error is:

    rs.Fields(SelectFields) = True

I'm not sure what to do with this.
MajP (TechnicalUser)
8 Dec 11 16:31
Not sure if that is a typo.

The constant is the name of the field that gets selected. Mine is called "Selected" which is a poor name. You need to modify the value of the constant if your field has a diff name.

Const SelectField = "Selected"
....
rs.Fields(SelectField)

in your code you show an "s"
 rs.Fields(SelectFields)

which is probably a typo.
The real problem is that you need to use your field name here.
Const SelectField = "Selected"
my guess it is something different.
 
Rzrbkpk (TechnicalUser)
8 Dec 11 16:43
D'oh! I accidentally added that "s" when I was modifying the code. It works perfectly now! THANK YOU!!!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close