How to change status based on date in another text box on form - Access - vba

Im making a training database.
Im looking to change a textbox value on a form based on a date in another textbox.
I have the following:
Refresher Period - TxtRef
Participation Date - TxtPart
Refresher Date - TxtRefDate
Status - TxtStatus
I want status to update to either - In Date, Expired or Expiring based on the following rules applying to the date in TxtRefDate.
Value < Now() + 60 "Expiring"
Value < Now() "Expired"
Value > Now() + 60 "In Date"

Create a small helper function:
Public Function Status(ByVal RefDate As Date) As String
Dim Description As String
Select Case DateDiff("d", Date, RefDate)
Case > 60
Description = "In date"
Case > 0
Description = "Expiring"
Case Else
Description = "Expired"
End Select
Status = Description
End Function
Now, set the ControlSource of txtStatus to:
=Status([TxtRefDate])

I think you're looking for something like that ?
If TxtRefDate.Value < Now() Then
TxtStatus.Value = "Expired"
Else:
If TxtRefDate.Value < Now() + 60 Then
TxtStatus.Value = "Expiring"
Else:
TxtStatus.Value = "In Date"
End If
End If

Related

How to compare only month and year? [VB]

Its quite simple, i just want to compare two dates using month and year, if the input date (mont and year only) are above or below that current date (month and year).
The problem is , when i compare two strings
Dim dDate as DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
MessageBox.Show("check date.")
Else
txtBox.Text = dDate.ToString("MM/yyyy")
end If
IF dDate.ToString("MM/yyyy") < DateTime.Now.ToString("MM/yyyy")
MessageBox.Show("Below") ' Problem: 03/2024 saying is below than 08/2019
Else
MessageBox.Show("Above")
End If
Any help?
UPDATE
I CHANGED THE CASE TO
If (dDate.Month AndAlso dDate.Year) < (DateTime.Now.Month AndAlso DateTime.Now.Year) Then
'input: 07/2019
'date expired
Else
'the problem is here
'saying 07/2019 is not < than 08/2019
End If
I'd avoid using strings.
Dim dDate As DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
'bad date
MessageBox.Show("check date.")
Else
Select Case dDate.Year
Case Is < DateTime.Now.Year
MessageBox.Show("Below")
Case Is > DateTime.Now.Year
MessageBox.Show("Above")
Case Else 'years are equal,check month
Select Case dDate.Month
Case Is < DateTime.Now.Month
MessageBox.Show("Below")
Case Is > DateTime.Now.Month
MessageBox.Show("Above")
Case Else 'equal months
MessageBox.Show("SAME") '????
End Select
End Select
End If
Using date values is probably best, but if string comparisons must be made.
Dim dDate as DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
MessageBox.Show("check date.")
Else
txtBox.Text = dDate.ToString("yyyyMM")
End If
If dDate.ToString("yyyyMM") < DateTime.Now.ToString("yyyyMM") Then
MessageBox.Show("Below")
ElseIf dDate.ToString("yyyyMM") > DateTime.Now.ToString("yyyyMM") Then
MessageBox.Show("Above")
Else
MessageBox.Show("Same")
End If
Thank you everyone, but the final solutions is made with
If (dDate.Year < DateTime.Now.Year Or (dDate.Year = DateTime.Now.Year And dDate.Month < DateTime.Now.Month)) Then
'something
Else
'something
End If
Convert the year and month to Integer values, which are easy to compare less-than or greater-than. For example, if you had two DateTimePicker controls on a form, named dtpDate1 and dptDate2, and a button btnTest:
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim date1 As Integer = (dtpDate1.Value.Year * 100) + dtpDate1.Value.Month
Dim date2 As Integer = (dtpDate2.Value.Year * 100) + dtpDate2.Value.Month
MsgBox("Date1 = " & date1.ToString & vbCrLf & "Date2 = " & date2.ToString)
If date1 = date2 Then MsgBox("Equal!")
If date1 < date2 Then MsgBox("date1 is less than date2")
If date1 > date2 Then MsgBox("date1 is greater than date2")
End Sub
The dates are converted to integers in a YYYYMM format. This is a data warehouse technique to convert dates to integers for better query performance when date is used often in a WHERE clause.

