Thingol
Technical User
- Jan 2, 2002
- 169
Hi All,
I have a database in which I have a form containing a listbox. When a user selects some rows in the listbox and then hits a commandbutton, he should be taken to a new form, which has its recordsource turned into the records that correspond with the selection from the listbox. Here's the code I have so far:
The form should now display the selected records only, but instead, it shows all the records in tblGroups. Does anyone understand why, and what I should do to actually make it show only those records that I want to show?
Kind regards,
Martijn Senden.
In the Beginning there was nothing, which exploded.
--Terry Pratchett, Lords and Ladies--
I have a database in which I have a form containing a listbox. When a user selects some rows in the listbox and then hits a commandbutton, he should be taken to a new form, which has its recordsource turned into the records that correspond with the selection from the listbox. Here's the code I have so far:
Code:
Private Sub cmdChangeGroupProperties_Click()
Dim Criteria As String
Dim ctl As Control
Dim itm As Variant
Dim db As Database
Dim Q As QueryDef
Dim strSQL As String
Dim sql As String
Dim strQSQL As String
Dim frm As Form
Dim strForm As String
' Build a list of the selections.
Set ctl = Me.lstGroups
'If a selection is made, build a list of the selections
If Not ctl.ItemsSelected.Count = 0 Then
'Build list
For Each itm In ctl.ItemsSelected
If Len(Criteria) = 0 Then
Criteria = ctl.ItemData(itm)
Else
Criteria = Criteria & "," & ctl.ItemData(itm)
End If
Next itm
'Apply list to query
Set db = CurrentDb()
Set Q = db.QueryDefs("qrySelectedGroups")
strSQL = "SELECT * FROM tblGroups WHERE (((tblGroups.GroupID) In (" & Criteria & "))) ORDER BY tblGroups.Name;"
Q.sql = strSQL
'Apply query to form
DoCmd.OpenForm "frmEnterGroups" , , , , , acHidden
strForm = "EnterGroups"
Set frm = Forms(strForm)
With frm
.RecordSource = Q.sql
.Caption = "Change properties of selected groups"
.ScrollBars = 0
.RecordSelectors = False
.NavigationButtons = True
.DividingLines = False
End With
'Form is now correctly formatted and has the right
'recordsource; now close and save it and re-open it
'as a dialog.
DoCmd.Close acForm, "frmEnterGroups", acSaveYes
DoCmd.OpenForm "frmEnterGroups", acNormal, , , acFormEdit, acDialog
'Close Querydef
Q.Close
'If no rows have been selected, alert the user and exit
'the sub.
Else
MsgBox ("You have not selected any groups!")
End If
End Sub
The form should now display the selected records only, but instead, it shows all the records in tblGroups. Does anyone understand why, and what I should do to actually make it show only those records that I want to show?
Kind regards,
Martijn Senden.
In the Beginning there was nothing, which exploded.
--Terry Pratchett, Lords and Ladies--