Multi selection list box to query (1 Viewer)

phil999

New member
Local time
Today, 04:18
Joined
Jul 22, 2006
Messages
5
Hi All

So i have the following which works as a select query

Code:
Private Sub Command4_Click()
 Dim Q As QueryDef, DB As Database
   Dim Criteria As String
   Dim ctl As Control
   Dim Itm As Variant

   ' Build a list of the selections.
   Set ctl = Me![List0]

   For Each Itm In ctl.ItemsSelected
      If Len(Criteria) = 0 Then
         Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
      Else
         Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
          & Chr(34)
      End If
   Next Itm

   If Len(Criteria) = 0 Then
      Itm = MsgBox("You must select one or more items in the" & _
        " list box!", 0, "No Selection Made")
      Exit Sub
   End If

   ' Modify the Query.
   Set DB = CurrentDb()
   Set Q = DB.QueryDefs("QryUpdateTapes")
   Q.SQL = "SELECT * From TblTapes Where [TapeID] In(" & Criteria & _
     ");"
   Q.Close

   ' Run the query.
   DoCmd.OpenQuery "QryUpdateTapes"

End Sub

My table consisits of 3 colums

TapeID this is what the multi selection is based on
TxtBxDate this is date
CaseId listbox (single selection)

what i need is the command button to update the txtbxdate, CaseID ased on the given variables in the listbox , textbox but only for the items selected in the TapeID multiselection list box.

How would i modify the above to do this? The above displays the multi selected tapeID listbox.

Thanks in advance for any help.
 

LPurvis

AWF VIP
Local time
Today, 04:18
Joined
Jun 16, 2008
Messages
1,269
Hi.
It's just a question of rejigging what you've alreayd got.
Instead of a SELECT query you just need an Update statement. You can build your criteria in exactly the same way.
Instead of the section beginning with the "' Modify the Query" comment you could just have

Code:
CurrentDb.Execute "UPDATE TblTapes SET TxtBxDate = Date(), " & _
          "CaseId = '" & Me.CaseId & "' WHERE [TapeID] In (" & Criteria & ")"

Assuming you want todays date in there and that CaseId is the name of the single selection mode listbox...
 

phil999

New member
Local time
Today, 04:18
Joined
Jul 22, 2006
Messages
5
Code:
CurrentDb.Execute "UPDATE TblTapes SET TxtBxDate = Date(), " & _
          "CaseId = '" & Me.CaseId & "' WHERE [TapeID] In (" & Criteria & ")"

the date acually is populated by a calendar control.

so it would be ??
Code:
CurrentDb.Execute "UPDATE TblTapes SET DateRemovedfromLibary = txtbxdate, " & _
          "CaseId = '" & Me.list1 & "' WHERE [TapeID] In (" & Criteria & ")"
 

LPurvis

AWF VIP
Local time
Today, 04:18
Joined
Jun 16, 2008
Messages
1,269
Hi

CurrentDb.Execute "UPDATE TblTapes SET DateRemovedfromLibary = " & Format(txtbxdate,"\#yyyy\-mm\-dd\#") & ", " & _
"CaseId = '" & Me.list1 & "' WHERE [TapeID] In (" & Criteria & ")"
 

phil999

New member
Local time
Today, 04:18
Joined
Jul 22, 2006
Messages
5
thanks for your help. this is the final code

Code:
CurrentDb.Execute "update tblTapes SET TblTapes.DateRemovedfromLibary = " & Format(Forms!frmremovetapefromlibrary!TxtDateRemoved, "\#yyyy\-mm\-dd\#") & "," & _
 " TblTapes.Status = '" & Forms!frmremovetapefromlibrary!LstLocation & "',TblTapes.Caseid = '" & Forms!frmremovetapefromlibrary!LstDataCase & "' WHERE TblTapes.TapeID in  (" & Criteria & ")"
 

Users who are viewing this thread

Top Bottom