Validation problem!

  • Thread starter Thread starter Niklas Östergren
  • Start date Start date
N

Niklas Östergren

Hi!

I´m trying to validate input data from the user where the user specify a
break date (ex. 1 of september each year) for free membership.

This is what it´s all about:
If a new person pay membership fee for current year same day or after the
break day then shall the membership also be valid for not only current year
but also for the next year.

The user can select, set, this break date in setup fot the assosiation. The
break date is stored as a string:
Ex: 0109 = 1 of september.

Now to my problem:
The validation I have (see code below) works just fine BUT not if the user
tryes to set the dates to ex. 0009.

The validation I need to do is that the user enters a valid day which is
between 1-31 and valid mounth which is between 1-12. But the code I have
below fails to validate if the user enters 0 (zero) for either the day or
the mounth. If the user enters lower value (ex.-1) or higher (ex. 13 for the
mounth and 32 for the day) then it works just fine but NOT 0 (zero).

Any help are appreciated!

TIA
// Niklas

'===========================================
'Seperate break date into day and month
'===========================================
strBreakDay = Trim$(Left$(strBreakDate, 2))
strBreakMonth = Trim$(Right$(strBreakDate, 2))

'================================================
' Check if BreakDay and BreakMonth are valid
'================================================
If CInt(strBreakDay) < 1 Or CInt(strBreakDay) > 31 Then
MsgBox "En månad består av max 31 dagar" & vbCrLf _
& "och du har angett: " & strBreakDay & " dagar." & vbCrLf _
& "Ange en dag inom intervallet 1 - 31.", vbInformation +
vbOKOnly, "Ogiltigt datum (dag)"
ValidateBreakDateForNewFreeMembership = False
GoTo Exit_Procedure
End If

If CInt(strBreakMonth) < 1 Or CInt(strBreakMonth) > 12 Then
MsgBox "Ett år består av 12 månader" & vbCrLf _
& "och du har angett månad: " & strBreakMonth & "." & vbCrLf
_
& "Ange en månad inom intervallet 1 - 12.", vbInformation +
vbOKOnly, "Ogiltigt datum (månad)"

ValidateBreakDateForNewFreeMembership = False
GoTo Exit_Procedure
End If
 
Hi Niklas,

I created a new unbound form to test your code. The form includes an unbound textbox named
txtBreakDate and a command button named Command2. Here is the code behind this form. Note that I
set the return value to True if both of your IF...THEN loops evaluate as false. I also included a
debug.print statement to show the value entered and the result:
___________________________________

Private Sub Command2_Click()
ValidateBreakDateForNewFreeMembership
End Sub

Function ValidateBreakDateForNewFreeMembership() As Boolean
Dim strBreakDate As String
Dim strBreakDay As String
Dim strBreakMonth As String

strBreakDate = Me.txtBreakDate

<Your code inserted here>

ValidateBreakDateForNewFreeMembership = True

Exit_Procedure:
Debug.Print strBreakDate & vbTab & ValidateBreakDateForNewFreeMembership
End Function
___________________________________

Here are the results printed to the debug window:

0009 False <---The example you gave with day = 0 works fine for me.
1009 True <---10 Oct. is okay
1000 False <---Example where month is zero
3102 True <---Oops! There is no such date: 31 Feb.

So, I don't seem to be able to reproduce the situation that you reported with a zero day or zero
month. However, there is a problem for months where the date is not possible. You can use the
built-in IsDate() function to test for a valid date. This will catch invalid entries, yet still
allow for 29 Feb. during leap years. Here is a suggested procedure, although I must caution that
I have not done extensive testing yet!

Option Compare Database
Option Explicit

Private Sub Command2_Click()
ValidateBreakDateForNewFreeMembership
End Sub

Function ValidateBreakDateForNewFreeMembership() As Boolean

Dim strBreakDate As String
Dim strBreakDay As String
Dim strBreakMonth As String
Dim strBreakYear As String

If Not IsNull(txtBreakDate) Then
strBreakDate = Me.txtBreakDate
Else
MsgBox "Please enter a date in the format: ddmm, ddmmyy or ddmmyyyy"
GoTo Exit_Procedure
End If

' Strip out / or - date separators if user included them
strBreakDate = Replace(strBreakDate, "/", "")
strBreakDate = Replace(strBreakDate, "-", "")

