Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find string in string, then act if found?

Status
Not open for further replies.

cheriberi

Technical User
May 27, 2003
60
US
I know I can use InStr to see if there's a period or space in a string, but what do I do once I know? I need to determine whether a string contains a period, space, or hyphen. If it does, I need to bring the form back up so the user can retype their entry without the offending character.

Can anyone help?

Any help is greatly appreciated!

Cheryl
 
Cheryl,

How 'bout checking each character as it's being entered. If an offending character is entered, set a flag and then when the user is done, if the flag is set, do the offending-character-process.
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

End Sub

Skip,

[red]Be advised:[/red] [glasses]
Alcohol and Calculus do not mix!
If you drink, don't derive! [tongue]
 
Another variation, this will test for any "." or "-" and if found prompt the user again, you can add more symbols to check against.
Code:
Sub test()
a$ = InputBox("Enter data")
If InStr(1, a$, ".") > 0 Or InStr(1, a$, "-") > 0 Then
  a$ = InputBox("Please enter valid data")
End If
End Sub
 
Adding on to Skip's post...

Within that sub, you could test for the invalid character, and set KeyAscii = 0 if it contains an invalid character.

For example, the ASCII value of "." is 46. The following will test for that value, and not display the period if it has been pressed:

[tt]
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 46 Then
KeyAscii = 0
End If
End Sub
[/tt]

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
I think I understand Molby's solution, but I'm not sure re Skip and cLFlaVa's solution. For that one, where (and how) would I set up "if KeyAxcii = 0, bring up the form again so user can correct their entry"?

Any help is greatly appreciated!

Cheryl
 
Well, that code would instead be placed in the textbox's KeyPress event. This way, you wouldn't have to bring the form back up - it would still be visible. As soon as a user presses the "." while typing in the texbox, that code will re-set the KeyAscii value to 0, thus not allowing the "." to even display in the textbox.

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Is there a way to flag any non-alpha character? Cuz basically, a-z is all that I want to allow.

Any help is greatly appreciated!

Cheryl
 
Capitals: 65 - 90.
Lowers: 97 - 122.
Space: 32.

The code below allows for lowercase letters, capital letters and spaces.
[tt]
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 65 Or (KeyAscii > 90 And KeyAscii < 97) Or KeyAscii > 122 Or KeyAscii = 32 Then
KeyAscii = 0
End If
End Sub
[/tt]

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Sorry, the will dis-allow spaces. This will allow spaces.

[tt]
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii < 65 And KeyAscii <> 32) Or (KeyAscii > 90 And KeyAscii < 97) Or KeyAscii > 122 Then
KeyAscii = 0
End If
End Sub
[/tt]

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case KeyAscii
    Case Is < 32, 33 To 47, Is > 122
       YourRoutine
  End Select
End Sub


Skip,

[red]Be advised:[/red] [glasses]
Alcohol and Calculus do not mix!
If you drink, don't derive! [tongue]
 
Another variation on the same theme...
just for the hell of it!

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Chr(KeyAscii) Like "[!A-Z]" And _
    Chr(KeyAscii) Like "[!a-z]" And _
    KeyAscii <> 32 Then
        KeyAscii = 0
End If
End Sub

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 



...and I fish for the halibut!

Skip,

[red]Be advised:[/red] [glasses]
Alcohol and Calculus do not mix!
If you drink, don't derive! [tongue]
 


...and when I pull up a bunch of seaweed...

it looks like kale to me!

Skip,

[red]Be advised:[/red] [glasses]
Alcohol and Calculus do not mix!
If you drink, don't derive! [tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top