• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Easy Easy Word-Visual Basic ?

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)
0
saksafon2
Asked:
saksafon2
  • 17
  • 7
  • 3
  • +2
1 Solution
 
MarineCommented:
you can use Format and FormatDate Function to get this done.
0
 
saksafon2Author Commented:
Edited text of question.
0
 
saksafon2Author 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
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
saksafon2Author 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
0
 
wesleystewartCommented:
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
0
 
saksafon2Author Commented:
. for some reason the system is doubling everything I type..
odd..

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

-rick
0
 
Alfredo Luis Torres SerranoDevelopment Director / DBACommented:
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
0
 
saksafon2Author Commented:
Masteraco:

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

rick
0
 
Brendt HessSenior DBACommented:
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
0
 
saksafon2Author 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
0
 
Alfredo Luis Torres SerranoDevelopment Director / DBACommented:
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
0
 
wesleystewartCommented:
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
0
 
saksafon2Author Commented:
Masteraco:

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

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

Wes
0
 
saksafon2Author Commented:
Thats fine.. checking it out now..

*goes back to program*

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

rick
0
 
Alfredo Luis Torres SerranoDevelopment Director / DBACommented:
Ups in Word that control does not exist sorry

Bye
0
 
wesleystewartCommented:
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
0
 
saksafon2Author 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
0
 
saksafon2Author 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
0
 
wesleystewartCommented:
#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
0
 
saksafon2Author 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
0
 
wesleystewartCommented:
The code should have posted like this:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1) Then
MsgBox "Input a valid date"
TextBox1.SetFocus
Cancel = True
End If
End Sub

The:

Cancel = True

belongs on its own line.

Want to know all the events for a control?  Double-click on the control.  This will take you to the code window.  At the top of the window there are two scroll boxes.  The one on the left lists all the controls and is currently on the control you selected.  The scroll box to the right lists ALL the events for that control.  You will find some weird ones in there.

Wes

0
 
saksafon2Author Commented:
Adjusted points from 20 to 30
0
 
saksafon2Author Commented:
Yep, got it.  Rock and roll.  Your the man.

-rick
0
 
saksafon2Author 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
0
 
wesleystewartCommented:
Just happy to help out.

Wes
0
 
saksafon2Author Commented:
Wes, your my hero.

-rick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 17
  • 7
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now