PC Review


Reply
Thread Tools Rate Thread

Checking characters

 
 
Mats Samson
Guest
Posts: n/a
 
      11th Aug 2009
RI've a Userform where a textbox picks its value as a proposal from a
worksheet cell when initiating.
This cell may contain invalid filename characters like %&/.
I want the user alerted so they may change the characters in the textbox
before proceeding. The corrected textbox string will be saved in another cell.
regards
Mats
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      11th Aug 2009
Try this Textbox1 Exit event...code...You can add more invalid characters to
the variable strInvalid...

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim strInvalid As String, intTemp As Integer
strInvalid = "%&/."
For intTemp = 1 To Len(strInvalid)
If InStr(Me.TextBox1.Text, Mid(strInvalid, intTemp, 1)) > 0 Then
MsgBox "Invalid character present " & strInvalid
Cancel = True
End If
Next
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Mats Samson" wrote:

> RI've a Userform where a textbox picks its value as a proposal from a
> worksheet cell when initiating.
> This cell may contain invalid filename characters like %&/.
> I want the user alerted so they may change the characters in the textbox
> before proceeding. The corrected textbox string will be saved in another cell.
> regards
> Mats

 
Reply With Quote
 
Mats Samson
Guest
Posts: n/a
 
      11th Aug 2009
Thank you Jakob,
it worked fine!
Cheers
Mats

"Jacob Skaria" wrote:

> Try this Textbox1 Exit event...code...You can add more invalid characters to
> the variable strInvalid...
>
> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> Dim strInvalid As String, intTemp As Integer
> strInvalid = "%&/."
> For intTemp = 1 To Len(strInvalid)
> If InStr(Me.TextBox1.Text, Mid(strInvalid, intTemp, 1)) > 0 Then
> MsgBox "Invalid character present " & strInvalid
> Cancel = True
> End If
> Next
> End Sub
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Mats Samson" wrote:
>
> > RI've a Userform where a textbox picks its value as a proposal from a
> > worksheet cell when initiating.
> > This cell may contain invalid filename characters like %&/.
> > I want the user alerted so they may change the characters in the textbox
> > before proceeding. The corrected textbox string will be saved in another cell.
> > regards
> > Mats

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      11th Aug 2009
You don't need to loop through all the characters in the TextBox to do this;
you can make use of the Like operator instead...

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Me.TextBox1.Text Like "*[%&/.]*" Then
MsgBox "One of these Invalid character are present: %&/."
Cancel = True
End If
End Sub

However, for a general solution, I think there are too many invalid
characters to list. Using the valid character list at this link...

http://support.microsoft.com/kb/177506

Here is a function that can be called to test if a text string is composed
of valid characters or not...

Function IsValidFileName(FileName As String) As Boolean
IsValidFileName = InStr(FileName, "]") > 0 Or Not FileName _
Like "*[! 0-9A-Za-z^&'@{}[,$=!#()%.+~_-]*"
End Function

The OP can then use this function like this...

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsValidFileName(Me.TextBox1.Text) Then
MsgBox "Invalid character present!"
Cancel = True
End If
End Sub

--
Rick (MVP - Excel)


"Jacob Skaria" <(E-Mail Removed)> wrote in message
news:C55969D1-48DA-41B4-A157-(E-Mail Removed)...
> Try this Textbox1 Exit event...code...You can add more invalid characters
> to
> the variable strInvalid...
>
> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> Dim strInvalid As String, intTemp As Integer
> strInvalid = "%&/."
> For intTemp = 1 To Len(strInvalid)
> If InStr(Me.TextBox1.Text, Mid(strInvalid, intTemp, 1)) > 0 Then
> MsgBox "Invalid character present " & strInvalid
> Cancel = True
> End If
> Next
> End Sub
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Mats Samson" wrote:
>
>> RI've a Userform where a textbox picks its value as a proposal from a
>> worksheet cell when initiating.
>> This cell may contain invalid filename characters like %&/.
>> I want the user alerted so they may change the characters in the textbox
>> before proceeding. The corrected textbox string will be saved in another
>> cell.
>> regards
>> Mats


 
Reply With Quote
 
Mats Samson
Guest
Posts: n/a
 
      11th Aug 2009
Thank you Rick,
that works well too.
It's no big deal in my case. The value picked to the textbox is a customer
order number that will be used later on in the filename for reference. In
most cases the invalid character is a : or a /. Will keep your solutions in
mind, new things happens
all the time!
Regards
Mats

"Rick Rothstein" wrote:

> You don't need to loop through all the characters in the TextBox to do this;
> you can make use of the Like operator instead...
>
> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> If Me.TextBox1.Text Like "*[%&/.]*" Then
> MsgBox "One of these Invalid character are present: %&/."
> Cancel = True
> End If
> End Sub
>
> However, for a general solution, I think there are too many invalid
> characters to list. Using the valid character list at this link...
>
> http://support.microsoft.com/kb/177506
>
> Here is a function that can be called to test if a text string is composed
> of valid characters or not...
>
> Function IsValidFileName(FileName As String) As Boolean
> IsValidFileName = InStr(FileName, "]") > 0 Or Not FileName _
> Like "*[! 0-9A-Za-z^&'@{}[,$=!#()%.+~_-]*"
> End Function
>
> The OP can then use this function like this...
>
> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> If Not IsValidFileName(Me.TextBox1.Text) Then
> MsgBox "Invalid character present!"
> Cancel = True
> End If
> End Sub
>
> --
> Rick (MVP - Excel)
>
>
> "Jacob Skaria" <(E-Mail Removed)> wrote in message
> news:C55969D1-48DA-41B4-A157-(E-Mail Removed)...
> > Try this Textbox1 Exit event...code...You can add more invalid characters
> > to
> > the variable strInvalid...
> >
> > Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> > Dim strInvalid As String, intTemp As Integer
> > strInvalid = "%&/."
> > For intTemp = 1 To Len(strInvalid)
> > If InStr(Me.TextBox1.Text, Mid(strInvalid, intTemp, 1)) > 0 Then
> > MsgBox "Invalid character present " & strInvalid
> > Cancel = True
> > End If
> > Next
> > End Sub
> >
> > If this post helps click Yes
> > ---------------
> > Jacob Skaria
> >
> >
> > "Mats Samson" wrote:
> >
> >> RI've a Userform where a textbox picks its value as a proposal from a
> >> worksheet cell when initiating.
> >> This cell may contain invalid filename characters like %&/.
> >> I want the user alerted so they may change the characters in the textbox
> >> before proceeding. The corrected textbox string will be saved in another
> >> cell.
> >> regards
> >> Mats

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking for special characters =?Utf-8?B?ZHdpZ2h0?= Microsoft C# .NET 6 28th Apr 2005 06:28 PM
Checking for Characters =?Utf-8?B?UGxheWE=?= Microsoft VB .NET 5 22nd Feb 2005 07:46 AM
Checking for valid characters Brad Microsoft VB .NET 2 7th Nov 2004 07:17 PM
Checking characters Xarky Microsoft C# .NET 1 22nd Oct 2004 02:21 PM
Checking characters using VBA Gary Microsoft Excel Programming 1 4th Aug 2003 05:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:15 PM.