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

MS Access Calendar Populate from query with calculated field

I'm trying to populate the ms access caledar from a query with a calculated field here s the code from the query
Expand|Select|Wrap|Line Numbers
  1. SELECT HPContacts.FirstName,   HPContacts.LastName, HPContacts.LastContactDate+[DaysBeforeNextContact] AS [Next Contact]
  2. FROM HPContacts
  3. WHERE ((([HPContacts].[LastContactDate]+[DaysBeforeNextContact]) Is Not Null))
  4. ORDER BY HPContacts.LastContactDate+[DaysBeforeNextContact];
I’ve not had any success getting calendar to populate from query (I’m just learning sql coding)
Any help would be appreciated

PS. Using Access 2003 on a Windows 7 machine.
Feb 19 '12 #1

✓ answered by ADezii

You picked a rather difficult Project for just learning VBA and SQL Coding, but we will give it a shot. The following changes to the PopulateCalendar() Sub-Routine will display the Last Name, First Initial of the First Name, as well as the Next Contract Date for each Contract Date, sorted by Last Name, First Name. Because of the complexity of this Procedure, I only listed a relevant portion of the Code and also Attached a Demo DB that I created for you. Pay special attention to Code Lines 3 to 9, 16, and 20. Any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Set db = CurrentDb
  2.  
  3. strSQL = "SELECT HP.FirstName, HP.LastName, HP.LastContractDate, " & _
  4.          "HP.[DaysBeforeNextContact] " & _
  5.          "FROM HPContacts AS HP WHERE [HP].[LastContractDate] is Not Null " & _
  6.          "AND [HP].[LastContractDate] BETWEEN %F AND %L " & _
  7.          "ORDER BY HP.LastName, HP.FirstName;"
  8. strSQL = Replace(strSQL, "%F", lngFirstOfMonth)
  9. strSQL = Replace(strSQL, "%L", lngLastOfMonth)
  10.  
  11. Set rstEvents = db.OpenRecordset(strSQL)        'Added 4/16/2008
  12.  
  13. With rstEvents
  14.   Do While Not .EOF
  15.     'CFB added 2-18-10
  16.     lngFirstDateInRange = ![LastContractDate]
  17.     If lngFirstDateInRange < lngFirstOfMonth Then
  18.       lngFirstDateInRange = lngFirstOfMonth
  19.     End If
  20.     lngLastDateInRange = ![LastContractDate]
  21.     If lngLastDateInRange > lngLastOfMonth Then
  22.       lngLastDateInRange = lngLastOfMonth
  23.     End If
  24.  
  25.     For lngEachDateInRange = lngFirstDateInRange To lngLastDateInRange
  26.       bytEventDayOfMonth = (lngEachDateInRange - lngLastOfPreviousMonth)
  27.       bytBlockCounter = bytEventDayOfMonth + bytBlankBlocksBefore
  28.         If astrCalendarBlocks(bytBlockCounter) = "" Then      'no existing Text in Array
  29.           astrCalendarBlocks(bytBlockCounter) = ![LastName] & ", " & Left$(![FirstName], 1) & "." & _
  30.                                                 " - [" & DateAdd("d", ![DaysBeforeNextContact], ![LastContractDate]) & "]"
  31.         Else
  32.           astrCalendarBlocks(bytBlockCounter) = astrCalendarBlocks(bytBlockCounter) & vbNewLine & _
  33.                                                 ![LastName] & ", " & Left$(![FirstName], 1) & "." & _
  34.                                                 " - [" & DateAdd("d", ![DaysBeforeNextContact], ![LastContractDate]) & "]"
  35.       End If
  36.     Next lngEachDateInRange
  37.     'End of CFB added 2-18-10
  38.       .MoveNext
  39. Loop
  40. End With
P.S. - I've also disabled the Events Listbox at the bottom of the Form since I felt that there was no need for it.

9 2764
ADezii
8,834 Expert 8TB
You picked a rather difficult Project for just learning VBA and SQL Coding, but we will give it a shot. The following changes to the PopulateCalendar() Sub-Routine will display the Last Name, First Initial of the First Name, as well as the Next Contract Date for each Contract Date, sorted by Last Name, First Name. Because of the complexity of this Procedure, I only listed a relevant portion of the Code and also Attached a Demo DB that I created for you. Pay special attention to Code Lines 3 to 9, 16, and 20. Any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Set db = CurrentDb
  2.  
  3. strSQL = "SELECT HP.FirstName, HP.LastName, HP.LastContractDate, " & _
  4.          "HP.[DaysBeforeNextContact] " & _
  5.          "FROM HPContacts AS HP WHERE [HP].[LastContractDate] is Not Null " & _
  6.          "AND [HP].[LastContractDate] BETWEEN %F AND %L " & _
  7.          "ORDER BY HP.LastName, HP.FirstName;"
  8. strSQL = Replace(strSQL, "%F", lngFirstOfMonth)
  9. strSQL = Replace(strSQL, "%L", lngLastOfMonth)
  10.  
  11. Set rstEvents = db.OpenRecordset(strSQL)        'Added 4/16/2008
  12.  
  13. With rstEvents
  14.   Do While Not .EOF
  15.     'CFB added 2-18-10
  16.     lngFirstDateInRange = ![LastContractDate]
  17.     If lngFirstDateInRange < lngFirstOfMonth Then
  18.       lngFirstDateInRange = lngFirstOfMonth
  19.     End If
  20.     lngLastDateInRange = ![LastContractDate]
  21.     If lngLastDateInRange > lngLastOfMonth Then
  22.       lngLastDateInRange = lngLastOfMonth
  23.     End If
  24.  
  25.     For lngEachDateInRange = lngFirstDateInRange To lngLastDateInRange
  26.       bytEventDayOfMonth = (lngEachDateInRange - lngLastOfPreviousMonth)
  27.       bytBlockCounter = bytEventDayOfMonth + bytBlankBlocksBefore
  28.         If astrCalendarBlocks(bytBlockCounter) = "" Then      'no existing Text in Array
  29.           astrCalendarBlocks(bytBlockCounter) = ![LastName] & ", " & Left$(![FirstName], 1) & "." & _
  30.                                                 " - [" & DateAdd("d", ![DaysBeforeNextContact], ![LastContractDate]) & "]"
  31.         Else
  32.           astrCalendarBlocks(bytBlockCounter) = astrCalendarBlocks(bytBlockCounter) & vbNewLine & _
  33.                                                 ![LastName] & ", " & Left$(![FirstName], 1) & "." & _
  34.                                                 " - [" & DateAdd("d", ![DaysBeforeNextContact], ![LastContractDate]) & "]"
  35.       End If
  36.     Next lngEachDateInRange
  37.     'End of CFB added 2-18-10
  38.       .MoveNext
  39. Loop
  40. End With
P.S. - I've also disabled the Events Listbox at the bottom of the Form since I felt that there was no need for it.
Attached Files
File Type: zip Rstruthers_Calendar.zip (61.4 KB, 146 views)
Feb 19 '12 #2
Thank You ADezii
I will work on DB this evening and let you know tomorrow how I’m progressing
Again thanks for the quick response
Ron
Feb 19 '12 #3
ADezii
8,834 Expert 8TB
P.S. - I forgot to mention that Code Line Numbers 28 to 35 control what Text is actually displayed in the Calendar for a given Date.
Feb 20 '12 #4
ADezii
I should have given a better detail on my first post. I would like just the first and last name to populate on the appropriate date when prospect should be contacted next (last contact date + Days Before Next Contact) I created a query (NextContact) that calculates the date the contact would populate on the calendar, I also changed the code in the populate calendar section to [LastContactDate] from [LastContractDate] this change has not changed how calendar runs. Also in the select statement can you use HP.LastName in lieu of HPContacts.Lastname if you have other tables that start with HP?
Here is the zipped file (Rstruthers_Calendar2) with added query
Ron
Attached Files
File Type: zip Rstruthers_Calendar2.zip (106.2 KB, 112 views)
Feb 20 '12 #5
ADezii
8,834 Expert 8TB
The following Revised Code, will map the Contract Date + Days Before Next Contract to the Calendar, listing the Last Name, First Names. I did not have the time to Optimize it, and a Query with a Calculated Field is not needed to produce the desired results.
Expand|Select|Wrap|Line Numbers
  1. Set db = CurrentDb
  2.  
  3. strSQL = "SELECT HP.FirstName, HP.LastName, HP.LastContractDate, " & _
  4.          "HP.[DaysBeforeNextContact] " & _
  5.          "FROM HPContacts AS HP WHERE [HP].[LastContractDate] is Not Null " & _
  6.          "AND ([LastContractDate]+[DaysBeforeNextContact]) BETWEEN %F AND %L " & _
  7.          "ORDER BY HP.LastName, HP.FirstName;"
  8. strSQL = Replace(strSQL, "%F", lngFirstOfMonth)
  9. strSQL = Replace(strSQL, "%L", lngLastOfMonth)
  10.  
  11. Set rstEvents = db.OpenRecordset(strSQL)        'Added 4/16/2008
  12.  
  13. With rstEvents
  14.   Do While Not .EOF
  15.     'CFB added 2-18-10
  16.     lngFirstDateInRange = (![LastContractDate] + ![DaysBeforeNextContact])
  17.     If lngFirstDateInRange < lngFirstOfMonth Then
  18.       lngFirstDateInRange = lngFirstOfMonth
  19.     End If
  20.     lngLastDateInRange = (![LastContractDate] + ![DaysBeforeNextContact])
  21.     If lngLastDateInRange > lngLastOfMonth Then
  22.       lngLastDateInRange = lngLastOfMonth
  23.     End If
  24.  
  25.     For lngEachDateInRange = lngFirstDateInRange To lngLastDateInRange
  26.       bytEventDayOfMonth = (lngEachDateInRange - lngLastOfPreviousMonth)
  27.       bytBlockCounter = bytEventDayOfMonth + bytBlankBlocksBefore
  28.         If astrCalendarBlocks(bytBlockCounter) = "" Then      'no existing Text in Array
  29.           astrCalendarBlocks(bytBlockCounter) = ![LastName] & ", " & ![FirstName]
  30.         Else
  31.           astrCalendarBlocks(bytBlockCounter) = astrCalendarBlocks(bytBlockCounter) & vbNewLine & _
  32.                                                 ![LastName] & ", " & ![FirstName]
  33.       End If
  34.     Next lngEachDateInRange
  35.     'End of CFB added 2-18-10
  36.       .MoveNext
  37. Loop
  38. End With
