I'm still not clear just want you are trying to achieve but you'd
probably want something along these lines:
Dim cmd As ADODB.Command
Dim strSQL As String
Dim varItem As Variant
Dim ctrl As Control
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
Set ctrl = Me.List38
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strSQL = "UPDATE tblPopulation " & _
"SET covered = """ & Me.SiteID & """" & _
"WHERE county = """ & ctrl.ItemData(varItem) & """"
cmd.CommandText = strSQL
cmd.Execute
Next varItem
Else
MsgBox "No counties selected", vbInformation, "Warning"
End If
This assumes that the covered column in the tblPopulation table is to
be updated in each row where the value of the county column is one of
those selected in the list box, updating the column with a string
value of SiteID which is a control in the current form. Both covered
and county are assumed to be columns of text data type.
However, I'm having to make a number of leaps in the dark here as you
don't say where the value of SiteID is coming from (so I've assumed
it’s a control, bound or otherwise, in the form). More fundamentally,
I'm not entirely clear whether by 'update' you are using this term in
the usual sense of changing a value in an existing row in a table
(which I've assumed above) or in the more generic sense in which it is
often used in the literature, covering any changes, including the
insertion of a row, the amendment of a value or values, and even the
deletion of a row.
We can of course rule out deletion of a row, but if you want to insert
a row then the SQL statement built in the above code would need to be
amended so that it does that rather than amending an existing row.
Ken Sheridan
Stafford, England
Sorry, I have a multi-select list box. The data in this box comes
from tblPopulation, which contains variables for state, county,
population, and a text field [covered] which indicates the siteID
associated with that county (to indicate whether or not it is
covered).
On the form, the user first selects the state, which then causes the
list box (List38) to populate only with the counties for that state.
They will then click on which counties are covered.
What I'd like to do, is make sure that if the user has selected a
county, that the field [covered] in tblPopulation is updated to
contain the SiteID.
note: qry_Population is essentially the same as the table, only it has
a combined field for state and county - for ex: "Massachusetts: Essex
County"
I've figured out how to point to the field I'm interested in, but not
how to actually change it:
With Forms![form1]!List38
For i = 0 To .ListCount - 1
If .Selected(i) Then
statecounty = newProgramState & ": " & .Column(2, i)
MsgBox (DLookup("[covered]", "qry_Population", "[stcty] =
'" & statecounty & "'"))
End If
Next i
End With
I hope that is more clear, thanks in advance for your help.
On May 7, 11:47 am, (e-mail address removed) wrote: