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: - Dim rs As Recordset
-
Set rs = CurrentDb.OpenRecordset("tbl", dbOpenDynaset)
-
-
With rs
-
For Each varItem In Me.Listb1.ItemsSelected
-
Dim Sdate1 As Date
-
Dim edate1 As Date
-
Dim n As Date
-
Dim c As Long
-
Sdate1 = Me.date1
-
edate1 = Me.date2
-
For n = Sdate1 To edate1
-
c = c + 1
-
-
rs.AddNew
-
rs!StartDate = Me.date1
-
rs!EndDate = Me.date2
-
rs!Listb1item1 = Me.Listb1.ItemData(varItem)
-
rs!istb2item1 = Me.listb2.Column(0)
-
rs!lstb3item1 = Me.listb3.Column(0)
-
rs!rptfld = Me.Listb1.ItemData(varItem) 'this is another field to hold the same data of rs!Listb1item1
-
-
rs.Update
-
-
Next n
-
Next varItem
-
-
End With
-
-
rs.Close
-
Set rs = Nothing
-
But this code is not doing the way I want it. Any help will be appreciated. Thank you
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
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
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? - Dim rs As Recordset
-
Set rs = CurrentDb.OpenRecordset("tbl", dbOpenDynaset)
-
-
With rs
-
For Each varItem In Me.lstbx1.ItemsSelected
-
-
Dim Sdate1 As Date
-
Dim edate1 As Date
-
Dim n As Date
-
Sdate1 = Me.SDate
-
edate1 = Me.EDate
-
-
For n = Sdate1 To Sdate1
-
-
rs.AddNew
-
-
rs!StartDate = n
-
rs!EndDate = n
-
rs!fld1 = Me.lstbx1.ItemData(varItem)
-
'there are other fields as well which I have removed from this code
-
rs.Update
-
-
-
Next n
-
-
Next varItem
-
-
End With
-
-
rs.Close
-
Set rs = Nothing
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.
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 - Dim rs As Recordset
-
Set rs = CurrentDb.OpenRecordset("tbl", dbOpenDynaset)
-
Dim n As Date
-
-
-
With rs
-
For Each varItem In Me.lstbox.ItemsSelected
-
-
For n = Me.SDate To Me.EDate
-
-
rs.AddNew
-
-
rs!StartDate = n
-
rs!EndDate = n
-
rs!lsboxfld= Me.lsbox.ItemData(varItem)
-
-
rs.Update
-
-
Next n
-
-
Next varItem
-
-
-
End With
-
-
rs.Close
-
Set rs = Nothing
-
-
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
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?
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
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: - Dim Sdate1 As Date
-
Dim dTemp As Date
-
Dim iCount As Integer
-
Dim iItemCount As Integer
-
Dim rs As Recordset
-
-
Sdate1 = Me.sDate
-
iItemCount = 0
-
Set rs = CurrentDb.OpenRecordset("Schedule", dbOpenDynaset)
-
-
For iCount = 1 To Me.lstbx1.ListCount
-
If Me.lstbx1.Selected(iCount) = True Then
-
rs.AddNew
-
dTemp = DateAdd("d", iItemCount, Sdate1)
-
rs!StartDate = dTemp
-
rs!EndDate = dTemp
-
rs!Action = Me.lstbx1.ItemData(iCount)
-
rs.Update
-
iItemCount = iItemCount + 1
-
End If
-
Next iCount
-
-
rs.Close
-
Set rs = Nothing
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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: &" "&&", "&
|
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
|
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...
|
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
| |
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...
|
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()
...
|
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?
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |