I have this code under a Timer's tick event to refresh the value of a DateTimePicker after everyday/at 7:19 (tTmNow)
`Private Sub tmrNewDay_Tick(sender As Object, e As EventArgs) Handles tmrNewDay.Tick
If tTmNow.Text.Contains("7:19:") Then 'at this time pick yesterday's date
Dim ystd As String
ystd = Today.AddDays(-1) 'yesterday
dtPicker1.Value = ystd 'put yesterday's date into the date time picker
End If
End Sub`
Unfortunately, at the first run of the program, I achieve my aim successfully. But the second day (The program is to run everyday and pick yesterday's date), when picking the date, it adds the time; giving 19-04-2017 00:00:00, instead of 19-04-2017. Is there a way to refresh the contents of the DateTimePicker or is there an entirely new way of picking yesterday's date, day after day.
Related
I want to create a button which will do the following.
- Retrieve the current date and time -->
- This date and time plus the time in (O2) -->
- In the range of cells (L2:L11) needs to be displayed if the current time plus the time in cell O2 is less or more than the date and time in the cell next to it (Range M2:M11). The cell L2 will be compared with M2, L3 with M3 etc.
On any current time, you will be able to click on the button and it will calculate if it is on time or too late.
I know how to get the current date but can't get any further.
Click here for the image with the cells
Sub OnTime()
With Range("L2:L11")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
End Sub
Why don't you simply do this
In cell O2 type "=now"
in cell L2 type "=IF(B1<D1,"Ontime", "Late")"
After that each time you refresh the formulaes it will be recalculated. Do that by linking this macro to a button
Sub Refresh_formulaes()
Calculate
End Sub
Everytime I double click the datetimepicker, it automatically send its values to the textbox. Now, I want to send the value of DateTimePicker in the Textbox, only when I click the date, specific days or year of the month?
Here's my vb.net code:
Private Sub DateTimePicker1_DOB_CloseUp(sender As Object, e As EventArgs) Handles DateTimePicker1_DOB.CloseUp
TextBox1_Date.Text = DateTimePicker1_DOB.Text
End Sub
I need to determine the day of week that someone turns 21. I have tried so many different ways to do this, I can get the age, the day of week a person was born but not the day they turned 21, this is my current code. Yes this is homework but I have worked a few hours on this and can't figure it out,
Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click
Dim dob As Date = txtDay.Text
Dim age, days As Double
days = DateDiff(DateInterval.Day, dob, Today) / 365
age = Fix(days)
txtWeek.Text = age.DayOfWeek.ToString()
End Sub
First of all, think of how you would solve this problem in real life, then try to write a program that would go about it in the same manner.
So, first of all, I would find out what day is the subject's 21st birthday by simply adding 21 years to his/her date of birth. This is really simple in Visual Basic because of the AddYears function.
Dim _21BirthDay = dob.AddYears(21)
Then, I would look at a calendar and find out what day of the week that was.
Dim WeekDayOf_21BirthDay = _21BirthDay.DayOfWeek
Then, I could use the WeekDayOf_21BirthDay variable to show the user.
I need to auto fill a specific column with a date. If the date occurs between Saturday through Tuesday, I need to auto fill the date column with the previous Friday. If the date occurs between Wednesday and Friday I need to put in the coming Friday date for the auto fill.
For example: If I run the spreadsheet on Tuesday 10/23 I need the auto fill date to be Friday 10/19. If I run the spreadsheet on Wednesday 10/24 I need the auto fill date to be Friday 10/26.
Here is the formula I have so far, so I need to invoke this formula via macro when saving the spreadsheet or by clicking a custom button. Any assistance would be greatly appreciated.
=IF(ISBLANK($A2),"",IF(WEEKDAY(TODAY())<4,(TODAY()+(-1-WEEKDAY(TODAY()))),(TODAY()+(6-WEEKDAY(TODAY())))))
I'd suggest using VBA for what you want, something like this which would go in the ThisWorkbook module and run prior to the workbook saving:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Check if A2 is blank
If Cells(2, 1) = "" Then Exit Sub
'Find the date and use it
Dim x As Long
Dim dateToUse As Date
x = Weekday(Date, vbSaturday)
If x <= 4 Then
dateToUse = Date - x
Else
dateToUse = Date + (7 - x)
End If
'Change Cells(1, 1) to the actual target you want to have the date,
'which could be determined based upon the contents of your workbook/sheet.
Cells(1, 1) = dateToUse
End Sub
If a booking is present in database on 12/27/2011 at 5:00 pm for 2 hours and i try to make a new booking on 12/27/2011 between 5 to 7 pm then my code generates an error message. Even if i try to book at 4:00 pm for 2 hours it generates an error message as the 2nd hour will be overlapped here with already made booking from 5:00 to 7:00.
Now here comes the problem part. When day changes it doesn't generate error message i.e. IF a booking is there on 12/27/2011 at 11:00 pm for 3 hours then it should not allow new booking till 12/28/2011 2:00 am but when i try to book 12/28/2011 at 1:00 am it saves it in the database and doesn't generate an error message. I want an error message generated in such a case.
I am using two separate fields in database one for time and one for date. Both of them have DateTime datatype.
newtime refers to time on which i'm trying to make new booking
addednewtime refers to time after adding the duration to time on which i'm trying to make new booking
addeddbtime contains just time value (after adding duration for booking in database) extracted from datetime field stored in database
newdate refers to the next date as the day changes at 12:00 am so if database booking is at 11:00 pm on 12/12/2011 then new date will have 12/13/2011 in it
Problem lies in the last part of the If condition which checks overlapped bookings when a booking spans over two days
Here is my code:
Dim newtime, addednewtime, addeddbtime, changetime, newdate As DateTime 'defines variables
addeddbtime = dbonlytime.AddHours(dbdur)
newtime = TxtBookTime.Text
addednewtime = newtime.AddHours(TxtBookDur.Text)
changetime = "12:00:00 AM"
newdate = dbdate.AddDays(1)
If TxtBookDate.Text = dbdate And TxtPoolNo.Text = dbpoolno And TxtCrtNo.Text = dbcrtno And TxtTblNo.Text = dbtblno And CmboGame.SelectedItem = dbgame And ((newtime > dbonlytime And newtime < addeddbtime) Or (addednewtime > dbonlytime And addednewtime < addeddbtime) Or (newtime < dbonlytime And addeddbtime > changetime And addednewtime < dbonlytime And addednewtime <= addeddbtime And TxtBookDate.Text = newdate)) Then
MessageBox.Show("The date and time you have entered has already been booked " & vbCrLf & " Try Again!", "Bookings Overlapped", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Exit Sub
End If
Want you want to do is have a routine which checks for overlappings between the current booking and existing ones. You should have a method for this, with the following input values:
Current booking dates
List of existing bookings
Let's say you have a class called Booking which has begin and end DateTimes (no need to have separate fields for date and time, a DateTime object contains both).
Public Class Booking
Public Sub New(ByVal beginDate As DateTime, ByVal endDate As DateTime)
Me.BeginDate = beginDate
Me.EndDate = endDate
End Sub
Public Property BeginDate As Date
Public Property EndDate As Date
'Other booking properties here
End Class
You could have a routine like this which checks if there is an overlapping with an existing booking:
Private Function BookingOverlapping(ByVal booking As Booking, ByVal existingBookings As IEnumerable(Of Booking)) As Boolean
For Each existingBooking In existingBookings
If booking.BeginDate < existingBooking.EndDate AndAlso booking.EndDate > existingBooking.BeginDate Then
Return True
End If
Next
Return False
End Function
Then you would use the method like this:
' Figure out begin and end DateTimes from user input,
' then create a booking object.
Dim currentBooking As New Booking(beginDate, endDate)
' The GetExistingBookings method would retrieve bookings from the database
' and add new Booking objects to a list.
Dim existingBookings As List(Of Booking) = GetExistingBookings()
If BookingOverlapping(currentBooking, existingBookings)
MessageBox.Show("The date and time you have entered has already been booked " & vbCrLf & " Try Again!", "Bookings Overlapped", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End If