Asked By
Elmer Zabat
600 points
N/A
Posted on - 06/19/2011
Hi,
I need some help please.
I developed a Customer Management System, were there is a form to input information. My program works fine when inputting letters and numbers to the form. But when I input special characters ,it starts to send error message.
How can i fix my code, so that special characters wont be a problem.
Thank you very much.
Elmer.
Error in Typing Special Characters
Inputting Special Characters on form resulting to Error in Visual Basic Code
There are characters that Visual Basic is using when making Visual Basic Codes. This includes the following list of characters:
-
“+” (Plus/Unary Plus/Positive) – used for concatenating characters, adding values and assigning Unary sign (Positive values).
-
“-“ (Minus/Unary Minus/Negative/Dash/Hyphen/) – used for subtracting numbers, assigning unary numbers (Negative Values).
-
“*” (Asterisk) – used for multiplication (mathematical operator)
-
“/” (Slash) – used for division (mathematical operator).
-
“” (Back Slash) – used for integer division (mathematical operator).
-
“^” (Caret) – Used as a mathematical operator for exponential function.
-
“_” (Underscore) – used to define statement written in multiple rows. The same as concatenation, but in this case the character was used to concatenate a broken statement continued at the next row.
-
“(“ (Open Parenthesis) – used for grouping mathematical statement or formula. Also used to indicate parameters passed to a variable like array or a function and procedures.
-
“)” (Close Parenthesis) – to close the open parenthesis. When closing open parenthesis means ending of the statement inside the parenthesis.
-
“&” (Ampersand) – Used for concatenating string values.
-
“=” (Equal Sign) – used for assignment operator and as Boolean operator (either true or false).
-
“>” (Greater than sign) – used as Boolean operator (either true or false).
-
“<” (Less than sign) – used as Boolean operator (either true or false).
-
“!” (Exclamation Point) – Short cut for fields.
-
“’” (Single quotation) – Used for defining a comments and for quoting a string in query statements.
-
“”” (Double Quotes) – Used for enclosing string values.
-
“#” (Sharp or the Number Sign) – Used to define date value in a query statement.
The above listed characters are all reserved. There are instances using those characters raised and error in your program especially on input values.
Commonly the Double Quotes (“) and the Single quote (‘) generates error when saving to database or storing into values. For example we have an input box in our form where the fields sometimes used double quotes. Let’s say the field input is height, sometimes we input the seven feet and three inches like this: 7’3”. Note: we have used the character Double and Single Quotes. Then this input field will be assigned to a string or text field on a database. Our query statement will goes like this:
CONN.EXECUTE “INSERT INTO Table1 ( test ) VALUES (‘" & TEXT1.TEXT & "’)”;
The statement equals with this:
CONN.EXECUTE “INSERT INTO Table1 (test) VALUES (‘7’3”’);
As you can see the value 7 is enclosed with a single quotation. When the statement interprets by the visual basic compiler, the 3” does not belong to the statement and occurred to be syntax error.
Solution for this anomaly in your program is to generate a module or function that detects for the single quotation and double quotation in the string and double each quotation. For example a double quotes goes like this (“”) while single quotation goes like this (‘’). Visual basic interpreter interprets that the double double-quote inside the enclosed string is a just a double quote as well as the single quote.
If you can’t debug your system, in your input field double the quotes instead of using just one. 7’3” will goes like this: 7’’3””.
Answered By
mactech
0 points
N/A
#126471
Error in Typing Special Characters
All you have to do is to design an error handling module to handle the special character entered by the user and then show an error message to the user if any of the pressed character is a special character.
You can handle it by two ways :
1. Every time a special character is entered by a user, check if the entered character is a special character and take input again for that variable.
2. Take the input from the user, check if it contains any special characters and then remove those characters from the string before program moves further.
3. If you want to have those special character then write a module which will replace any special character entered by user with a predefined number and then process it.
Basically you need to deal with those special character, as soon as user enters it.