473,544 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Listbox and multiple items to ACCESS Table

171 New Member
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
15 2270
jimatqsi
1,273 Recognized Expert Top Contributor
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 New Member
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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:StartDa te 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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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

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

Similar topics

1
6341
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 database using unicode compression field (for ancient language). I would like to export this table into MySQL o Postgres, but it's
4
5348
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), column4 (Forename) and column5 (title). Columns 3,4 and 5 are not shown in the list box only the first two. DOB Name: &" "&&", "&
5
3516
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 selected items of that listbox and display it. can anyone help? Thanks
3
7370
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 Add button, and the selected items will move to the second ListBox. I've been trying to use the ListBox.SelectedObjectCollection with no success. It...
4
2342
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
1883
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 boxes I want filled are Area, County, Center. Each individual center has a specific number. I have entered all that information onto a table with...
7
7113
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 it only imports the one file. I would like the code to import all files in the folder into the table. Any suggestions? Sub FuelTaxImport() ...
3
2658
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 on a select query that joins the parent and child records, but it's not an obvious solution. I'm sure it's easy though, any links?
2
6931
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 come across a way to set the listbox value to only 1 at a time, but need multiple toggle buttons to be on their corresponding listbox item values...
0
7447
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7400
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5947
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5314
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4935
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3429
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1857
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
684
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.