[RESOLVED] Calculating Date from day's Number and Name
Hi,
I am using Visual basic 6. There are one combo box and 5 check boxex on a form. User selects the day from combo box (Sunday, Monday... to Saturday) and click on Check1,Check2 boxes.
I just want to set the tool tip with date of these 5 check boxes, so that at the time of clicking he/she can know that what will be the date of Check<N> of that day.
Let me explain again please.
Suppose user selected Wednesday and going to click 2nd Check Box, so at this time tool tip show 09/11/2011 date, if he/she clicks 3rd then 16/11/2011; means Nth Check box + Combo1.Text's Day should return the date of either current month and/or date may be a parameter (dtpicker).
Please let me know, i am clear in my request or not.
Thank you.
Regards
Girish Sharma
Re: Calculating Date from day's Number and Name
Lookup the DateAdd funtion. Here is a quote from MSDN:
Quote:
DateAdd Function
Returns a Variant (Date) containing a date to which a specified time interval has been added.
Syntax
DateAdd(interval, number, date)
The DateAdd function syntax has thesenamed arguments:
Part Description
interval Required.String expression that is the interval of time you want to add.
number Required.Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).
date Required. Variant (Date) or literal representing date to which the interval is added.
Settings
The intervalargument has these settings:
Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second
Remarks
You can use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now.
To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").
The DateAdd function won't return an invalid date. The following example adds one month to January 31:
DateAdd("m", 1, "31-Jan-95")
In this case, DateAdd returns 28-Feb-95, not 31-Feb-95. If date is 31-Jan-96, it returns 29-Feb-96 because 1996 is a leap year.
If the calculated date would precede the year 100 (that is, you subtract more years than are in date), an error occurs.
If number isn't aLong value, it is rounded to the nearest whole number before being evaluated.
Note The format of the return value for DateAdd is determined by Control Panel settings, not by the format that is passed in date argument.
Re: Calculating Date from day's Number and Name
Thanks for your reply. But it is not working. When is says :
Check1.ToolTipText = Format(DateAdd("w", Combo1.ListIndex, Date), "dd/mm/yyyy") It is showing me :
If i select Showing value
Sunday 10/11/2011
Monday 11/11/2011
....
Saturday 16/11/2011
I wish :
Sunday 06/11/2011 Because Combo1.txt is Sunday and its is 1st check box; means 1st Sunday of Nov. 2011 is 06/11/2011. If i put mouse on 2nd check box then it should be 13/11/2011.
How do i achieve this, please guide me.
Thank you.
Re: Calculating Date from day's Number and Name
That's because you're useing Date function which returns current date - instead you need to have intial date set into a variable and use that instead.
Also, if you want to add a week then seeting value is "ww" and you're using "w".
Try this:
Code:
Option Explicit
Dim dt As Date
Private Sub Form_Load()
dt = CDate("November 06, 2011")
Debug.Print dt
End Sub
Private Sub Combo1_Click()
Check1.ToolTipText = Format(DateAdd("ww", 1, dt + Combo1.ListIndex), "dd/mm/yyyy")
End Sub
Re: Calculating Date from day's Number and Name
This is my take on your question. For ease I'm using a checkbox array instead of individual controls.
Code:
Private Sub cboDays_Click()
Dim dteTemp As Date
Dim intMonth As Integer
Dim intYear As Integer
Dim i As Integer
'find the first selected day of the month
intMonth = Month(Date)
intYear = Year(Date)
dteTemp = DateSerial(intYear, intMonth, 1)
Do While Format(dteTemp, "dddd") <> cboDays.Text
dteTemp = DateAdd("d", 1, dteTemp)
Loop
' Add tooltips to the checkboxes
For i = 1 To 5
chkWeek(i - 1).ToolTipText = DateAdd("ww", i - 1, dteTemp)
Next i
End Sub
Private Sub Form_Load()
Dim i As Integer
cboDays.AddItem "Sunday"
cboDays.AddItem "Monday"
cboDays.AddItem "Tuesday"
cboDays.AddItem "Wednesday"
cboDays.AddItem "Thursday"
cboDays.AddItem "Friday"
cboDays.AddItem "Saturday"
For i = 1 To 5
chkWeek(i - 1).Caption = "Week " & i
Next i
End Sub
Re: Calculating Date from day's Number and Name
Thanks again for your reply and interest in my question.
No Sir, this not i want or probably either 1.I am not able to elaborate you my problem 2.I am not understanding your help.
Let me reexplain my need please.
Check1.ToolTipText = should be Date of, Combo1.Text (Sunday,Monday...Sat) + Number of Nth Day of Combo1.Text + Check<N>.
Suppose user selected Wednesday and now going to click on 3rd check box, then its tooltip should be 16/11/2011, because its 3rd check box and Wednesday means 3rd Wednesday of Nov 2011 is 16/11/2011.
Thank you again with Regards
Girish Sharma
Re: Calculating Date from day's Number and Name
Thank you MarkT. You got what i want and give what i was looking for.
Thank you once again. I am new/less user to this forum, so just searching how to i mark as RESOLVED it. (Probably looking in FAQs)
Thanks once again to you and all who helped and give their valuable time to my problem.
Best Regards
Girish Sharma
Quote:
Originally Posted by
MarkT
This is my take on your question. For ease I'm using a checkbox array instead of individual controls.
Code:
Private Sub cboDays_Click()
Dim dteTemp As Date
Dim intMonth As Integer
Dim intYear As Integer
Dim i As Integer
'find the first selected day of the month
intMonth = Month(Date)
intYear = Year(Date)
dteTemp = DateSerial(intYear, intMonth, 1)
Do While Format(dteTemp, "dddd") <> cboDays.Text
dteTemp = DateAdd("d", 1, dteTemp)
Loop
' Add tooltips to the checkboxes
For i = 1 To 5
chkWeek(i - 1).ToolTipText = DateAdd("ww", i - 1, dteTemp)
Next i
End Sub
Private Sub Form_Load()
Dim i As Integer
cboDays.AddItem "Sunday"
cboDays.AddItem "Monday"
cboDays.AddItem "Tuesday"
cboDays.AddItem "Wednesday"
cboDays.AddItem "Thursday"
cboDays.AddItem "Friday"
cboDays.AddItem "Saturday"
For i = 1 To 5
chkWeek(i - 1).Caption = "Week " & i
Next i
End Sub
Re: Calculating Date from day's Number and Name
Is there a reason why you don't set the checkbox caption instead of using the tool tip? To me it would seem more user friendly to see the date without having to hover over the control.
Re: Calculating Date from day's Number and Name
Because caption occupies space on the form and form is already full of different controls, so i think tooltiptext is the best here.
On other form, i will follow your suggestion for sure. Thank you for your great cooperation in MS programming even in 2011.
Regards
Girish Sharma
Quote:
Originally Posted by
MarkT
Is there a reason why you don't set the checkbox caption instead of using the tool tip? To me it would seem more user friendly to see the date without having to hover over the control.