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

How to restrict input for InputBox to certain characters?

Status
Not open for further replies.

rhyno2k

IS-IT--Management
Jun 9, 2001
222
US
Hi,


I have a small Outlook Macro that calls up an InputBox. The content of this determines the filename to be saved.

We're running into problems with people putting in restricted characters (e.g. slashes), and breaking functionality. The script seemingly completes successfully, but the file ("MyCo / Purchase.pdf", e.g.) is never saved.

How can I restrict my InputBox to only accept letters, numbers, spaces, dashes and underscores?


Thanks,
--RHYNO
 
I'd check the input after it's entered, then if it doesn't match your expected format, kick it back out to the user.

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
You may also use the Replace function:
strPath = Replace(strPath, "/", "_")
strPath = Replace(strPath, ":", "_")
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I have a few Replace()s in place, but in my experience there are dozens of characters that can futz up a Windows file write... so accounting for every one, especially when these input strings could contain any number of foreign characters, is incomplete and less than ideal.

Is there anything along the lines of a regex/keypress event limiter available? (e.g. only 'register' alphanumeric keystrokes)
 
Yes, you can make a regex do that...

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Could you not use a loop for example

Code:
    Filenamegood = False
    Errormsg = ""
    While Not Filenamegood
    vv = InputBox("Enter Filename" & Errormsg)
    Filenamegood = True
    For counter = 1 To Len(vv)
        If Asc(UCase(Mid(vv, counter, 1))) > 90 Or Asc(UCase(Mid(vv, counter, 1))) < 65 Then
            If Asc(UCase(Mid(vv, counter, 1))) > 57 Or Asc(UCase(Mid(vv, counter, 1))) < 48 Then
                Filenamegood = False
                Errormsg = Chr(13) & "Please only use numbers and letters"
            End If
        End If
    Next counter
    Wend

You would have to have anothr if then or 2 to be able to include spaces and other nonalphanumeric if you want them

ck1999
 
Is the Inputbox only for the filename (not the path etc.)?

What is your criteria? Only characters and numbers? Do they need to specify the extension?

Regexp can handle this easily but we'd need to know the answers to at least the above questions to provide a useful answer.

Cheers

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
rhyno2k,
Migrate your small Outlook Macro into a small Outlook [tt]UserForm[/tt] with a [tt]TextBox[/tt] that accepts the text your currently using the [tt]InputBox[/tt] for.

Benefit: The [tt]UserForm[/tt] can inspect the key strokes as the user types ensuring the filename for your macro is valid by dropping the 'bad' characters at runtime.

Just a thought...
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Is your input box allowed the full file specification?
Is your input box allowed path specification that do not yet exist?

What sort of validation do you need, this "MyCo/Purchase.pdf"

Do you need to resolve the full path of MyCo?

Answer these questions and I can give you the code for doing this...
 

[tt] putting in restricted characters (e.g. slashes), and breaking functionality[/tt]

you're looking at this from the wrong end.

you should trust the user to know what they want, and the operating system to know what it needs.

chances are your users know their own file systems better than you do.

why shouldn't they put in "myfolder\myfile.txt" if that's what they want?

what you should do is exactly what you're doing now, pass whatever they type in to the operating system with no checking.

by definition the operating system knows what's a valid filename way better than you ever could.

i came across programs for years after 3.1 was dead and buried that still checked filenames for valid 8.3 format. if you do any checking of filenames yourself you risk running into just that problem as well as many others.

when the operating system throws a fit, deal with the error and re-prompt the user with an option to cancel.

hth,


mr s. <;)

 
misterstick said:
you should trust the user to know what they want
You're a braver man than me...[wink]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
ditto Harley - a LOT of users don;t understand / know that $%^ etc cannot be used in file names

Having said that, there might be merit in an error trapping loop that just re displays the inputbox with a messgae of "Illegal character entered - please enter file name again" if an error is generated...



Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
the ones that don't couldn't care less.

the ones that do get very annoyed indeed.


mr s. <;)

 
xlbo said:
don;t understand / know that $%^ etc cannot be used in file names

<ahem> All of your example characters can be used in Windows filenames
 
yeh ok strongm - I just randomly hit number characters with the shift key down....my bad [blush]

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Hi guys,

Was out for a while - had a few Drs. appointments...

My macro is used by a few office admins to copy incoming purchase orders attached to emails to a folder on the web server running an intranet web app I created. The folder location is fixed. And it's exclusively for .PDFs, so the extension is added in script.

The primary criteria for the filename is simply one that is Windows compatible (so the file will copy!).

CK, I'll try your loop. If that doesn't work, I'll look into Forms.

Surprised there isn't a FilePathOKChars() function or something that would sanitize a string for filename ops... I run into this problem with a couple of legacy ASP scripts I use as well.
 
I generally find this is pretty handy;

Function VFileName$(Text$, Optional ReplaceWith$ = "_")

Dim Temp$, i%

Temp$ = Space$(256)
LSet Temp$ = Text$

For i = 1 To Len(Trim$(Text$))

Select Case Mid$(Temp$, i, 1)

Case "A" To "Z", "a" To "z", "0" To "9", "$", "&", "#", "@", "!", "%", "'", "`", "(", ")", "{", "}", "-", "_"
'all valid chars

Case Else
Mid$(Temp$, i, 1) = ReplaceWith$

End Select

Next
VFileName$ = Trim$(Temp$)

End Function
 
If the folder is fixed, and the extension is added, then really all you are trying to get is a name...not a path or extention, right?

faq219-2884

Gerry
My paintings and sculpture
 
@fumei, that's right. I want to prevent filename input that would cause a file not to be written, try to create a new directory (\), etc.

@HughLerwill, I'll try your code.
 

misterstick,

Is this really what you meant to say:

the ones that don't couldn't care less.

the ones that do get very annoyed indeed.

If I understand xlbo's post this wouldn't be a problem, as the ones that do know wouldn't be getting the error message in the first place, and the ones that don't wouldn't be able to ignore the message as they couldn't continue until they got it right.

Maybe I'm just dense today, but I don't get your objection.[ponder]

[glasses]

----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top