Solved

Easy Easy Word-Visual Basic ?

Posted on 2000-03-14
29
241 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)
0
Comment
Question by:saksafon2
  • 17
  • 7
  • 3
  • +2
29 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2616982
you can use Format and FormatDate Function to get this done.
0
 

Author Comment

by:saksafon2
ID: 2616987
Edited text of question.
0
 

Author Comment

by:saksafon2
ID: 2616992
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
 

Author Comment

by:saksafon2
ID: 2616998
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
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2616999
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
 

Author Comment

by:saksafon2
ID: 2617000
. for some reason the system is doubling everything I type..
odd..

rick
0
 

Author Comment

by:saksafon2
ID: 2617006
input mask?  I don't quite know what that is.

-rick
0
 
LVL 12

Expert Comment

by:Alfredo Luis Torres Serrano
ID: 2617010
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
 

Author Comment

by:saksafon2
ID: 2617014
Masteraco:

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

rick
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2617128
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
 

Author Comment

by:saksafon2
ID: 2617177
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
 
LVL 12

Expert Comment

by:Alfredo Luis Torres Serrano
ID: 2617199
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
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2617204
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
 

Author Comment

by:saksafon2
ID: 2617209
Masteraco:

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

-rick
0
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!

 
LVL 4

Expert Comment

by:wesleystewart
ID: 2617212
Sorry, you'll need to change references to TextBox1 to Txthearingdate in the code I posted.

Wes
0
 

Author Comment

by:saksafon2
ID: 2617217
Thats fine.. checking it out now..

*goes back to program*

rick
0
 

Author Comment

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

rick
0
 
LVL 12

Expert Comment

by:Alfredo Luis Torres Serrano
ID: 2617236
Ups in Word that control does not exist sorry

Bye
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2617245
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
 

Author Comment

by:saksafon2
ID: 2617247
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
 

Author Comment

by:saksafon2
ID: 2617248
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
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2617274
#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
 

Author Comment

by:saksafon2
ID: 2617299
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
 
LVL 4

Accepted Solution

by:
wesleystewart earned 30 total points
ID: 2617310
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
 

Author Comment

by:saksafon2
ID: 2617328
Adjusted points from 20 to 30
0
 

Author Comment

by:saksafon2
ID: 2617329
Yep, got it.  Rock and roll.  Your the man.

-rick
0
 

Author Comment

by:saksafon2
ID: 2617331
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
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2617334
Just happy to help out.

Wes
0
 

Author Comment

by:saksafon2
ID: 2617344
Wes, your my hero.

-rick
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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now