473,386 Members | 1,790 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,386 software developers and data experts.

Multiple Listbox and multiple items to ACCESS Table

171 100+
Hi
I have 2 date fields (date1 and date2)and 3 listbox (Listb1, LIstb2, and ListB3) in an ACCESS form.
What I am trying to do is select dates in the date fields, select 2 items from each listbox and trying to save it to a table. I need the date1,Listb1 item1, Listb2 item1 and Listb3 item1 to be saved as one record and date2,Listb1 item2, Listb2 item2 and Listb3 item2 to be saved as another record.
The code I am using is as below:
Expand|Select|Wrap|Line Numbers
  1. Dim rs As Recordset
  2.  Set rs = CurrentDb.OpenRecordset("tbl", dbOpenDynaset)
  3.  
  4.  With rs
  5. For Each varItem In Me.Listb1.ItemsSelected
  6. Dim Sdate1 As Date
  7. Dim edate1 As Date
  8. Dim n As Date
  9. Dim c As Long
  10. Sdate1 = Me.date1
  11. edate1 = Me.date2
  12.     For n = Sdate1 To edate1
  13.            c = c + 1
  14.  
  15.  rs.AddNew
  16.  rs!StartDate = Me.date1
  17.  rs!EndDate = Me.date2
  18.  rs!Listb1item1 = Me.Listb1.ItemData(varItem) 
  19.  rs!istb2item1 = Me.listb2.Column(0) 
  20.  rs!lstb3item1 = Me.listb3.Column(0) 
  21.  rs!rptfld = Me.Listb1.ItemData(varItem)   'this is another field to hold the same data of rs!Listb1item1
  22.  
  23. rs.Update
  24.  
  25. Next n
  26. Next varItem
  27.  
  28. End With
  29.  
  30. rs.Close
  31. Set rs = Nothing
  32.  
But this code is not doing the way I want it. Any help will be appreciated. Thank you
Jun 11 '15 #1

✓ answered by jforbes

This:
If there are 10 days then there will be 10 items selected from the listbox.
contradicts:
But my question is instead of daterange, i will have two dates in the date fields and two selections from listbox. How can i put record1 with date1 and list1 then record2 with date2 and list2.
The First/Previous Version would require one Day per Selected Item. The second is not bound by this one to one relationship. Instead, by implementing the formula:
Increment = (EndDate-StartDate)/ItemCount
The following code should distribute the Items over the Date Range:
Expand|Select|Wrap|Line Numbers
  1.     Dim Sdate1 As Date
  2.     Dim edate1 As Date
  3.     Dim iIncrement As Double
  4.     Dim dTemp As Date
  5.     Dim iCount As Integer
  6.     Dim iItemCounter As Double
  7.     Dim rs As Recordset
  8.  
  9.     Sdate1 = Me.date1
  10.     edate1 = Me.date2
  11.     If Me.lstbx1.ItemsSelected.Count > 1 Then iIncrement = (edate1 - Sdate1) / (Me.lstbx1.ItemsSelected.Count - 1)
  12.     iItemCounter = 0
  13.     Set rs = CurrentDb.OpenRecordset("Schedule", dbOpenDynaset)
  14.  
  15.     For iCount = 0 To Me.lstbx1.ListCount
  16.         If Me.lstbx1.Selected(iCount) = True Then
  17.             rs.AddNew
  18.                 dTemp = DateAdd("d", iItemCounter, Sdate1)
  19.                 rs!StartDate = dTemp
  20.                 rs!EndDate = dTemp
  21.                 rs!Action = Me.lstbx1.ItemData(iCount)
  22.             rs.Update
  23.             iItemCounter = iItemCounter + iIncrement
  24.         End If
  25.     Next iCount
  26.  
  27.     rs.Close
  28.     Set rs = Nothing

15 2264
jimatqsi
1,271 Expert 1GB
rajeevs, could you be more specific about the problem? Just saying it doesn't work is not very informative. What is happening?

I have never tried a for/next loop based on incrementing a date. Does that work? You might want to stop the debugger to check if n is incrementing correctly.

