Force text field to be a year 1999

How can create validation for my text field to make sure that it is a year?  Or only numbers are valid....1999

Additionally, I have 3 combo boxes on my form that need to have default values.  Currently, on new records, they appear blank.
KathyBrowningAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danlevansCommented:
Kathy,
Try this:
Private Sub Command1_Click()
    Dim TestYear
    TestYear = "1999"
    testDate = "1/1/"
    If IsDate(testDate & TestYear) Then
        MsgBox "good year"
    Else
        MsgBox "bad year"
    End If
End Sub
0
danlevansCommented:
Kathy,

To add default values to combo's

Private Sub Form_Load()
    Combo1.AddItem = "Default Value 1"
    Combo1.AddItem = "Default Value 2"
    ' etc.
End Sub
0
KathyBrowningAuthor Commented:
Please confirm that the answer you provided is for a field of type "text" not date.  I don't know why you did the 1/1/.  I simply want to verify that the items entered are numbers... not characters.  (Since it is a text field and both are allowed.)

Additionally, I've used the .additem before, but how does this specify the "default value" say if the item is the 3rd in the list?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

lazarruCommented:
For the first question you can limit the field to only handle numbers by using the keypress event  with the following code

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii > 47 And KeyAscii < 59 Then
    Else
        KeyAscii = 0
    End If
End Sub

what happens is if anything other than a numeric key is pressed the value is ignored. I think what danlevans was doing was trying to check if the value that was entered was a year by check if 1/1/(value entered) is a date.  Only limiting the field to handle numbers doesn't handle both of your criteria.

As far as the second question I think what you are looking to do is set the listindex property.

If you have the following
    Combo1.AddItem "d"
    Combo1.AddItem "e"
    Combo1.AddItem "f"
    Combo1.AddItem "g"
    Combo1.ListIndex = 2

then f will be seleced when the combo box is displayed.  The listindex property sees the first item as 0.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KathyBrowningAuthor Commented:
Instead of ignoring if a text entry is entered, should I send a msgbox?

since I have the list items in a property of the combo box, will I need to re-enter this under Sub Form OPen()? With all of the .AddItem's.  Or can I just add the .ListIndex under the FormOpen() routine without the .AddItem's?
0
lazarruCommented:
Typically if you want to limit to just numbers you would not use a msgbox.  It would be too confusing.

You do not have to do the additems, I just had that as an example.  You can just do the listindex
0
KathyBrowningAuthor Commented:
Thanks, see my other question regarding ID's.
0
KathyBrowningAuthor Commented:
By the way,I've tried this and it's not working.  I tried it on the Form Load() property.  Perhaps there is a Data1.AddNew() or something and I'm just not wording it right.  I have

Private Sub Data1_AddNew()
    cmbBusType.ListIndex = 0
    cmbBusCert.ListIndex = 0
   
End Sub


should I be placing this somewhere else?

0
lazarruCommented:
How are you creating new records?
0
KathyBrowningAuthor Commented:
Yes, I want it to be the default when creating new records.
0
lazarruCommented:
Are you creating new records in code?  If so just set the listindex after adding the record
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.