Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open records base on a multi-select listbox

Status
Not open for further replies.

ind

Programmer
Mar 9, 2000
121
US
I have a form with a listbox showing all the completed jobs for my company.&nbsp;&nbsp;The listbox is set up for multi-select (&quot;simple&quot;). I want the user to be able to open a form called &quot;frmCompletedJobs&quot; with a command button and the form filtered for the records he/she selected. I've tried everything. Here the code I've got so far.<br><br>Private Sub CmdGetJobs_Click()<br>On Error GoTo Err_CmdGetJobs_Click<br>Dim db As Database, rst As Recordset<br>Dim varNumber As Variant<br>If JobList.ItemsSelected.Count = 0 Then<br>MsgBox &quot;You must select at least one Job.&quot;, vbOKOnly + vbExclamation, &quot;Select a Job&quot;<br>End If<br>Set db = CurrentDb<br>Set rst = db.OpenRecordset(&quot;tblJobs&quot;, dbOpenTable)<br>rst.Index = &quot;PrimaryKey&quot;<br>For Each varNumber In JobList.ItemsSelected<br><br>rst.Seek &quot;=&quot;, JobList.ItemData(varNumber)<br>rst!strJobNumber = JobList<br><br>Next<br><br>Exit_CmdGetJobs_Click:<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br><br>Err_CmdGetJobs_Click:<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox Err.Description<br>&nbsp;&nbsp;&nbsp;&nbsp;Resume Exit_CmdGetJobs_Click<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>End Sub<br><br>Please help me.<br>Thanks :):):):
 
Here is what you do to cycle through each of the items in the listbox, create a filter out of the items, and open the form using that filter (I am assuming the field in the table that holds the jobs is fldJob):<br><br>Private Sub CmdGetJobs_Click()<br><br>On Error GoTo Err_CmdGetJobs_Click<br><br>Dim varNumber As Variant<br>dim strFilter as string<br><br>If me.JobList.listcount = 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;You must select at least one Job.&quot;, vbOKOnly + vbExclamation, &quot;Select a Job&quot;<br>else<br>&nbsp;&nbsp;&nbsp;&nbsp;For Each varNumber In Me![JobList].ItemsSelected<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if strFilter &lt;&gt; &quot;&quot; then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strFilter = strFilter & &quot; or &quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;endif<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strFilter = strFilter & &quot;fldJob = &quot;&quot;&quot; & Me![JobList].ItemData(varNumber) & &quot;&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;next varNumber<br>&nbsp;&nbsp;&nbsp;&nbsp;docmd.openform &quot;frmCompletedJobs&quot;, acNormal,,strFilter<br>endif<br><br>Exit_CmdGetJobs_Click:<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br><br>Err_CmdGetJobs_Click:<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox Err.Description<br>&nbsp;&nbsp;&nbsp;&nbsp;Resume Exit_CmdGetJobs_Click<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>End Sub<br><br>Give it a try.<br> <p>-Chopper<br><a href=mailto: > </a><br><a href= > </a><br>
 
Thanks for the help. This works great!!!!!!!!!!!!!!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top