Access VBA - Input Box Content - Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I wonder if this is possible as I have tried several ways and no luck yet...

In my VBA code I have an input box which asks the user to input a name to be
used as part of a file name later on in the code. This works very well, the
problem I have is that lately people have started inputting 'special
characters' such as / < * etc... How can I have the message in the input box
advising to 'Input File Name - (please don't use special characters such as
\/:*?"<>|)'

When I type the above it recognises < or " etc as part of the code and not
as text, how can I prevent this?

I hope that makes sense, any help would be greatly appreciated.

Kind Rgds,
 
Code like the following should work. Your problem may be that you failed to
double up on the internal quote mark. If you want a " mark in the middle of
a string that you are building you have to put two quotes marks.

strValue = Inputbox("Input File Name - (please don't use special characters
such as \/:*?""<>|)'")
 
Here is a function that will solve your problem. Just put all the characters
you want to omit in the constant conBadChars. It returns True if the input
string has no bad characters and false if it does. Then you can put your
call to the input box in a loop so the user has to retry until they get it
right or give up. The sample below the function shows how you can do that.
The use will have to click cancel or hit enter on the input box without
entering any data to cancel the loop

Function CheckSpecialCharacters(strInput As String) As Boolean
Dim lngCtr As Long
Dim lngLen As Long
Const conBadChars As String = "<>!~`^&*#-+={}[]"""

CheckSpecialCharacters = True
lngLen = Len(strInput)
For lngCtr = 1 To lngLen
If InStr(conBadChars, Mid(strInput, lngCtr, 1)) <> 0 Then
CheckSpecialCharacters = False
Exit For
End If
Next lngCtr

End Function
********** start of loop

Do While True
strSomeVar = InpubBox("Gimme FileName")
if strSomeVar = "" Then ' Nothing entered, exit the loop
Exit Do
End If
if CheckSpecialChars(strSomeVar) = False Then 'Has unallowed
characters
If MsgBox("Invalid Characters, Retry?", vbOkCancel) = vbCancel
Then
Exit Do
Else
'String is good - do what ever here
Exit Do
End If
End If
Loop
 
Hi,

I wonder if this is possible as I have tried several ways and no luck yet...

In my VBA code I have an input box which asks the user to input a name to be
used as part of a file name later on in the code. This works very well, the
problem I have is that lately people have started inputting 'special
characters' such as / < * etc... How can I have the message in the input box
advising to 'Input File Name - (please don't use special characters such as
\/:*?"<>|)'

When I type the above it recognises < or " etc as part of the code and not
as text, how can I prevent this?

I hope that makes sense, any help would be greatly appreciated.

Kind Rgds,

Note the doubling up of the " within the string and the added " at the
end of the string.

= InputBox("Input File Name - (please don't use special " _
& "characters such as \/:*?""<>|)' ", "Input name")
 
Back
Top