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

Populate a subform date control based on main form date range controls

Hello!

On my main form I have two date controls, dtmTrainingStartDate and dtmTrainingEndDate. On a sub form, I have a date control dtmWeekEndingDate. What I would like to do is auto-populate the sub form control with all the Saturday dates between the main form date range controls.

If this is possible, how would you go about doing it?

Thanks!!!
Dave
May 16 '11 #1

✓ answered by TheSmileyCoder

Your attachment is 2007, so I can not open it.

I asked you about what the trigger should be, but you didn't answer. Lets just assume you use a button after filling out the 2 date controls.
Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_DoDates_Click()
  2.   'Rudimentary data check
  3.     If Not IsDate(Me.dtm_StartDate) Or Not IsDate(Me.dtm_endDate) Then
  4.         MsgBox "Valid dates must be entered"
  5.         Exit Sub
  6.     End If
  7.     If Me.dtm_endDate < Me.dtm_StartDate Then
  8.         MsgBox "Enddate must be later then start date"
  9.         Exit Sub
  10.     End If
  11.  
  12.     'Lets assume that your form has the student ID, of type long (autonumber) on the form, in textbox tb_StudentID
  13.         Dim lngStudentID As Long
  14.         lngStudentID = Me.tb_StudentID
  15.  
  16.     'Open a empty recordset for manipulation
  17.         Dim rsPayments As DAO.Recordset
  18.         Set rsPayments = CurrentDb.OpenRecordset("SELECT * FROM tbl_Payments WHERE ID_Student=-1", dbOpenDynaset)
  19.  
  20.     Dim dtRunningDate As Date
  21.     dtRunningDate = Me.dtm_StartDate
  22.     Do While dtRunningDate <= dtmEndDate
  23.         If Weekday(dtRunningDate, vbMonday) = vbSaturday Then
  24.             rsPayments.AddNew
  25.                 rsPayments!ID_Student = lngStudentID
  26.                 rsPayments!dt_Payment = dtRunningDate
  27.             rsPayments.Update
  28.         End If
  29.         dtRunningDate = dtRunningDate + 1
  30.     Loop
  31.     'Cleanup
  32.       Set rsPayments = Nothing
  33.  
  34.     'Requery subform
  35.       Me.[subformControlName].Requery
  36. End Sub

15 3391
TheSmileyCoder
2,322 Expert Mod 2GB
Should there be 1 record for each date?

Or do you want a control like a combobox, where the combobox lists the available dates?

Is the subform meant for selection or for data entry?
May 16 '11 #2
One record for each date. The sub form is going to be for making payments every week. This way, auto-populating should prevent "missing" dates if the user has to manually type in all the dates.
May 16 '11 #3
Any suggestions on how to do this or is it not possible?
May 20 '11 #4
TheSmileyCoder
2,322 Expert Mod 2GB
You could use VBA to loop through all dates in the range, and if they are a saturday add them to a table, with the correct foreign key.

You need to decide what the appropriate trigger should be (I.e. when should the code run) and what should happen if the start/end date gets changed after you have run the append code.
May 20 '11 #5
NeoPa
32,556 Expert Mod 16PB
Dave, you need to appreciate that you don't enter data into forms (even though you may enter some via forms). Forms don't hold data. They are merely windows into the data.

With that understood, it seems you are really asking about populating the table the subform is showing you. This can be done - even within the code of a form or subform, but I suggest you stop a second and think very carefully about exactly what you want doing here before getting into deciding how to go about it. I don't hear the clarity as yet, but when we have that we can determine a way forward.
May 21 '11 #6
Okay, let me try to explain this a little better. I have two tables, tblStudents and tblPayments. The student table contains all the students personal and contract info. The payments table contains all the payment information. The student pays once a week, with the payments due by Saturday. What I am trying to do is insert every Saturday date into that table for each student. With the Saturday dates calculated and inserted, there is less chance staff will miss a payment or enter the wrong date. Also, staff can check a "Break" box to indicate the weeks students will not be making a payment.

Hope this helps!
Attached Files
File Type: zip Contracts.zip (54.4 KB, 94 views)
May 23 '11 #7
TheSmileyCoder
2,322 Expert Mod 2GB
Your attachment is 2007, so I can not open it.

I asked you about what the trigger should be, but you didn't answer. Lets just assume you use a button after filling out the 2 date controls.
Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_DoDates_Click()
  2.   'Rudimentary data check
  3.     If Not IsDate(Me.dtm_StartDate) Or Not IsDate(Me.dtm_endDate) Then
  4.         MsgBox "Valid dates must be entered"
  5.         Exit Sub
  6.     End If
  7.     If Me.dtm_endDate < Me.dtm_StartDate Then
  8.         MsgBox "Enddate must be later then start date"
  9.         Exit Sub
  10.     End If
  11.  
  12.     'Lets assume that your form has the student ID, of type long (autonumber) on the form, in textbox tb_StudentID
  13.         Dim lngStudentID As Long
  14.         lngStudentID = Me.tb_StudentID
  15.  
  16.     'Open a empty recordset for manipulation
  17.         Dim rsPayments As DAO.Recordset
  18.         Set rsPayments = CurrentDb.OpenRecordset("SELECT * FROM tbl_Payments WHERE ID_Student=-1", dbOpenDynaset)
  19.  
  20.     Dim dtRunningDate As Date
  21.     dtRunningDate = Me.dtm_StartDate
  22.     Do While dtRunningDate <= dtmEndDate
  23.         If Weekday(dtRunningDate, vbMonday) = vbSaturday Then
  24.             rsPayments.AddNew
  25.                 rsPayments!ID_Student = lngStudentID
  26.                 rsPayments!dt_Payment = dtRunningDate
  27.             rsPayments.Update
  28.         End If
  29.         dtRunningDate = dtRunningDate + 1
  30.     Loop
  31.     'Cleanup
  32.       Set rsPayments = Nothing
  33.  
  34.     'Requery subform
  35.       Me.[subformControlName].Requery
  36. End Sub
