SurreyNick
Member
- Local time
- Today, 17:37
- Joined
- Feb 12, 2020
- Messages
- 127
This is related to my thread "School database: How to use controls and a macro to simplify student exam registrations"
I need a method of selecting some records in a continuous subform that I want to do something with – in this instance copy those selected records to another table along with user input data from a couple of unbound controls.
The ideal solution would be a checkbox (used to denote selection) but I want it to be a temporary local user input choice and not a permanent data choice i.e. I really don’t want to create a yes/no field in a table. The trouble is when I add an unbound checkbox to my subform and then check it every record in the subform gets checked i.e. it’s an “all or nothing” option and that’s no good.
I have scoured the internet and on one of the forums there was a discussion dating back to 2008 about using a function to maintain either a collection or array of selected elements PK's (whatever that means) and use that to bind to a checkbox.
Apparently a guy called Albert Kallal came up with a solution, described as:
“Multi Select Example.”
This little example shows you how to select multiple items from a form, and then send it to report. The nice feature of this example is that it does not use a actual field in the database to accomplish this.
I have not yet written this one up...but the code involved is VERY simple.
Access2000: click here for the code
I have attached an image of his form and copied his code below.
What I’d really like to know is can this be adapted to my purpose? I’m using Access 2010.
I will want to copy the selected field records [Students]![StudentID] from the subform to my Tests table and append to each of those records the user input data from the two unbound controls on the main form called [TestDate] and [ExamPaperID].
Multi Select Example Form
Multi Select Example Code
Option Compare Database
Option Explicit
Dim colCheckBox As New Collection
----------------------------------------------------------------------
Public Function IsChecked(vID As Variant) As Boolean
Dim lngID As Long
IsChecked = False
On Error GoTo exit1
lngID = colCheckBox(CStr(vID))
If lngID <> 0 Then
IsChecked = True
End If
exit1:
End Function
----------------------------------------------------------------------
Private Sub Command13_Click()
Debug.Print "contact = " & Me.ContactID
If IsChecked(Me.ContactID) = False Then
colCheckBox.Add CLng(Me.ContactID), CStr(Me.ContactID)
Else
colCheckBox.Remove (CStr(Me.ContactID))
End If
Me.Check11.Requery
End Sub
----------------------------------------------------------------------
Private Sub Command14_Click()
MsgBox "records selected = " & MySelected, vbInformation, "Multi Select example"
End Sub
----------------------------------------------------------------------
Private Function MySelected() As String
Dim i As Integer
For i = 1 To colCheckBox.Count
If MySelected <> "" Then
MySelected = MySelected & ","
End If
MySelected = MySelected & colCheckBox(i)
Next i
End Function
----------------------------------------------------------------------
Private Sub Command16_Click()
Dim strWhere As String
strWhere = MySelected
If strWhere <> "" Then
strWhere = "contactID in (" & strWhere & ")"
End If
DoCmd.OpenReport "Contacts1", acViewPreview, , strWhere
DoCmd.RunCommand acCmdZoom100
End Sub
----------------------------------------------------------------------
Private Sub Command17_Click()
Set colCheckBox = Nothing
Me.Check11.Requery
End Sub
I need a method of selecting some records in a continuous subform that I want to do something with – in this instance copy those selected records to another table along with user input data from a couple of unbound controls.
The ideal solution would be a checkbox (used to denote selection) but I want it to be a temporary local user input choice and not a permanent data choice i.e. I really don’t want to create a yes/no field in a table. The trouble is when I add an unbound checkbox to my subform and then check it every record in the subform gets checked i.e. it’s an “all or nothing” option and that’s no good.
I have scoured the internet and on one of the forums there was a discussion dating back to 2008 about using a function to maintain either a collection or array of selected elements PK's (whatever that means) and use that to bind to a checkbox.
Apparently a guy called Albert Kallal came up with a solution, described as:
“Multi Select Example.”
This little example shows you how to select multiple items from a form, and then send it to report. The nice feature of this example is that it does not use a actual field in the database to accomplish this.
I have not yet written this one up...but the code involved is VERY simple.
Access2000: click here for the code
I have attached an image of his form and copied his code below.
What I’d really like to know is can this be adapted to my purpose? I’m using Access 2010.
I will want to copy the selected field records [Students]![StudentID] from the subform to my Tests table and append to each of those records the user input data from the two unbound controls on the main form called [TestDate] and [ExamPaperID].
Multi Select Example Form
Multi Select Example Code
Option Compare Database
Option Explicit
Dim colCheckBox As New Collection
----------------------------------------------------------------------
Public Function IsChecked(vID As Variant) As Boolean
Dim lngID As Long
IsChecked = False
On Error GoTo exit1
lngID = colCheckBox(CStr(vID))
If lngID <> 0 Then
IsChecked = True
End If
exit1:
End Function
----------------------------------------------------------------------
Private Sub Command13_Click()
Debug.Print "contact = " & Me.ContactID
If IsChecked(Me.ContactID) = False Then
colCheckBox.Add CLng(Me.ContactID), CStr(Me.ContactID)
Else
colCheckBox.Remove (CStr(Me.ContactID))
End If
Me.Check11.Requery
End Sub
----------------------------------------------------------------------
Private Sub Command14_Click()
MsgBox "records selected = " & MySelected, vbInformation, "Multi Select example"
End Sub
----------------------------------------------------------------------
Private Function MySelected() As String
Dim i As Integer
For i = 1 To colCheckBox.Count
If MySelected <> "" Then
MySelected = MySelected & ","
End If
MySelected = MySelected & colCheckBox(i)
Next i
End Function
----------------------------------------------------------------------
Private Sub Command16_Click()
Dim strWhere As String
strWhere = MySelected
If strWhere <> "" Then
strWhere = "contactID in (" & strWhere & ")"
End If
DoCmd.OpenReport "Contacts1", acViewPreview, , strWhere
DoCmd.RunCommand acCmdZoom100
End Sub
----------------------------------------------------------------------
Private Sub Command17_Click()
Set colCheckBox = Nothing
Me.Check11.Requery
End Sub