Feb 20 '12 #6
ADezii
Thank You
For your time!! MUCH APPRECIATED
I made the code changes and copied calendar into DB working great!!!
I took a look at the widescreen version liked the scroll bar and the list box futures will try to code wide version to use in DB will let you know how successful I was
Again thanks for your help
Ron
Feb 21 '12 #7
ADezii
8,834 Expert 8TB
@rstruthers:
You are quite welcome, and we're here should you need us. You should pat yourself on the back, for someone who is just 'learning SQL Coding', you tackled a difficult Project with little difficulty at all.
Feb 21 '12 #8
I have the widescreen calendar working as well. While poking around in the code I saw the disabled double click code for each block, I have activated some blocks (ones with data) to play with, I changed the code to open a filtered (only names in that block) form with a command button to do a focused open of a form called call log. This works well but seems a little convoluted.

So here are my questions

1. Anyway of making block entries click aware? If so one could just click on a name to open the call log form. My gut tells me this might be out of the reach of this calendar form.

2. The other way might be to have a temporary command button coded with each block entry place to the right of the entry to load the call log form.

3. Or just go with the two step rout as described above to get the call log form.

Please give me your feed back and coding suggestion if any.

Thanks Ron
Feb 22 '12 #9
ADezii
8,834 Expert 8TB
Another undocumented, vital feature of the Access Calendar is that the actual Date corresponding to each Text Box Control (42) is contained within its Tag Property. For February 2012, txtDayBlock07 correlates to February 4, 2012. Placing the following Code in the Dbl-Click Event of this single Text Box returns the actual Date of 2/4/2012. I use this functionality, in the Dbl-Click() Event of all 42 Text Boxes, to sometimes Open Forms Filtered to a specific Date. This in essence make the Text Boxes 'Date Aware'.
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDayBlock07_DblClick(Cancel As Integer)
  2.   MsgBox CDate(Me![txtDayBlock07].Tag)
  3. End Sub
Feb 23 '12 #10

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

Similar topics

2
by: Manfred | last post by:
Hi Would like to add a new Field to Table which is 75 % (or another Percentage) higher than the Value in another Field in same Table.Is this possible with Expression or would I have to enter the...
3
by: Bill Clark | last post by:
I have about 20,000 records pulled from Excel that I need to update. What I need to do is run an update query that bascially says: If a field is null, update it with the previous record value of...
5
by: Richard Holliingsworth | last post by:
Hello: Thanks for reading this post. I need to create a metrics (form or report - I don't care which) to display calculated fields about the database (A2002 front end to SQL Server 2K) 1) I...
2
by: hhathome | last post by:
In my report I have a calculated DateDiff Field, I also have a calculated count field. I'm trying to get a total of the DateDiff field and dividing it by the count field and I'm having problems --...
2
by: John | last post by:
I am using Access 2000. One table in my database has a field called RankName. Values inlcude: Officer, Sergeant, Lieutenant. I need create a report that groups these three RankNames into two...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
2
by: Cardinal | last post by:
I'm trying to create a calculation in a query in Access 2003 and not having much success. Basically, I want it to look at two fields and place the lower amount of the two fields into the calculated...
3
by: myemail.an | last post by:
I use Access 2007 and create queries with a number of calculated fields/expressions (I'm still a novice so please forgive me if my wording is imprecise...), like: MyCalculation = Field1 - Field2. ...
2
by: mkbrady | last post by:
I have a query that includes calculated fields that generate numeric results. I have wrapped conversion functions CLng() and CDdl() around the calculated fields to ensure the data types are...
3
by: Daryl Austin | last post by:
I have created a mileage report in access. I created a calculated field called "mileage value" that takes field Mileage x field IRSRate. Each is for a given date field in a table. This works fine...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.