' Test for required number of digits
Select Case Len(strBreakDate)
Case 4, 6, 8 ' Treat as valid date (4 = ddmm, 6 = ddmmyy, 8 = ddmmyyyy)
Case Else
MsgBox "Please enter a date in the format: ddmm, ddmmyy or ddmmyyyy"
GoTo Exit_Procedure
End Select

'===========================================
'Separate break date into day and month
'===========================================
strBreakDay = Trim$(Left$(strBreakDate, 2))
strBreakMonth = Trim$(Mid$(strBreakDate, 3, 2))

' Determine year. Use current year if not supplied by user.
Select Case Len(strBreakDate)
Case 4 ' User entered ddmm, so add current year
strBreakYear = Year(Now())
Case 6
strBreakYear = Trim$(Right$(strBreakDate, 2))
Case 8
strBreakYear = Trim$(Right$(strBreakDate, 4))
Case Else
MsgBox "Please enter a date in the format: ddmm, ddmmyy or ddmmyyyy"
GoTo Exit_Procedure
End Select

' And put them all together
strBreakDate = strBreakDay & "/" & strBreakMonth & "/" & strBreakYear

'================================================
' Check if BreakDay and BreakMonth are valid
'================================================

If Not IsDate(strBreakDate) Then
' Tell 'em about it
MsgBox "Oops! You've entered an invalid date"
GoTo Exit_Procedure
End If

ValidateBreakDateForNewFreeMembership = True

Exit_Procedure:
Debug.Print Me.txtBreakDate & vbTab & strBreakDate & _
vbTab & ValidateBreakDateForNewFreeMembership
End Function
______________________________________

And here are the results of some limited test data, as printed to the debug window:

02282005 02/28/2005 True
02/28/2005 02/28/2005 True
02-28/2005 02/28/2005 True
02-28-2005 02/28/2005 True
0229 02/29/2004 True
02/29 02/29/2004 True
02-29-2005 02/29/2005 False
02292005 02/29/2005 False
0009 00/09/2004 False

Note that the user can enter date separators "/" or "-", and they can enter a different year if
they wish. 2004 is a leap year, so there are 29 days in February. However, there are not 29
days in Feb., 2005, so that entry should fail.

Tom

****************************************************

Hi!

I´m trying to validate input data from the user where the user specify a
break date (ex. 1 of september each year) for free membership.

This is what it´s all about:
If a new person pay membership fee for current year same day or after the
break day then shall the membership also be valid for not only current year
but also for the next year.

The user can select, set, this break date in setup fot the assosiation. The
break date is stored as a string:
Ex: 0109 = 1 of september.

Now to my problem:
The validation I have (see code below) works just fine BUT not if the user
tryes to set the dates to ex. 0009.

The validation I need to do is that the user enters a valid day which is
between 1-31 and valid mounth which is between 1-12. But the code I have
below fails to validate if the user enters 0 (zero) for either the day or
the mounth. If the user enters lower value (ex.-1) or higher (ex. 13 for the
mounth and 32 for the day) then it works just fine but NOT 0 (zero).

Any help are appreciated!

TIA
// Niklas

'===========================================
'Seperate break date into day and month
'===========================================
strBreakDay = Trim$(Left$(strBreakDate, 2))
strBreakMonth = Trim$(Right$(strBreakDate, 2))

'================================================
' Check if BreakDay and BreakMonth are valid
'================================================
If CInt(strBreakDay) < 1 Or CInt(strBreakDay) > 31 Then
MsgBox "En månad består av max 31 dagar" & vbCrLf _
& "och du har angett: " & strBreakDay & " dagar." & vbCrLf _
& "Ange en dag inom intervallet 1 - 31.", vbInformation +
vbOKOnly, "Ogiltigt datum (dag)"
ValidateBreakDateForNewFreeMembership = False
GoTo Exit_Procedure
End If

If CInt(strBreakMonth) < 1 Or CInt(strBreakMonth) > 12 Then
MsgBox "Ett år består av 12 månader" & vbCrLf _
& "och du har angett månad: " & strBreakMonth & "." & vbCrLf
_
& "Ange en månad inom intervallet 1 - 12.", vbInformation +
vbOKOnly, "Ogiltigt datum (månad)"

ValidateBreakDateForNewFreeMembership = False
GoTo Exit_Procedure
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Validation problem 1
Validation problem (again) 1

Back
Top