Comparision of Two Fields:

sbaud2003

Member
Local time
Tomorrow, 04:35
Joined
Apr 5, 2020
Messages
186
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.
 
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....
 
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.
 

Users who are viewing this thread

Back
Top Bottom