Is Null return ""

I have the following code however if the RefDate is blank it is returning #Type!. Can I add in a line to say ifNull = ""
Public Function Status(ByVal RefDate As Date) As String
Dim Description As String
Select Case DateDiff("d", Date, RefDate)
Case > 60
Description = "In date"
Case > 0
Description = "Expiring"
Case Else
Description = "Expired"
End Select
Status = Description
End Function
If Null return "" nothing or blank
Try this, I've changed the input data type also
Public Function Status(ByVal RefDate As Variant) As String
Dim Description As String
If Len(RefDate) > 0 and IsDate(RefDate) Then
Select Case DateDiff("d", Date, RefDate)
Case Is > 60
Status = "In date"
Case Is > 0
Status = "Expiring"
Case Else
Status = "Expired"
End Select
Else
Status = "No date"
End If
End Function
Trying a similar approach, when comparing with #Ryan Wildry, but now explicitly setting "" to Description
Public Function Status(ByVal RefDate As Date) As String
Dim Description As String
If CInt(RefDate) = 0 Then
Description = ""
Else
Select Case DateDiff("d", Date, RefDate)
Case Is > 60
Description = "In date"
Case Is > 0
Description = "Expiring"
Case Else
Description = "Expired"
End Select
End If
Status = Description
End Function

how to compare 2 datetimepicker vb.net

I'd like to compare 2 DateTimePicker's value
Dim dd1 As Date, dd2 As Date
Dim diff As Integer
dd1 = DateTimePicker1.Value
dd2 = DateTimePicker2.Value
diff = DateDiff("d", dd1, dd2)
If diff > 0 Then
MsgBox("datetimpicker1datetimepicker2")
End If
but it doesn't work could you help me please
Another way to compare would be to use the DateTime.Compare function. For a simple "Are they the same?" you could try:
If Not DateTime.Compare(dd1,dd2) = 0 then
'they are diffent
End If
Or if you want to be more specific then you might go with something like this:
If Not DateTime.Compare(dd1,dd2) = 0 then
'they are same
ElseIf DateTime.Compare(dd1,dd2) > 0 then
'dd1 is later than dd2
Else
'dd1 is prior to dd2
End If
You can see more about DateTime.Comare here on MSDN
If You are using DateDiff() then there are three case can be possible:-
1) DatePicker1 date is bigger than DatePicker2
2) DatePicker1 date is lower than DatePicker2
3) DatePicker1 date is equal to DatePicker2
To handle this, You have to use this
diff = DateDiff("d", dd1, dd2)
If diff > 0 Then
MsgBox("datetimpicker1 is greater than datetimepicker2")
Else If diff < 0 Then
MsgBox("datetimpicker1 is lesser than datetimepicker2")
Else
MsgBox("datetimpicker1 is equal to datetimepicker2")
End If

Count of days that have at least 1 job open using SQL

I have a MS Access table that has a list of jobs that were executed (Job ID, StartDate, EndDate) I need to find the count of days in a specified date range (e.g. between 1st Jan and 30 Jun) that a user selects using textboxes that have at least 1 job open ie between StartDate and EndDate. I have some experience with VBA and with SQL (not very good with grouping).
Could anyone assist me with a suggestion of how I could get this count?
JobID| StartDate| EndDate
1142| 03-Jan-14| 04-Feb-14|
1143| 13-Mar-14| 18-May-14|
1144| 03-Jan-14| 29-Jan-14|
1145| 20-Jan-14| 13-Apr-14|
1146| 03-Jan-14| 07-Jan-14|
You could create a calendar table as suggested in the comments and add a button called cmdCountJobDays to your form.
The below code will check all possible dates in the selected date range against the job dates. A bit clunky but it will get you there :-)
Private Sub cmdCountJobDays_Click()
Dim StartDate as Date
Dim EndDate as Date
StartDate = me.txtYourStartDateTextBox
EndDate = me.txtYourEndDateTextBox
SQLGetJobCountPeriod = SQLGetJobCountPeriod & "Select CalendarDate from tblCalendarDates where CalendarDate >=" & cdbl(StartDate)
SQLGetJobCountPeriod = SQLGetJobCountPeriod & " and CalendarDate <= " & cdbl(EndDate)
Set dateschecked = CurrentDb.OpenRecordset(SQLGetJobCountPeriod)
If dateschecked.EOF = False Then
dateschecked.MoveFirst
CountOpenJobDays = 0
CountAllDays = 0
Do While dateschecked.EOF = False
CurrentCheckDate = CDbl(dateschecked.Fields("CalendarDate"))
SQLJobDates = "Select StartDate, EndDateDate from jobdetails "
Set Jobdates = CurrentDb.OpenRecordset(SQLJobDates)
If Jobdates.EOF = False Then
Jobdates.MoveFirst
Do While Jobdates.EOF = False
Jobstartdate = CDbl(Jobdates.Fields("StartDate"))
Jobenddate = CDbl(Jobdates.Fields("EndDate"))
If (CurrentCheckDate > Jobstartdate - 1) And (CurrentCheckDate < Jobenddate + 1) Then
CountJobOpen = CountJobOpen + 1
Exit Do
End If
Jobdates.MoveNext
Loop
End If
CountAllDays = CountAllDays + 1
dateschecked.MoveNext
Loop
End If
msgbox CountJobOpen
End Sub
The count is using a datediff function.
datediff(dd,startdate, enddate)
"dd" tells it to find days, start date would be 1/03/2014, and end date would be 2/04/2014 as an example for your first line

