We help IT Professionals succeed at work.

Easy Easy Word-Visual Basic ?

300 Views
Last Modified: 2013-11-25
This is so easy!   I used to do it in good 'ole Quick Basic quite a few years back, but I'm either completely braindead or half asleep.

Take a look at this:


===C=O=D=E==========

Private Sub txtHearingDate_Change()

    dash = "-"          ' <- already dimmed
   
    If Chr(KeyAscii) <> Chr$(8) Then
         
        If txtHearingDate.TextLength = 2 Then
        txtHearingDate.Text = txtHearingDate.Text + dash
        Else: End If
                 
        If txtHearingDate.TextLength = 5 Then
        txtHearingDate.Text = txtHearingDate.Text + dash
        Else: End If
           
    Else
    MsgBox ("backspace!")     ' <- for debugging
    End If
End Sub

==================

I'm getting input from a user asking for a date in MM-DD-YY format.  When the user presses two keys (for MM), I want the system to input the dash in for the user, and when the user pounds two more keys (for DD), I want the system to input the final dash.  The reason why the IF statement is in concerning chr$(8)(backspace) is because when the user presses backspace it will kick back to character 5 or 2 (erasing the dash) and the code will place a dash on the screen immediately,  making it impossible to edit MM or DD without the movement of the mouse.  I believe I'm messing something up with the character reading commands.  It's a stupid, simple concept but it's racking my brain today.  Keep in mind I am (A) novice at VB and (B) using the MS-Word Visual Basic and not the actual program.  Any help would be most appreciated, as usual!  Thanks in advance!

-rick, detroit (go wings)
Comment
Watch Question

Commented:
you can use Format and FormatDate Function to get this done.

Author

Commented:
Edited text of question.

Author

Commented:
I've tried, but I wasn't able to get it to go.
How can I make it work *as* the user is inputting it?  I want them to hit ## and the dash to pop up out of nowhere so they don't have to type it..

Ideas?

rick

Author

Commented:
I've tried, but I wasn't able to get it to go.
How can I make it work *as* the user is inputting it?  I want them to hit ## and the dash to pop up out of nowhere so they don't have to type it..

Ideas?

rick
Yes, by all means use formatting or the input mask for the textbox.  You can save yourself a lot of headaches.

In the future, don't use the "+" operator to concatenate strings.  ALWAYS use "&" or you can end up introducing math where you don't want it.

Wes

Author

Commented:
. for some reason the system is doubling everything I type..
odd..

rick

Author

Commented:
input mask?  I don't quite know what that is.

-rick
Alfredo Luis Torres SerranoASP .Net Developer

Commented:
Hi,

  Why you don't try to use the mask edit control box that is in msmask32.ocx on the components instead the text box

Regards

Author

Commented:
Masteraco:

I'm way new to this.  msmask32.ocx?  What exactly is that and where can I find it in Word VB?

rick
Brendt HessSenior DBA
CERTIFIED EXPERT

Commented:
Here - try this code.  It works for me, at least.  

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim TL%
    TL = Len(Text1)
    Select Case KeyAscii
    Case 8
        If TL = 3 Then
            Text1 = Left(Text1, 1)
        ElseIf TL = 6 Then
            Text1 = Left(Text1, 4)
        ElseIf TL > 1 Then
            Text1 = Left(Text1, TL - 1)
        Else
            Text1 = ""
        End If
    Case 48 To 57
        If TL >= 10 Then
            Beep
        Else
            Text1 = Text1 & Chr(KeyAscii)
            If TL = 1 Or TL = 4 Then Text1 = Text1 & "-"
        End If
    Case 13     ' CR
        SendKeys "{Tab}"
    Case Else
        Beep
    End Select
    KeyAscii = 0
    Text1.SelStart = Len(Text1)
End Sub

Author

Commented:
Wow, incredible approach to it.  I have a couple of questions:


"txtHearingDate.Text = Left(txtHearingDate.Text, 4)"

