|
 |
access thread: month validation
Message #1 by "Howard Stone" <ququmber@h...> on Thu, 6 Dec 2001 20:23:32
|
|
I have an Access batabasd in which I want to validate that the month is
enteres as January, Febreuary,March.....December.
I created an event procedure and attach to the LostFocus event.
The Message box pops up but the focus is not being set as coded. The
cursor moves to the next control. Also, when I enter January the message
box still pops up.
I am new to VB and know that this is a simple error I am making in my
code. Would someone review the code and assistance in resolving this.
Thank you all.
EVENT PROCEDURE:
Private Sub Month_Of_LostFocus() 'Name of the txexbox is Month_Of
Dim theMonth(0 To 11) As String, i As Integer
theMonth(0) = "January"
theMonth(1) = "February"
theMonth(2) = "March"
theMonth(3) = "April"
theMonth(4) = "May"
theMonth(5) = "June"
theMonth(6) = "July"
theMonth(7) = "August"
theMonth(8) = "September"
theMonth(9) = "October"
theMonth(10) = "November"
theMonth(11) = "December"
For i = 0 To 11
If Month_Of.Text <> theMonth(i) Then
MsgBox ("Enter month in the format January, or April")
Month_Of.SetFocus
Exit Sub
End If
Next i
End Sub
Howard Stone
Message #2 by Patrick_Slesicki@d... on Thu, 6 Dec 2001 12:37:07 -0800
|
|
Hi Howard,
Can you give some context as to what you're doing. From what little I see,
it might be easier for you and for your end users not to have to type in
the months. A combo box with the month list would be a much easier way for
the end user to enter the months. Also are these months based on some date
field elswhere in your database? If so, perhaps you can just format a date
entry such as 10/15/01 to read "October."
Message #3 by Walt Morgan <wmorgan@s...> on Thu, 06 Dec 2001 14:53:29 -0600
|
|
Have you considered a combobox containing the month "Names"? Is this a
separate field in the table or is it derived from a date field? The answers
may elicit some other ideas.
Walt
Message #4 by "Howard Stone" <ququmber@h...> on Fri, 7 Dec 2001 13:09:05
|
|
Thanks for the assistance. I now learn from the responses that a combo
box would be easier for the end used and myself. I have actually done it
quite easily with the combo box as suggested and it worked.
Since I am newbie and for my own programming skills I will still ask what
I was doing wrong for my code not to work?.
I want to be a good programmer and do not want to just forget about what I
was doing even though I was shown an easier method. Knowing my mistaked
from this exercise will make me a better programmer.
The fields do not originate from a table. I aslo thought of entering the
date and format it to show Jamuary but decided to use the array method
because of my limited knowledge.
So, please review the code and tell me my mistakes.
Thanks a million
Howard Stone
Message #5 by "Pardee, Roy E" <roy.e.pardee@l...> on Fri, 07 Dec 2001 07:07:37 -0800
|
|
I think the biggest problem was that you were checking the value in your
text box against each of the twelve valid values one at a time and on the
first mismatch you returned an error and exited the sub. Here's the code
I'm talking about:
For i = 0 To 11
If Month_Of.Text <> theMonth(i) Then
MsgBox ("Enter month in the format January, or April")
Month_Of.SetFocus
Exit Sub
End If
Next i
The problem is that even a valid value can only be equal to *one* of the
twelve values at a time--even valid values will fail 11 of the twelve
comparisons. For instance if the user entered "February", then when this
loop was first entered and i = 0, the comparison in your If statement
("February" <> "January) would return True and the code would give the error
msg and terminate.
I can think of two other approaches. If you like the array solution, you
can use an extra variable like so:
'==================================================
Dim booIsValid as Boolean
...
booIsValid = False
For i = 0 To 11
' Note the change to equals in this If-Then statement:
If Month_Of.Text = theMonth(i) Then
booIsValid = True
End If
Next i
' Now we've compared the entered value to each of the twelve
' valid values & if entered matched *any* of the valid values
' booIsValid would be set to True
If booIsValid Then
' Do nothing
Else
MsgBox ("Enter month in the format January, or April")
Month_Of.SetFocus
End If
'==================================================
But probably easiest would be a Select Case statement like so:
'==================================================
Select Case Month_Of.Value
Case "January", "February", "March", _
"April", "May", "June, _
"July", "August", "September", _
"October", "November", "December"
' Do nothing
Case Else
MsgBox ("Enter month in the format January, or April")
Month_Of.SetFocus
End Select
'==================================================
HTH,
-Roy
Roy Pardee
Programmer/Analyst
SWFPAC Lockheed Martin IT
Extension 8487
-----Original Message-----
From: Howard Stone [mailto:ququmber@h...]
Sent: Friday, December 07, 2001 5:09 AM
To: Access
Subject: [access] Re: month validation
Thanks for the assistance. I now learn from the responses that a combo
box would be easier for the end used and myself. I have actually done it
quite easily with the combo box as suggested and it worked.
Since I am newbie and for my own programming skills I will still ask what
I was doing wrong for my code not to work?.
I want to be a good programmer and do not want to just forget about what I
was doing even though I was shown an easier method. Knowing my mistaked
from this exercise will make me a better programmer.
The fields do not originate from a table. I aslo thought of entering the
date and format it to show Jamuary but decided to use the array method
because of my limited knowledge.
So, please review the code and tell me my mistakes.
Thanks a million
Howard Stone
|
|
 |