Solved

Input mask for a textbox

Posted on 1997-04-04
3
688 Views
Last Modified: 2011-09-20
How do I have a input mask, like in Acces, for a text box.
I have tried Textbox1 = Format$(0000-0000000), for my telephone textbox. But user got to enter the -, can I display the - in the textbox.
Also how can I validate the data entered into the textbox, making sure that user enter the correct information. Is there a way that I can specify these checks in the properties. Upon error in the data entered, have a message to warn user. I have a lot of fields, do I have to create a msgbox for every field.
0
Comment
Question by:LarryTan
3 Comments
 
LVL 1

Accepted Solution

by:
aktis earned 250 total points
ID: 1425051
The events you want are
1) KeyPress. Changing the KeyPress variable you can change one keystroke to another e.g. an non numberic ascii to "-"
2) LostFocus: fires when you leave the textbox and you can format the Text in the way you want

You can make a procedure with a parameter representing the current control. e.e.
Private Procedure RepairTelephone( cntrl as control)
    cntrl.Text=.......  
end sub
0
 

Author Comment

by:LarryTan
ID: 1425052
REgarding about the KeyPress event, can you be more specific about it, I have tried playing around but couldn't get it working. Please show me how its done.
0
 

Expert Comment

by:mhuber
ID: 1425053
Try adding a maskcontrol to your form.  Under properties enter the following for a phone number: ###-####.  By doing this your mask control will display the - mark and someone using your program will not have to enter it.  I usually name my mask controls like txtPhone, it's easier for me to keep up with it.  Here's the code I use in my lostfocus on this control.
Private Sub txtDate_LostFocus()
Dim I As Integer
DT1$ = Left$(txtDate.Text, 2): DT2$ = Mid$(txtDate.Text, 3, 2): DT3$ = Right$(Date$, 2)
CheckDate = DT1$ + ", " + DT2$ + ", " + DT3$
Print CheckDate
If IsDate(CheckDate) = False Then
   
beep: beep: beep: beep: beep: beep: beep
    Prompt = "Invalid date entered! Do you want to use today's date? " + txtDate.Text
   
    I = MsgBox(Prompt, vbYesNo)
If I = vbYes Then
    M1$ = Left$(Date$, 2): D1$ = Mid$(Date$, 4, 2): Y1$ = Right$(Date$, 2)
    UI$ = M1$ + "-" + D1$ + "-" + Y1$
    txtDate.Text = UI$
Else
    txtDate.SetFocus

End If
End If
End Sub

Hope this helps...if you need to contact me:  mhuber@peop.tdsnet.com
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Is an IP address in a Range 22 72
Determine Range to Select 5 34
Adding to a VBA? 6 49
Access query that references subform 5 35
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now