Jim
Jun 11 '15 #2
rajeevs
171 100+
Hi jimatqsi
Sorry for the delay. I was off duty during the last two days.
That was a copied code from somewhere.
What I want to do is : there will be two date selections in the form (from and to dates) and 3 listboxes which can be used to select multiple Items (listboxes as listbox1, listbox2 and listbox3). If the duration of dates selected is 5 then the count of items will be equal to that count.
eg: if date1 is 01-01-15 and date2 is 05-01-15 the user will select 5 items from the listboxes. After the selections I like to put 5 records into the table as :
record1 with date 01-01-15 listbox1 item1 listbox2 item1 listbox3 item1. Record 2 will be with date 01-01-05 and listbox1 item2, listbox2 item2 and listbox3 item2.
Record3 with date 03-01-15 and listbox items 3
record 4 with date 04-01-15 and listbox items 4 and record 5 with date 05-01-15 and listbox items 5
Hope I explained well. Any help to modify the code according to my requirement will be appreciated.
Thank you
Jun 14 '15 #3
rajeevs
171 100+
Hi

I have modified the code as below. Now my problem is the first record writes correctly but the second record writes everything correctly except the date. In the second record I need the edate1 to be written.
Any help?
Expand|Select|Wrap|Line Numbers
  1. Dim rs As Recordset
  2.  Set rs = CurrentDb.OpenRecordset("tbl", dbOpenDynaset)
  3.  
  4. With rs
  5.  For Each varItem In Me.lstbx1.ItemsSelected
  6.  
  7. Dim Sdate1 As Date
  8. Dim edate1 As Date
  9. Dim n As Date
  10. Sdate1 = Me.SDate
  11. edate1 = Me.EDate
  12.  
  13.   For n = Sdate1 To Sdate1
  14.  
  15.  rs.AddNew
  16.  
  17.  rs!StartDate = n
  18.  rs!EndDate = n
  19.  rs!fld1 = Me.lstbx1.ItemData(varItem)  
  20.  'there are other fields as well which I have removed from this code
  21.  rs.Update
  22.  
  23.  
  24. Next n
  25.  
  26. Next varItem
  27.  
  28. End With
  29.  
  30.  rs.Close
  31.  Set rs = Nothing
Jun 16 '15 #4
jforbes
1,107 Expert 1GB
I'm not sure what happens when you include a Dim Statement inside of a For Each Loop. You might try moving the Dim Statements in Lines 7-9 of post #4 to the Top of the Code. I would also move Lines 10 and 11 along with the Dim Statements as you really only need to get these values once.

Also, Line 13 sets up a loop where the start and end are the same. If this is correct, then you really don't need a loop as it will just run once.
Jun 17 '15 #5
rajeevs
171 100+
Hi jforbes

I have modified the code earlier also and now the modified one i am posting. But this code is creating 4 records if the list box selection is 2 items and startdate to end date is 2 days
Expand|Select|Wrap|Line Numbers
  1. Dim rs As Recordset
  2.  Set rs = CurrentDb.OpenRecordset("tbl", dbOpenDynaset)
  3.  Dim n As Date
  4.  
  5.  
  6. With rs
  7. For Each varItem In Me.lstbox.ItemsSelected
  8.  
  9. For n = Me.SDate To Me.EDate
  10.  
  11.  rs.AddNew
  12.  
  13.  rs!StartDate = n
  14.  rs!EndDate = n
  15.  rs!lsboxfld= Me.lsbox.ItemData(varItem)
  16.  
  17. rs.Update
  18.  
  19. Next n
  20.  
  21. Next varItem
  22.  
  23.  
  24. End With
  25.  
  26.  rs.Close
  27.  Set rs = Nothing
  28.  
  29.  
Suppose if i have sdate as 01-01-15 and edate as 02-01-15 and listbox 2 items selected (Say apple and orange) then there will be 4 records added to the tbl as
record1: StartDate 01-01-15 item apple enddate 01-01-15.
record2: StartDate 02-01-15 item apple enddate 02-01-15.
record3: StartDate 01-01-15 item orange enddate 01-01-15.
record4: StartDate 02-01-15 item orange enddate 02-01-15.
But my desired result need to be as below:
REcord1:StartDate 01-01-15 item apple enddate 01-01-15.
record2: StartDate 02-01-15 item orange enddate 02-01-15.
Hope I have explained well. please advice how to modify the code to get this result
Jun 17 '15 #6
jforbes
1,107 Expert 1GB
Interesting. I still don't know enough of the rules to help.

What do you do if there are 10 days between the Start Date and End Date and only 2 Items Selected? Do you insert Ten Records, five with the first Item and five with the second item? Or do you insert Two Records, one on the start date with the First Item and one on the End Date with the Last Item?