Set Categories on DateNavigator

My friend seems to be having some trouble with ABAP. Here's a copy of his question - posted on the SAP community forums.
Hey Everyone,
I am trying to mark the DateNavigator with two categories. I made a context called Marking, with attributes Date, Category and Tooltip.
Node: Marking
Date:
Category:
Tooltip:
I filled category attribute with two categories : e_category-three and e_category-four. I filled the Date attribute with dates. I want some of these dates to be category-three and others category-four.
Currently, all dates are set to the first category (e_category-three) and the code looks like this.
if ls_host_name-host_name <> host_msg and ls_vm_name-vm_name = vm_msg.
loop at lt_machine_booking into wa.
if ls_host_name-host_name = wa-host_name.
date = wa-reserved_from.
while date <= wa-reserved_till.
ls_dates_shared-dates = date. > i want these dates to be e_category-three
append ls_dates_shared to lt_dates_shared.
add 1 to date.
ENDWHILE.
endif.
ENDLOOP.
elseif ls_host_name-host_name <> host_msg and ls_vm_name-vm_name <> vm_msg.
loop at lt_machine_booking into wa.
if ls_host_name-host_name = wa-host_name and ls_vm_name-vm_name = wa-vm_name.
date = wa-reserved_from.
while date <= wa-reserved_till.
ls_dates_shared = date. > i want these dates to be e_category-four
append ls_dates_shared to lt_dates_shared.
add 1 to date.
ENDWHILE.
endif.
" ...
I'm assuming that ls_dates_shared is of type marking?
If this is the case you have to fill the fields ls_dates_shared-category and ls_dates_shared-tooltip explicitly.
Currently this may be filled prior to the code snippet that you give us. Try something like this:
if ls_host_name-host_name <> host_msg and ls_vm_name-vm_name = vm_msg.
loop at lt_machine_booking into wa.
if ls_host_name-host_name = wa-host_name.
date = wa-reserved_from.
while date <= wa-reserved_till.
ls_dates_shared-dates = date. "i want these dates to be e_category-three"
ls_dates_shared-category = e_category-three.
"ls_dates-tooltip = appropriate_tooltip for e_category-three"
append ls_dates_shared to lt_dates_shared.
add 1 to date.
ENDWHILE.
endif.
ENDLOOP.
elseif ls_host_name-host_name <> host_msg and ls_vm_name-vm_name <> vm_msg.
loop at lt_machine_booking into wa.
if ls_host_name-host_name = wa-host_name and ls_vm_name-vm_name = wa-vm_name.
date = wa-reserved_from.
while date <= wa-reserved_till.
ls_dates_shared = date. "i want these dates to be e_category-four"
ls_dates_shared-category = e_category-four.
"ls_dates-tooltip = appropriate_tooltip for e_category-four"
append ls_dates_shared to lt_dates_shared.
add 1 to date.
ENDWHILE.
endif.
...