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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for illegal characters when saving

Status
Not open for further replies.

tolan

IS-IT--Management
Jun 9, 2001
15
GB
Hi

I've written a little program which allows users to generate letters using forms, etc. I'm also attempting to standardise the way users save, which works to a degree.....

The problem I have is when the users are entering the contact details and description for the letter if they enter any of the following invald characters: forward slash (/), backslash (\), greater than sign (>), less than sign (<), asterisk (*), question mark (?), quotation mark (&quot;), pipe symbol (|), colon :)), or semicolon (;).

the debug 5152 appears and all variables are lost and they crash out.

The saving aspect of my code reads:

ActiveDocument.SaveAs FileName:=(&quot;p:\&quot; & GetUserName & &quot;\letters\Let - &quot; & TxtClient & &quot; - &quot; & txtCompany & &quot; - &quot; & Txttoname & &quot; - &quot; & txtHeading1 & &quot; - &quot; & Format(Now(), &quot;dd-mm-yyyy&quot;) & &quot; &quot; & Format(Now(), &quot;h m s&quot;) & &quot;.doc&quot;)

which saves the file in the users home directory and sub folder called letters and then enters various variables, as well as the creation date and time at the end. So a pathname might look like:

p:\john smith\letters\let - Tek Tips - Mr Smith - What a great site - 24-07-2002 10:30.doc

How can I check that no invalid characters have been entered and that the form/module completes? Should the checking for these characters be done when the users clicks on OK or as the user is entering the characters in each text box?

Any help is great appreciated.

Tolan




 
I would be tempted to use the textbox change event to handle this

Enter this code within the Txtbox_Change sub
If len(textbox1.text)>0 then
tStr = right(textbox1.text,1)
Select Case tStr
case is chr(42) or chr(47) or chr(49)' * / msgbox &quot;Invalid character - please try again&quot;
tStr = left(tStr,1)
textbox1.text = tStr
case else
End Select
Else
End If
Rgds
~Geoff~
 
Tolan,

I would agree with Geoff that it makes sense to inspect each character as it is typed rather than prior to supplying the string to the SaveAs method. Otherwise, the user has the chance of entering multiple illegal characters resulting in repeated notifications while he tries to find the illegals. Just much cleaner.

I want to modify Geoff's code somewhat. Mainly, the lines
Code:
tStr = Left(tStr,1)
TextBox1.Text = tStr
leave the offending character in the textbox string.

Code:
Private Sub TextBox1_Change()
Dim Length As Integer

With TextBox1
  Length = Len(.Text)
  If Length > 0 Then
  Select Case Right(.Text, 1)
  Case Chr(42), Chr(47), Chr(92) ' * /     MsgBox &quot;Invalid character - please try again&quot;
    .Text = Left(.Text, Length - 1)
  Case Else
  End Select
  End If
End With
End Sub

One other note: I would test the length of the combined string of path and filename to ensure it does not exceed the Windows limit (which I believe is 256 characters).

Regards,
M. Smith
 
Just as I was gonna do a re-post cos I noticed exactly the same thing - d'oh
Nice one Mike <g> Rgds
~Geoff~
 
Thanks guys, that's a great help! Appreciated!

Tolan
 
You'll still need to get all the illegal chr codes - the only ones this checks for are * / \ - the chr codes can change dependant on font as well so you might want to set the font of the textbox on form load just in case. To add other chr codes to exclude, just carry on the Case chr(42) line with as many different chrs as you want to exclude
Rgds
~Geoff~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top