Record Selection Form for Update Query

D

David Grant

I need to design a form that allows the user to select records via the
standard SHIFT-click/CTRL-click/CTRL-SHIFT-click method (selecting record
ranges with gaps). These selected records must then update a user-defined
field with a user-defined value. The need to do this stems from the fact
that Access datasheets apparently don't behave like Excel with respect to
copying a single field and pasting to multiple fields.

Is this even possible? The selection method I described doesn't seem to be
inherently supported in any of Access' forms... I'm pretty new to this and
not really sure where to begin. Any tips? Any examples out there that I can
start from?

Thanks,

David
 
D

David Grant

The need to do this stems from the fact that Access datasheets apparently
don't behave like Excel with respect to copying a single field and pasting
to multiple fields.

I meant "cells" not fields. Not sure what the terminology is in Access yet.
 
G

Guest

David;

Access is a database management system not a spreadsheet, so despite
superficial similarities in appearance between a table in datasheet view and
an Excel worksheet the two are very different in both concept and
functionality.

You can use a list box on an unbound form to select the records in this way
by setting its MultiSelect property to Extended. The RowSource for the list
box would be a query which returns the rows from the table. The ColumnCount
and ColumnWidths properties of the list box would be set to show the columns
as appropriate.

To update the table you would then need to write some VBA code which steps
through the list box's ItemsSelected collection and updates the relevant row
in the table for each item selected in the list, identifying the row by the
value of its primary key column. This code could be in the Click event
procedure of a separate button. The best option would be execute an SQL
UPDATE statement for each selected row in the code as this could be built in
code to include the column name and value to which it is to be updated.

While this is relatively straightforward, particularly if you have
experience of writing VBA code, it cannot be said to be trivial. I'd be
happy to give more detailed guidance if you wish to proceed with this
approach, but it might not necessarily be the simplest or most efficient
means of achieving the objective. If you'd care to expand on the rationale
behind what you want to do we might be able to suggest an alternative
approach which is more appropriate in Access.

Ken Sheridan
Stafford, England
 
D

David Grant

Ken,

Thanks for the reply. I understand the method you described (and you're
right about the VBA code... I'm experienced in other languages but not yet
VBA)

Here's the problem in its most basic form.

We have a table which contains, as its primary key, a Location ID. These are
locations within a building where audiovisual (AV) equipment resides. The
majority of IDs refer to peripheral equipment, such as ceiling speakers,
microphones, or projectors. There are typically a few locations where
central equipment resides, which peripheral equipment will connect to.

A second table, the Interconnect Table, connects Location IDs together and
gives cable quantities, cable types, etc.

An AV designer at our company (the database user) will typically browse
through the Interconnect Table and decide "hey, these particular peripheral
locations would be better off being wired to central location A rather than
B" His decision as to which ones, will be based on information not contained
in database.

We need to update destination Location ID fields in a large numbers of
records, which have no apparent common property (hence ruling out a simple
update query). It's technically possible to sequentially, one by one, change
the destination Location IDs but this would be very time consuming (and we'd
be better off sticking with Excel). Due to the nature of things, it is often
possible to sort the records such that large numbers of the records needing
updating are contiguous, hence why I want the SHIFT-click functionality.

I hope I've been clear.

Thanks for your time and help.

David

Ken Sheridan said:
David;

Access is a database management system not a spreadsheet, so despite
superficial similarities in appearance between a table in datasheet view
and
an Excel worksheet the two are very different in both concept and
functionality.

You can use a list box on an unbound form to select the records in this
way
by setting its MultiSelect property to Extended. The RowSource for the
list
box would be a query which returns the rows from the table. The
ColumnCount
and ColumnWidths properties of the list box would be set to show the
columns
as appropriate.

To update the table you would then need to write some VBA code which steps
through the list box's ItemsSelected collection and updates the relevant
row
in the table for each item selected in the list, identifying the row by
the
value of its primary key column. This code could be in the Click event
procedure of a separate button. The best option would be execute an SQL
UPDATE statement for each selected row in the code as this could be built
in
code to include the column name and value to which it is to be updated.

While this is relatively straightforward, particularly if you have
experience of writing VBA code, it cannot be said to be trivial. I'd be
happy to give more detailed guidance if you wish to proceed with this
approach, but it might not necessarily be the simplest or most efficient
means of achieving the objective. If you'd care to expand on the
rationale
behind what you want to do we might be able to suggest an alternative
approach which is more appropriate in Access.

Ken Sheridan
Stafford, England
 
G

Guest

David:

I think the method I suggested in my first reply is likely to be appropriate
here. What you'd need is an unbound form with a multi-select list box,
lstInterconnects say, which lists rows from the Interconnect table, a combo
box, cboLocations say, which lists the destination locations so the user can
select which the one to which to update the rows selected in the list box and
a button to execute the updates.

