Comparision of Two Fields: (1 Viewer)

sbaud2003

Member
Local time
Today, 12:45
Joined
Apr 5, 2020
Messages
178
If TXTLV.value = "CML" And txtfrom.value >= TXTDSAN.value Then
MsgBox "Commuted Leave can't be presanctioned, Please check!!!!", vbOKOnly + vbCritical, "Hello !! Information incomplete"
Me.TXTDSAN.SetFocus
Exit Sub
End If

is there any mistake in code, any value i am putting it flases the errormsg only.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:15
Joined
Feb 19, 2002
Messages
43,293
Logically, I don't see an issue but in Access, the most efficient way to refer to form controls is with the Me. reference. So:
Code:
If Me.TXTLV = "CML" And Me.txtfrom >= Me.TXTDSAN Then
    MsgBox "Commuted Leave can't be presanctioned, Please check!!!!", vbOKOnly + vbCritical, "Hello !! Information incomplete"
    Me.TXTDSAN.SetFocus
Exit Sub
End If
The issue could be that your date fields are defined as strings or formatted as strings. When you do that, the date acts like a string rather than like a date and you will frequently run into this type of issue.

As a string "01/10/2024" is less than "12/25/2023".
However, as a date #01/10/2024# is greater than #12/25/2023#.

REMEMBER, dates are stored internally as double precision numbers. The integer portion is the number of days since Dec 30, 1899 and the decimal represents the percent of the day that has elapsed. So:

0.0 = 12/30/1899 midnight
0.5 = 12/30/1899 noon
1.75 = 12/31/1899 06:00 PM
-3.25 = 12/27/1899 06:00 AM

The code is out of context. It belongs in the form's BeforeUpdate event AND it should include two more lines.
Code:
If Me.TXTLV = "CML" And Me.txtfrom >= Me.TXTDSAN Then
    MsgBox "Commuted Leave can't be presanctioned, Please check!!!!", vbOKOnly + vbCritical, "Hello !! Information incomplete"
    Me.TXTDSAN.SetFocus
    Cancel = True
    Exit Sub
Exit Sub
End If
If the code is not in the correct event, it will be ineffective. It will raise the error message but it will still allow bad data to be saved. You might want to take a look at one or more of these videos. If you are only going to watch one, the most entertaining is the YouTube video of an Access user group meeting where I made a presentation of the sample I built to help you to understand how form/control events work.
PS, use the forum's code tag function to make your code retain its original format so it is more readable.
 
Last edited:

sbaud2003

Member
Local time
Today, 12:45
Joined
Apr 5, 2020
Messages
178
Logically, I don't see an issue but in Access, the most efficient way to refer to form controls is with the Me. reference. So:
Code:
If Me.TXTLV = "CML" And Me.txtfrom >= Me.TXTDSAN Then
    MsgBox "Commuted Leave can't be presanctioned, Please check!!!!", vbOKOnly + vbCritical, "Hello !! Information incomplete"
    Me.TXTDSAN.SetFocus
Exit Sub
End If
The issue could be that your date fields are defined as strings or formatted as strings. When you do that, the date acts like a string rather than like a date and you will frequently run into this type of issue.

As a string "01/10/2024" is less than "12/25/2023".
However, as a date #01/10/2024# is greater than #12/25/2023#.

REMEMBER, dates are stored internally as double precision numbers. The integer portion is the number of days since Dec 30, 1899 and the decimal represents the percent of the day that has elapsed. So:

0.0 = 12/30/1899 midnight
0.5 = 12/30/1899 noon
1.75 = 12/31/1899 06:00 PM
-3.25 = 12/27/1899 06:00 AM

The code is out of context. It belongs in the form's BeforeUpdate event AND it should include two more lines.
Code:
If Me.TXTLV = "CML" And Me.txtfrom >= Me.TXTDSAN Then
    MsgBox "Commuted Leave can't be presanctioned, Please check!!!!", vbOKOnly + vbCritical, "Hello !! Information incomplete"
    Me.TXTDSAN.SetFocus
    Cancel = True
    Exit Sub
Exit Sub
End If
If the code is not in the correct event, it will be ineffective. It will raise the error message but it will still allow bad data to be saved. You might want to take a look at one or more of these videos. If you are only going to watch one, the most entertaining is the YouTube video of an Access user group meeting where I made a presentation of the sample I built to help you to understand how form/control events work.
PS, use the forum's code tag function to make your code retain its original format so it is more readable.
Logically, I don't see an issue but in Access, the most efficient way to refer to form controls is with the Me. reference. So:
Code:
If Me.TXTLV = "CML" And Me.txtfrom >= Me.TXTDSAN Then
    MsgBox "Commuted Leave can't be presanctioned, Please check!!!!", vbOKOnly + vbCritical, "Hello !! Information incomplete"
    Me.TXTDSAN.SetFocus
Exit Sub
End If
The issue could be that your date fields are defined as strings or formatted as strings. When you do that, the date acts like a string rather than like a date and you will frequently run into this type of issue.

As a string "01/10/2024" is less than "12/25/2023".
However, as a date #01/10/2024# is greater than #12/25/2023#.

REMEMBER, dates are stored internally as double precision numbers. The integer portion is the number of days since Dec 30, 1899 and the decimal represents the percent of the day that has elapsed. So:

0.0 = 12/30/1899 midnight
0.5 = 12/30/1899 noon
1.75 = 12/31/1899 06:00 PM
-3.25 = 12/27/1899 06:00 AM

The code is out of context. It belongs in the form's BeforeUpdate event AND it should include two more lines.
Code:
If Me.TXTLV = "CML" And Me.txtfrom >= Me.TXTDSAN Then
    MsgBox "Commuted Leave can't be presanctioned, Please check!!!!", vbOKOnly + vbCritical, "Hello !! Information incomplete"
    Me.TXTDSAN.SetFocus
    Cancel = True
    Exit Sub
Exit Sub
End If
If the code is not in the correct event, it will be ineffective. It will raise the error message but it will still allow bad data to be saved. You might want to take a look at one or more of these videos. If you are only going to watch one, the most entertaining is the YouTube video of an Access user group meeting where I made a presentation of the sample I built to help you to understand how form/control events work.
PS, use the forum's code tag function to make your code retain its original format so it is more readable.

Thanks its working now....
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:15
Joined
Feb 19, 2002
Messages
43,293
What did you use to fix the problem? Telling us here helps others who might find this thread later.
 

sbaud2003

Member
Local time
Today, 12:45
Joined
Apr 5, 2020
Messages
178
I have use the Me! refrence and it works fine
I need you help in another situation.
I am having two fileds of leave sanction i.e. From and To, I want to put a validation check, while applying any othrr leave, to see if the Period is is inbetween the leave already sanctioned.
how can I resolve this issue.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:15
Joined
Feb 19, 2002
Messages
43,293
The validation code belongs in the BeforeUpdate event of the form. When multiple fields are involved it is especially awkward to try to validate in the control level events so just put the code in the FORM's BeforeUpdate event.

You need to use dCount() to count the number of rows where the From date is between the start and end dates of existing leave records. Then you need to use dCount() to count the number of rows where the to date is between the start and end dates of existing leave records. If either count is > 0, you have an overlap so you need to cancel the event as I showed in my earlier code.
 

Users who are viewing this thread

Top Bottom