What do you do if there are 10 items selected and only 2 days between the Start and End Dates?
Jun 17 '15 #7
rajeevs
171 100+
Hi jforbes
Thank you for the kind reply. If there are 10 days then there will be 10 items selected from the listbox. That is the way it is restricted. So for each date between start date and end date there need to be one record for each item selected. record 1 will have date1 and item1 record2 will have date2 and item2 like that.
Hope i explained well on the logic. Please help
Jun 18 '15 #8
jforbes
1,107 Expert 1GB
I think I understand now what you are attempting to do. I think the first place you are getting stuck is you only need one Loop structure to do what you want, and adding two loops is just messing things up. Since you are only inserting one date per selected item, it would be easiest to loop on the selected items and insert a date for each one found. So in that case, you don't even need to know the End Date as it will be the Start Date plus a day for each item selected. This is at least my understanding.

Given this understanding, you could do something like the following. It only has one loop and inserts one record for each item that is selected. Also, I just mocked this up, so I'm not 100% sure it's syntax is correct:
Expand|Select|Wrap|Line Numbers
  1.     Dim Sdate1 As Date
  2.     Dim dTemp As Date
  3.     Dim iCount As Integer
  4.     Dim iItemCount As Integer
  5.     Dim rs As Recordset
  6.  
  7.     Sdate1 = Me.sDate
  8.     iItemCount = 0
  9.     Set rs = CurrentDb.OpenRecordset("Schedule", dbOpenDynaset)
  10.  
  11.     For iCount = 1 To Me.lstbx1.ListCount
  12.         If Me.lstbx1.Selected(iCount) = True Then
  13.             rs.AddNew
  14.                 dTemp = DateAdd("d", iItemCount, Sdate1)
  15.                 rs!StartDate = dTemp
  16.                 rs!EndDate = dTemp
  17.                 rs!Action = Me.lstbx1.ItemData(iCount)
  18.             rs.Update
  19.             iItemCount = iItemCount + 1
  20.         End If
  21.     Next iCount
  22.  
  23.     rs.Close
  24.     Set rs = Nothing
  25.  
Jun 18 '15 #9
rajeevs
171 100+
Hi jforbes
Thank you for the prompt reply. I think your code will solve my issue. I haven't tried that yet. as soon as I saw your reply I thought of replying first. I will try the code and let you know the result.
Jun 18 '15 #10
rajeevs
171 100+
Hi jforbes
Thank you for the help and support.
I tried the code just now. If line number 11
Expand|Select|Wrap|Line Numbers
  1. For iCount = 1 To Me.lstbx1.ListCount
it writes only the last item from the list.
I have modified that line to icount=0 like below then it writes two records because the list box selection was two items.
Expand|Select|Wrap|Line Numbers
  1. For iCount = 0 To Me.lstbx1.ListCount
I will try with more scenario and let you know the result.
One more question: if I have two different dates in the sdate and edate field and two items selected from list box how can I modify this to write the first record with sdate and item1 and second record with edate and item2. Because most of the situation the user will be selecting either consecutive dates or two different dates.
I will give it a try also with the code you have provided.
I thank you so much for the quick response
Jun 18 '15 #11
jforbes
1,107 Expert 1GB
Nice catch on the zero based index for the Listbox.

OK then, I assumed since you were using a loop on the Date before, that you expected to increment the Date as one Day at a time. If you want to allow the user to Select a Start Date and End Date and then select two items in the ListBox, then insert the First item with the Start Date and the Last Item with the End Date, this would be a slightly different scenario as the Date is no longer incremented by one Day. If I understand correctly, in this case, the Date would be Incremented by the amount of Days between the Start Date and End Date, divided by the amount of Items.
Increment = (EndDate-StartDate)/ItemCount

So if the User selects Three Items, then Selects a Start Date of the First of the Month (Day 1) and End Date of the 5th Day of the Month (Day 5), then the Three Items should be evenly distributed across those 5 Days ending up with:
Item 1 on Day 1
Item 2 on Day 3
Item 3 on Day 5

Also, if the User selects Six Items, then a Start Date of the First and an End Date of the Third, you should end up with:
Item 1 on Day 1
Item 2 on Day 1
Item 3 on Day 2
Item 4 on Day 2
Item 5 on Day 3
Item 6 on Day 3

