[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Force text field to be a year 1999

Posted on 2000-01-05
11
Medium Priority
?
119 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:KathyBrowning
  • 5
  • 4
  • 2
11 Comments
 
LVL 2

Expert Comment

by:danlevans
ID: 2326122
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
 
LVL 2

Expert Comment

by:danlevans
ID: 2326131
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
 

Author Comment

by:KathyBrowning
ID: 2326185
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Accepted Solution

by:
lazarru earned 200 total points
ID: 2326237
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
 

Author Comment

by:KathyBrowning
ID: 2326393
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
 

Expert Comment

by:lazarru
ID: 2326402
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
 

Author Comment

by:KathyBrowning
ID: 2326575
Thanks, see my other question regarding ID's.
0
 

Author Comment

by:KathyBrowning
ID: 2326897
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
 

Expert Comment

by:lazarru
ID: 2326968
How are you creating new records?
0
 

Author Comment

by:KathyBrowning
ID: 2327062
Yes, I want it to be the default when creating new records.
0
 

Expert Comment

by:lazarru
ID: 2327148
Are you creating new records in code?  If so just set the listindex after adding the record
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question