Can you explain that to me a little?  I'm assuming it's moving the cursor of txtHearingDate.Text Left, but what is the 4?  It's not moving back four spaces, is it?

Also, since I put the code in, I can't seem to get a strict MM-DD-YY format.  It's allowing me to put in MM-DD-YYYY.  Is there something in that code (possibly the 4) that is allowing the field to ignore it's maxlength property?

Thanks!

rick
Alfredo Luis Torres SerranoASP .Net Developer

Commented:
Hi,

  When you open VB in the "Project" menu you can see "References" look into it and mark the one called "Microsoft Masked edit control 5.0" with this control instead the text box you can especify a mask on the mas property.

Regards
You can try this as well.  It monitors the length of the textbox to see if a dash, which is chr(45), is needed.

On leaving the control, the data is checked to see if it is a valid date.  If it isn't, it takes the user back to the control to fix it:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1) Then
MsgBox "Input a valid date"
TextBox1.SetFocus
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(TextBox1) = 2 Then
    TextBox1 = TextBox1 & Chr(45)
ElseIf Len(TextBox1) = 5 Then
    TextBox1 = TextBox1 & Chr(45)
End If
End Sub

Wes

Author

Commented:
Masteraco:

I'm not using VB and I can't find it in Word.
I'm just using the MS-Word version.

-rick
Sorry, you'll need to change references to TextBox1 to Txthearingdate in the code I posted.

Wes

Author

Commented:
Thats fine.. checking it out now..

*goes back to program*

rick

Author

Commented:
When does this "BeforeUpdate" run?
Before each individual character is put on the screen or before the entire thing is done?

rick
Alfredo Luis Torres SerranoASP .Net Developer

Commented:
Ups in Word that control does not exist sorry

Bye
The beforeupdate event fires as the text box is exited.  It does not fire as each character is input.

It seems to work pretty well on a form I put together, but you might want to change the keypress function so that it enters the dash sooner.  In my example it enters the dash as you type the third character, which looks kinda weird.

Let me know if you have trouble getting the code attached to your userform.

Wes

Author

Commented:
Wes:

Two things.  

1)  It's not re-setting the focus after I put in a completely wrong date.  

2)  Is there a way that you can press two keys and the dash pops up, instead of pressing three?
It'd be easier for the people I'm writing this for if they just appeared before they go to type it themselves.  I'm looking into it as well.  There has to be a way!

-rick

Author

Commented:
Ah, your a step ahead of me.  I'm going to try and get it to fire quicker.  I def'ly like that date-proof idea, though!

rick
#2

Put the code in the keyup event:

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Len(TextBox1) = 2 Then
    TextBox1 = TextBox1 & Chr(45)
ElseIf Len(TextBox1) = 5 Then
    TextBox1 = TextBox1 & Chr(45)
End If
End Sub

and then it measures the length of the textbox text AFTER the 2nd key has been pressed, rather than before the third key.

#1

Working on it.  You might just want to set the textbox to a default date if the user inputs a bad one.  Something like:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1) Then
MsgBox "Input a valid date"
TextBox1 = format(date,"mm-dd-yy")Cancel = True
End If
End Sub

Author

Commented:
Wes,

My helpfile doesn't include info on KEYUP.  What does KEYUP do differently theoretically then CHANGE or even KEYDOWN?  It works but I'm wondering WHY.. lol.  

TextBox1 = format(date,"mm-dd-yy")Cancel = True

I'm getting an error on the CANCEL portion of it.

Either way.. I think you've got me going here.

Big suprise, 'eh?

-rick
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Adjusted points from 20 to 30

Author

Commented:
Yep, got it.  Rock and roll.  Your the man.

-rick

Author

Commented:
Wes:  I'm just going to send you a copy of the program and you can write it, ok?  I mean geesh I'm going to have to put your name in the credits or I'll feel bad.  (That means licensing fees.. man)

-rick
Just happy to help out.

Wes

Author

Commented:
Wes, your my hero.

-rick

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.