May 23 '11 #8
I'm sorry, yes, a button would initiate the code after the two dates were entered.
May 23 '11 #9
NeoPa
32,556 Expert Mod 16PB
You're still not dealing with the points I raised Dave. If you are going to look at coding this up you will certainly need all the details as to which records need to be created; Will you decide to work on the basis of creating all possible records in one go or will you just add the first relevant record into the form as a template and let the user fill it in? What are the determining factors for which range of dates are to be added (Control Names are important here)?

This is why I suggested you think more clearly about your question. Not because I don't understand what you're after well enough, but because I don't get the impression you do (Obviously I don't have enough information either - but that's not the important point).
May 23 '11 #10
TheSmileyCoder
2,322 Expert Mod 2GB
Did you look over the code I posted for you?
May 25 '11 #11
Yes, I have looked at it. I'm sorry for not getting back quickly, other projects took precedent for a few days.

I am probably what you call a casual user of Access, so its taking a little bit of time to understand the code, but I am working through it! I thank you for posting it!

What I don't understand is what NeoPa is asking me though. I thought i was pretty clear in what I was asking, and I fully understand what I need to do, so its a bit confusing there.

Again, thank you for your reply!
May 25 '11 #12
NeoPa
32,556 Expert Mod 16PB
I'm sure you're fairly happy with the help Smiley is already providing, but as you had the sense to respond indicating your confusion as to my earlier posts, I feel you deserve the courtesy of clarification.

In paragraph #1 of my post #10 there are two questions that I felt needed answering. Now I've reread all your posts again carefully, I can see that there is indication in your post #7 of what your answer would likely be to the first question and in post #1 for the second. Sometimes it hard to work on a question though, when the relevant parts of the question are dotted around in different posts. This is understandable. It's often a building process after all, but it makes answering questions (or requests for information) clearly and precisely all the more important when they come up. In this case, I felt that your original terminology based around your form indicated there was at least a possibility that you might be after a form-based approach rather than a code based one (even after post #7). Unlikely, but an answer would have clarified the situation and helped me to realise that it was an issue you understood.

Sometimes a lack of response can tell us as much as a response can. Sometimes it's hard to know exactly what a member wants (after all we deal here with so many different types from professional to mildly amateur to unwilling participants) so it's very easy to give an answer that doesn't fit the question - even though it might seem to for any casual observer.

If you're keen on a form-based solution after all then the response would revolve around controls and their .DefaultValue properties. If, on the other hand, the batch idea is your preferred approach then I would submit a SQL based suggestion. Rightly or wrongly, from the little I saw in direct response to my posts, I was still unsure of your thinking.

I should add that I understand only too well how complicated it can be responding to multiple experts in the same thread. It's not a bad thing by any means, but some members can find it more complicated to maintain the flow fully.

Anyway, enough rabbiting on my part. I'm here if you want me, but I'm also happy if you decide that working with Smiley is an altogether easier proposition. Best wishes either way.
May 25 '11 #13
Thanks for your post, NeoPa. I do fully understand how hard it is trying to decipher the questions asked sometimes.

Since I am fairly new to Access (only used for about 2 years off and on), it takes me a bit to go through the code and completely understand what it is doing. I'm not one to just copy/paste code in, I like to understand what it is doing and why its there.

Once I get through it, and if I have more questions, I will reply back.

Thanks so much for all the help so far!
May 26 '11 #14
Sorry about the delay on this, but the code works perfectly!

Thanks Smiley!!!!
Jul 25 '11 #15
NeoPa
32,556 Expert Mod 16PB
Glad to hear it all resolved in the end :-)
Jul 25 '11 #16

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

Similar topics

2
by: chris vettese | last post by:
I've been having trouble with this for a long time. I have a form that has many subforms. In the subforms I have unbound text boxes in the footer that sum a field. I want to Display this sum on...
5
by: Brian Link | last post by:
The design of my app is such that I have a main form which loads a variety of user controls. Sometimes these controls will contain their own constituent sub-controls. The main form should...
3
by: Richard Lewis Haggard | last post by:
I have a form that has a label. This form also has a tab control. The tab control has a user control pasted on its page. Under some conditions, the user control needs to send status strings back to...
9
by: freeskier | last post by:
I am working on a form to edit student field placements. the student is selected from a combo box (cboStudent) which then populates a list box(lstPlacements) with field placements for that student....
2
by: rlamber | last post by:
Hello, I have 2 subforms in a Main form, one links fine, the other doesn't. I am trying to link them both to a "tracking ID" on the main form. The Tracking ID is a text field that is a combination...
5
JustJim
by: JustJim | last post by:
I must have Friday-itis! I have a main form which has a sub-form. The sub form is shown as a datasheet, but in design view I have a control on the Form Footer which totals a field on the Detail...
1
by: Moish | last post by:
I have a form that does not have any bound fields. I want to open a subform on that form to a specific record based on the value of an unbound field on the main form. Obviously the subform is not...
24
by: MNNovice | last post by:
I am trying to display a value from the sub form on to the main form. It's not a calculated data, just plain text. This is what I attempted without success. 1. Created a text box called...
1
by: dekk | last post by:
Hi, I have a main form ("frm_TimeParent") and a subform ("frm_TimeChild"). Issue: How do I refresh the combo box values in the subform once the contract has been selected in the main form? ...
4
sueb
by: sueb | last post by:
ADezii helped me with this once, but I must have messed it up somehow and can't figure it out again! All my forms are opened either in Dynaset or Snapshot mode, depending on whether the user has...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.