Ben,
Sorry for the delay. I've been really busy (as usual).
Let's see if I understand you correctly. The form, frmECR, is based on
tblECR, and this table contains a field called "ECRNumber" which is the
field of interest. I presume this table also contains a primary key,
probably an AutoNumber field called (something like) ECR_ID (an AutoNumber
datatype). On frmECN, you have two listboxes, the first of which gets its
data from tblECR and displays all the ECRNumbers from tblECR. The second
lisbox is unbound and is populated using code that gets the multi-selected
values from the first listbox. I'm guessing you want to store the
multi-selected values into tblECN.
What you haven't explained is what the data in tblECN relates to. We can of
course, just dump the data into tblECN, but this list of ECRs must "belong"
to or "relate" to something. What I mean by this is, if you were to display
a record in a form, you would probably want to see all the ECRs that
"relate" or "belong" to that record. And you might have many such "parent"
records, each potentially "owning" a number of multi-selected ECRs. The
usual way to accommodate this functionality is to create a table like this:
tblECN
ECR_ID (Long Integer)
(a foreign key to another 'related' table) (Long Integer)
ECRNumber (I assume a Text datatype)
...and then create a one-to-many relationship between the "parent" table,
and tblECN (to the foreign key in tblECN). Another one-to-many relationship
would exist between tblECR and tblECN (to ECR-ID). Such a table structure
will store a separate record for every ECR that was selected (for each
"parent" record).
Once you have such a structure, the code needed to store each datum into
tblECN is as follows:
The following pseudo-code should replace the line that adds the record
to the second listbox.
Dim db As DAO.Database
Dim strSQL As String
Set db = CurrentDb
For Each .........
'Code that gets each selected value from the first listbox
strSQL = "INSERT INTO tblECN (foreign_key, ECRNumber) " & _
"VALUES (" & lngForeignKey & ", """ &
strSelectedValue & """)"
db.Execute strSQL, dbFailOnError
Next ....
Set db = Nothing
Does this make sense?
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
bronen said:
Graham,
Here is what I want to do. I have a table called tblECR and a fieldname
in
tblECR called 'ECRNumber'. On a form, 'frmECR' i have a text box that you
just type the number of the ECR such as ECR123. Next, I have a table
called
tblECN with a fieldname called 'ECRsSelected'. On the form, frmECN I have
a
listbox assigned to ECRsSelected field. I want to be able to select
multiple
values from the tblECR - ECRNumber field and store it in the ECRsSelected
field. Essentially I want to store 1 or many entries.
What I have currently is an unbound listbox that does a query on the
tblECR
and pulls in all the ECRNumbers. Then I have a command button that pulls
the
multi-selected items from the unbound listbox and copies them to the
ECRsSelected listbox. So far, I can see multiple values in the
ECRsSelected
listbox. But, when i open the table datasheet view of tblECN, the
ECRsSelected field only has 1 data item. How can i store multiple values
in
that field AND when I go to the next record on the form, the ECRsSelected
listbox is cleared out? You see my code below. It works to a certain
point.
Thanks
Ben
Graham R Seach said:
Ben,
It's not that easy! In order to to what you're asking, we need to know
the
following:
- The listbox's RowSource (if it's a table or query, then we need to
know the name and structure of the table that contains the data you want
to
copy)
- The name and structure of the destination table
But before we go too far down the track, exactly what is it that you're
trying to accomplish? I have the feeling you're attempting to duplicate
data.
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
So how would you adjust my code to meet that recommendation?
Ben
:
Ben,
Listboxes are simply data presentation devices, not data storage
devices.
If
you want the data persisted to a table, then you must push the data
into
the
table, not to the listbox. Once the data are in the table, requery the
listbox.
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
I have the following code to move items from the listbox to another
listbox.
It works, but almost.
Once I have moved multiple items to the destination listbox I can
see
the
many entries. What I don't get is why all the entries are not
stored
in
the
field for that table. I only get the first entry. Also, after I
add
the
data on the form and go to the next record, the data doesn't change
or
clear
out.
Please help. My code below:
Dim ctrlListBox As ListBox
Dim FRM As Form, CTL As Control
Dim varitem As Variant
Dim strSQL As String
Set FRM = Forms![frmECN]
Set CTL = FRM!ECRNumber
Dim DB As Database
Set DB = CurrentDb
Set ctrlListBox = FRM!ECRsSelected
For Each varitem In CTL.ItemsSelected
ctrlListBox.AddItem Item:=CTL.ItemData(varitem)
Next
Thanks for the help
Ben