473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Adding Items from a Multiple Select List Box to a Table

beacon
579 512MB
Hi everybody,

[Access 2003]

I'm creating a database that will assign standards to departments. My plan is to have a form that will allow the user to use 'Next Record' to go to each standard and have a multiple select list box with all of the departments, so the user can select those departments that are responsible for the standards.

I don't have much experience with list boxes, so is it possible to create a form like this? If so, and I think it is, will I have to add code to the list box to update the table based on the selections or is there a way to set up the form to have it automatically update the table? I searched online, but the results were limited and didn't seem to address what I'm trying to do.

Here's my tables (only relevant fields are shown):
Expand|Select|Wrap|Line Numbers
  1. tblDepartments
  2.  
  3. DepartmentID - Autonumber
  4. DepartmentName - Text
  5.  
Expand|Select|Wrap|Line Numbers
  1. tblStandards
  2.  
  3. StandardsID - Autonumber
  4. StandardText - Text
  5.  
Expand|Select|Wrap|Line Numbers
  1. tblStandardsDepartment
  2.  
  3. StandardsDepartmentID - Autonumber
  4. StandardsFK - Number
  5. DepartmentsFK - Number
  6.  
There's a one-to-many relationship from tblStandards to tblStandardsDepartment and a one-to-many relationship from tblDepartment to tblStandardsDepartment.

Thanks,
beacon
Jun 15 '10 #1
4 7144
ADezii
8,834 Expert 8TB
@beacon
This would be the general idea, make sure that you set a Unique, Non-Primary Key, Composite Index on the tblStandardsDepartment Table based on the [StandardsFK] and [DepartmentsFK] Fields in order to avoid Duplication.
Expand|Select|Wrap|Line Numbers
  1. Dim intI As Integer
  2. Dim lst As ListBox
  3. Dim varItem As Variant
  4.  
  5. Set lst = Me![lstDepartments]
  6.  
  7. With lst
  8.   If .ItemsSelected.Count = 0 Then Exit Sub
  9.     For Each varItem In .ItemsSelected
  10.       CurrentDb.Execute "INSERT INTO tblStandardsDepartment ([StandardsFK], [DepartmentsFK]) VALUES (" & _
  11.                          Me![txtStandardsID] & "," & .Column(0, varItem) & ");", dbFailOnError
  12.     Next
  13. End With
Jun 15 '10 #2
beacon
579 512MB
I'm not sure what you mean when you say, "make sure that you set a Unique, Non-Primary Key, Composite Index on the tblStandardsDepartment Table based on the [StandardsFK] and [DepartmentsFK] Fields in order to avoid Duplication." Are you saying that I need to create another autonumber field?

Between the time I posted my initial question and now I showed the person that requested the database what my plans were and they gave me new instructions for the listbox. They want the listbox to include check boxes.

I know that won't be possible using the listbox control, but thought that it might be possible using a subform with check boxes on it. My question now is whether or not I could still use the code you provided to insert the departments selected into the table.

One other question, the subform would need to be unbound if I went that route, right?
Jun 16 '10 #3
ADezii
8,834 Expert 8TB
@beacon
Are you saying that I need to create another autonumber field?
No, just eliminating duplication on the Foreign Key Fields.
I know that won't be possible using the listbox control, but thought that it might be possible using a subform with check boxes on it. My question now is whether or not I could still use the code you provided to insert the departments selected into the table.
No, completely different scenario.
Jun 16 '10 #4
beacon
579 512MB
To eliminate the duplication on the foreign keys, all I have to do is change the foreign key fields on the tblStandardsDepartment to "Yes (No Duplicates)" for the index, right?

Just to make sure I'm clear on your post prior to your last one, the code that you provided will update my tblStandardsDepartment after the selection has been made, right?

What about the display and if items are unselected? If, for the first standard, I'm supposed to have Dept 1, 3, and 5 assigned to it and I go to the second standard, where Dept 2 and 4 should be assigned, will the selections on the list update based on the standard? And would I need to write a block of code similar to yours for when the user unselects an item in the list box so it updates the table?

I'm also concerned what I need to enter for the control source, if anything, for the Department List Box.
Jun 17 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: R.G. Vervoort | last post by:
Not sure if it is a php question but since i am working in php here it is. Ho can i add items to (the item label=visual text and the value=index number) a list object on a website. I am having...
0
by: Stephen Witter | last post by:
I have a multiple select list and I am trying to post to a page where I have no option to use the request.form or vbscript to iterate through the control. The page is a sql server reporting...
29
by: Paul | last post by:
Hi, I'd like to limit the number of selections a user can make in a multiple select listbox. I have a note on the interface to say that only x no. of items should be selected and I check the...
1
by: | last post by:
Hi guys, i have a multiple select list box on my form, and it saves the selections I make into the field just fine., but when i open the form on an existing record, it does not show up the...
3
by: Usenet | last post by:
Hi there, I am quite a novice to MS Access, and I would deeply appreciate help on the following: I have created a list box in a form. I wish for that list box to enter multiple values from table...
1
by: abhishekhs | last post by:
Hi all I have more than one multiple select lists in a page. Something like this <tr> <td> <select NAME="StrainList" ID="StrainList" SIZE="5" multiple="multiple" style="width: 150px"> <?...
4
by: rn5a | last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5 options are shown at any given time) allows multiple selection whereas the 2nd one allows only 1 option to be selected at a time. ...
2
by: sathyashrayan | last post by:
Dear group, I have a task where I have to fetch datas from mysql and display it in the multiple select list box. I have fetched the datas and it is getting diplayed correctly in the multiple...
3
by: asivakrupa | last post by:
Hi, I am using a multiple select list box. I use an array to retrieve the values that are selected by the user. <select name="competitor"> When I try to validate the list box if the...
3
!NoItAll
by: !NoItAll | last post by:
I have some code that adds items to a List(of Object). It is behaving extremely bizarre! Here's my original code Dim BeaconStoryList as New List(Of My.StoryType) Dim BeaconStory as New...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.