# Thread: re: If statement

1. Registered User
Join Date
May 2002
Posts
395

## Unanswered: re: If statement

What I really need is an If statement to validate the value of an input variable. It is a string variable. EX: 03325587
03 is the last 2 digits of the current yr (can't lesser than previous yr or greater than next year)
325 is the Julian yr (it is valid if it is between 1-365)
587 is auto count (can't be greater than 999)
If any of the condition is true then return false to check_lotum and exit function. I know there has to be a better way to write this code. Please help. Thanks!

Public Function check_lotnum(lotnum As String)
check_lotnum = True

If Len(lotnum) < 8 Then
check_lotnum = False
Exit Function

Else

If CInt(Mid\$(lotnum, 1, 2)) < CInt(Right(Format(Now(), "yyyy"), 2)) - 1 Then ' \$(Format(Now(), "yyyy")) - 1 Then
check_lotnum = False
Exit Function
Else
If CInt(Mid\$(lotnum, 1, 2)) > CInt(Right(Format(Now(), "yyyy"), 2)) + 1 Then
check_lotnum = False
Exit Function
End If
End If

If CInt(Mid\$(lotnum, 3, 3)) <= 0 Then
check_lotnum = False
Exit Function
Else
If CInt(Mid\$(lotnum, 3, 3)) > 365 Then
check_lotnum = False
Exit Function
End If
End If

'check_material_lotnum = False
'Exit Function
If CInt(Right(lotnum, 3)) >= 999 Then
check_lotnum = False
Exit Function
End If

End If
'Debug.Print CInt(Right(Format(Now(), "yyyy"), 2)) ' \$(Format(Now(), "yyyy")) - 1
'Debug.Print CInt(Mid\$(material_lotnum, 1, 2))
'End If

End Function

2. Stuck on my opinions...
Join Date
Nov 2003
Posts
1,487
Here you go...

[Code Start]

Public Function Check_Lotnum(Lotnum As String) As Boolean
Check_Lotnum = False

Dim Yr As Integer, NowYr As Integer
Dim Jul As Integer
On Error Resume Next
Lotnum = Trim(Lotnum)

'Does the Code number contain the proper number
'of characters.
If Len(Lotnum) > 8 Then Exit Function

'Fill Variables
Yr = CInt((Mid\$(Lotnum, 1, 2)) + 2000)
NowYr = CInt(Year(Now))
Jul = CInt(Mid\$(Lotnum, 3, 3))

'Check the Year
If Yr < (NowYr - 1) Or Yr > (NowYr + 1) Then Exit Function

'Is it between but not 0 and between but not 366.
'In other words, 1 to 365 is valid.
If Jul < 1 Or Jul > 365 Then Exit Function

'We do not need to worry about the Auto Count for the
'simple reason that any number greater than 999 will
'add one additional digit to the overall string thus
'making it 9 characters long. We have already established
'at the beginning of this function that this as illegal.

'If there have been no errors and all the conditions
'are met, we return a 'True' otherwise this function
'will return 'False'
If Err = 0 Then Check_Lotnum = True

End Function

[Code End]
=================================

This is what it looks like without all the Rem statements:

[Code Start]

Public Function Check_Lotnum(Lotnum As String) as boolean
Check_Lotnum = False
Dim Yr As Integer, NowYr As Integer
Dim Jul As Integer
On Error Resume Next
Lotnum = Trim(Lotnum)
If Len(Lotnum) > 8 Then Exit Function
Yr = CInt((Mid\$(Lotnum, 1, 2)) + 2000)
NowYr = CInt(Year(Now))
Jul = CInt(Mid\$(Lotnum, 3, 3))
If Yr < (NowYr - 1) Or Yr > (NowYr + 1) Then Exit Function
If Jul < 1 Or Jul > 365 Then Exit Function
If Err = 0 Then Check_Lotnum = True
End Function

[Code End]

3. Registered User
Join Date
May 2002
Posts
395
Originally posted by CyberLynx
Here you go...

[Code Start]

Public Function Check_Lotnum(Lotnum As String) As Boolean
Check_Lotnum = False

Dim Yr As Integer, NowYr As Integer
Dim Jul As Integer
On Error Resume Next
Lotnum = Trim(Lotnum)

'Does the Code number contain the proper number
'of characters.
If Len(Lotnum) > 8 Then Exit Function

'Fill Variables
Yr = CInt((Mid\$(Lotnum, 1, 2)) + 2000)
NowYr = CInt(Year(Now))
Jul = CInt(Mid\$(Lotnum, 3, 3))

'Check the Year
If Yr < (NowYr - 1) Or Yr > (NowYr + 1) Then Exit Function

'Is it between but not 0 and between but not 366.
'In other words, 1 to 365 is valid.
If Jul < 1 Or Jul > 365 Then Exit Function

'We do not need to worry about the Auto Count for the
'simple reason that any number greater than 999 will
'add one additional digit to the overall string thus
'making it 9 characters long. We have already established
'at the beginning of this function that this as illegal.

'If there have been no errors and all the conditions
'are met, we return a 'True' otherwise this function
'will return 'False'
If Err = 0 Then Check_Lotnum = True

End Function

[Code End]
=================================

This is what it looks like without all the Rem statements:

[Code Start]

Public Function Check_Lotnum(Lotnum As String) as boolean
Check_Lotnum = False
Dim Yr As Integer, NowYr As Integer
Dim Jul As Integer
On Error Resume Next
Lotnum = Trim(Lotnum)
If Len(Lotnum) > 8 Then Exit Function
Yr = CInt((Mid\$(Lotnum, 1, 2)) + 2000)
NowYr = CInt(Year(Now))
Jul = CInt(Mid\$(Lotnum, 3, 3))
If Yr < (NowYr - 1) Or Yr > (NowYr + 1) Then Exit Function
If Jul < 1 Or Jul > 365 Then Exit Function
If Err = 0 Then Check_Lotnum = True
End Function

These are much, much better code.
Thank you!!

[Code End]

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•