The list box's RowSource would be a query which lists the interconnect rows
in whatever order you consider most appropriate. There needs to be a way of
uniquely identifying each row, so I'll assume for now that the table is
called Interconnects and has, amongst others, a numeric primary key
InterconectID, e.g an autonumber, and a numeric column DestinationLocationID
which references the primary key of the Locations table. To keep things
simple for this example lets assume the list box list the InterconnectID and
the Destination Locations, the latter as a text column from the Locations
table, and is ordered alphabetically by that column. The RowSource would be:

SELECT InterConnectID, Location
FROM InterConnects INNER JOIN Locations
ON Location.LocationID = InterConnects.DestinationLocationID
ORDER BY Location;

Its BoundColumn property would be 1, its ColumnCount 2, its ColumnWidths
something like 2cm;4cm (experiment with the dimensions to get the best fit)
and its MultiSelect property would be Extended (this allows for Ctrl-Clicking
and Shift-Clicking). In reality you'd probably want more columns listed, but
that’s just a case of modifying the SQL statement and the control's
properties accordingly. If necessary you could build in a means of changing
the sort order of the list on the fly, which would be done by modifying the
control's RowSource property in code, for instance in the AfterUpdate event
procedure of a combo box from which the column on which to sort the list
could be selected.

Lets assume the combo box lists all locations alphabetically, but its value
will be the unseen LocationID corresponding to the text Location value. It’s
the LocationID which will be used in the code. For this the combo box's
RowSource would be:

SELECT LocationID, Location
FROM Locations
ORDER BY Location;

You might be able to restrict the list to only relevant locations rather
than listing them all by means of a WHERE clause in the query.

So, having a list box to select multiple interconnects from and a combo box
to select which destination location the selected interconnects are to be
updated to all that's now necessary is some code in a button's Click event
procedure to execute the updates. The way this will be done is to iterate
through all the selected items in the list box, which make up its
ItemsSelected collection, and to execute an SQL statement to update each one
to the location selected in the combo box, so the code would go like this:

Dim varItem As Variant
Dim strSQL As String
Dim ctrl As Control
Dim cmd As ADODB.Command

Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText

Set ctrl = Me.lstInterconnects

' first make sure a location has been selected
If Not IsNull(Me.cboLocations) Then
' if one or more interconnect has been
' selected iterate through selected items
' and build and execute an SQL statement
' to update each selected row
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strSQL = "UPDATE InterConnects " & _
"SET DestinationLocationID = " & _
Me.cboLocations & _
" WHERE InterConnectID = " & _
ctrl.Itemdate(varItem)
cmd.CommandText = strSQL
cmd.Execute
Next varItem
Else
MsgBox "No interconnects selected", vbInformation, "Warning"
End If
Else
MsgBox "No location selected", vbInformation, "Warning"
End If

Doubtless you'd need to modify this to suit your own scenario, but I hope
I've given you enough to show how it can be done. Let me know if you need
any further clarification. Don't forget that if you've included spaces or
other special characters in any table, column or control names the names need
to be wrapped in brackets [like this] when used in the code.

I've not included any error handling in the above code but that can always
be added later when its been tested. As always when developing routines for
set update operations like this it goes without saying that the table should
be backed up until the routine has been thoroughly tested. Even then regular
backing up of the data is essential of course.

Ken Sheridan
Stafford, England

David Grant said:
Ken,

Thanks for the reply. I understand the method you described (and you're
right about the VBA code... I'm experienced in other languages but not yet
VBA)

Here's the problem in its most basic form.

We have a table which contains, as its primary key, a Location ID. These are
locations within a building where audiovisual (AV) equipment resides. The
majority of IDs refer to peripheral equipment, such as ceiling speakers,
microphones, or projectors. There are typically a few locations where
central equipment resides, which peripheral equipment will connect to.

A second table, the Interconnect Table, connects Location IDs together and
gives cable quantities, cable types, etc.

An AV designer at our company (the database user) will typically browse
through the Interconnect Table and decide "hey, these particular peripheral
locations would be better off being wired to central location A rather than
B" His decision as to which ones, will be based on information not contained
in database.

We need to update destination Location ID fields in a large numbers of
records, which have no apparent common property (hence ruling out a simple
update query). It's technically possible to sequentially, one by one, change
the destination Location IDs but this would be very time consuming (and we'd
be better off sticking with Excel). Due to the nature of things, it is often
possible to sort the records such that large numbers of the records needing
updating are contiguous, hence why I want the SHIFT-click functionality.

I hope I've been clear.

Thanks for your time and help.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top