Is my understanding correct?
Jun 18 '15 #12
rajeevs
171 100+
Hi jforbes
Let me explain the concept of this recordset. Basically I am trying to put a choice of duty types to a table. Now the code is doing what i want to do with the entry. That is if there is a date range and same number of selections in the listbox it need to put date 1 list1 as record1 date2 list2 as record2 date3 list3 as record3 etc.
But my question is instead of daterange, i will have two dates in the date fields and two selections from listbox. How can i put record1 with date1 and list1 then record2 with date2 and list2.
Hope I explained well. Thank you for the efforts and your kindness
Jun 19 '15 #13
jforbes
1,107 Expert 1GB
This:
If there are 10 days then there will be 10 items selected from the listbox.
contradicts:
But my question is instead of daterange, i will have two dates in the date fields and two selections from listbox. How can i put record1 with date1 and list1 then record2 with date2 and list2.
The First/Previous Version would require one Day per Selected Item. The second is not bound by this one to one relationship. Instead, by implementing the formula:
Increment = (EndDate-StartDate)/ItemCount
The following code should distribute the Items over the Date Range:
Expand|Select|Wrap|Line Numbers
  1.     Dim Sdate1 As Date
  2.     Dim edate1 As Date
  3.     Dim iIncrement As Double
  4.     Dim dTemp As Date
  5.     Dim iCount As Integer
  6.     Dim iItemCounter As Double
  7.     Dim rs As Recordset
  8.  
  9.     Sdate1 = Me.date1
  10.     edate1 = Me.date2
  11.     If Me.lstbx1.ItemsSelected.Count > 1 Then iIncrement = (edate1 - Sdate1) / (Me.lstbx1.ItemsSelected.Count - 1)
  12.     iItemCounter = 0
  13.     Set rs = CurrentDb.OpenRecordset("Schedule", dbOpenDynaset)
  14.  
  15.     For iCount = 0 To Me.lstbx1.ListCount
  16.         If Me.lstbx1.Selected(iCount) = True Then
  17.             rs.AddNew
  18.                 dTemp = DateAdd("d", iItemCounter, Sdate1)
  19.                 rs!StartDate = dTemp
  20.                 rs!EndDate = dTemp
  21.                 rs!Action = Me.lstbx1.ItemData(iCount)
  22.             rs.Update
  23.             iItemCounter = iItemCounter + iIncrement
  24.         End If
  25.     Next iCount
  26.  
  27.     rs.Close
  28.     Set rs = Nothing
Jun 19 '15 #14
rajeevs
171 100+
Hi jforbes
Thank you for the reply. The first code is ok. My last question refers to a different scenerio. Only two dates and two selection in the list box. I want to write the first record with startdate and listbox item1. Next record will be Enddate and list box item 2.
Jun 20 '15 #15
rajeevs
171 100+
Hi jforbes
I have posted a reply earlier without testing the code. But now I have tested the code and I feel that it is doing what i need.
Here, we are on weekend and I will test it on Monday thoroughly and let you know. I am so grateful to you and thank you for the patience and kindness.
Jun 20 '15 #16

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

Similar topics

1
by: JJ | last post by:
Hi, usually, I'm not using MS servers, but I have a big problem with a Access table. I should create a web application for a Historical Dipartment. They have create a populated a Access...
4
by: carl.barrett | last post by:
Hi, I have a list box that displays 2 columns. Behind it sits a query with five columns. These are Column1 (DOB), column2 (a concatenated string of Surname Forname, Title), Column3 (Surname),...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
3
by: Dany P. Wu | last post by:
Hi everyone, One of my Windows forms contain two listbox controls, with Add and Remove buttons between them. The idea is to allow users to select multiple items from one ListBox, click the...
4
by: Bill Nguyen | last post by:
I would like to be able to do the following: Listbox1/Textbox1 contains list of all items not selected item A item B item D Listbox2//Textbox2 contains list of all items selected
9
by: KMorris | last post by:
I'm working on a database for comment cards (similar to the ones at restaurants with the number scoring) I have 3 boxes at the beginning of each entry I would like to have filled automatically. The...
7
by: cannunzi | last post by:
I'm trying to import multiple files in one access table all at once but i keep hitting an error stating incorrect file path. The code below works when i change the * to the exact file name but then...
3
by: bbcrock | last post by:
Real quick question, can anyone point me to an article discussing how to use a listbox where the data comes from a lookup table to store multiple records in a child table? I created a form based...
2
by: 6afraidbecause789 | last post by:
Hi - Has anyone ever used toggle buttons to select items in a listbox? I'd like to put about 24 toggle buttons on an unbound form that select or deselect items in a multiple select listbox. I've...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.