sms student results

and1_hotsauce

Registered User.
Local time
Yesterday, 22:04
Joined
Aug 29, 2011
Messages
17
Hi,
I would like to create an sql statement where it verifies a number and password before proceeding to the next statement.

Why is there a single and double apostrphpe enclosing the statement &intStdNO&. And also what do the two ands mean?

"SELECT * FROM tblStudentInfo WHERE ID = '" & int StdNo & " ' "

I've tried
"SELECT * FROM tblStudentInfo WHERE ID = '" & int StdNo & " ' AND '" & txtPassword & "' "

Is this correct?
 
Last edited:
One way to understand this is to think about what you are trying to end up with. Suppose your SQL statement is supposed to look like this (note that here, 3 doesn't have quotes but ILoveJane does):
Code:
SELECT * FROM tblStudentInfo WHERE ID=3 AND Password="ILoveJane"
where the values 3 and ILoveJane come from intStdNo and txtPassoword respectively. We need to construct a string that will equate to the above.

First of all notice that I've included Password=. Presumably you have a field in tblStudentInfo that is called Passoword or something similar?

Next we have to thing about how we contruct the string. So we are using the concatenator (&) to put the elements of the string together.

Supose we try this (without the single quotes).
Code:
"SELECT * FROM tblStudentInfo WHERE ID = " & int StdNo & " AND Password=" & txtPassword
Note that double quotes here are used to identify text whereas intStdNo and txtPassword do not have any quotes because they are variables and we actually want to use the value of those variables (rather than simply the textual name).

The resulting string is:
Code:
SELECT * FROM tblStudentInfo WHERE ID = 3 AND Password=ILoveJane
Now the important bit.... If ID is an integer then ID=3 will work fine in SQL. However, if it is not then it needs quotes. We'll assume it's integer since that's the implecation.

The Password=ILoveJane bit will definately note work. In SQL the text has to be enclosed in quotes. So the following should work fine:
Code:
SELECT * FROM tblStudentInfo WHERE ID = 3 AND Password="ILoveJane"
So you see that we have got add some quotes to the ILoveJane bit. You'd kind of hope you could just do this:
Code:
""" & "ILoveJane" & """
In other words I've just wrapped the double quotes that I want to add in quotes. But the SQL engine really gets confused by this. Luckily we can aim for this instead (add single quotes):
Code:
'ILoveJane'
So the string expression will be:
Code:
"'" & "ILoveJane" & "'"
But of course we're really using txtPassword not ILoveJane so:
Code:
"'" & txtPassword & "'"
And the full correct expression is:
Code:
"SELECT * FROM tblStudentInfo WHERE ID =" & intStdNo & " AND Password='" & txtPassword & "'"
A good way to test your expression is to do the following to see what the final string looks like (where mySQLstring is your string expression):
Code:
Debug.Print mySQLstring
You should also be able to copy/paste the string into the query SQL window and run it (to test it works ok).

Hope that makes sense.
Chris
 
Hi
Thanks for your reply. I tried out the sql statement you had wrote and it did ineed work. But there are some steps I don't understand.

I understand that ILoveJane has to be in quotes because it is a string. But at this point why are you using concatenators? The first & links the string but what does the second & do?


So you see that we have got add some quotes to the ILoveJane bit. You'd kind of hope you could just do this:
Code:
""" & "ILoveJane" & """
Chris
 
In the following expression we are linking three strings together. I've hightlighted the string parts that will be linked. The remaining pairs of double quotes that are not highlighted are there to identify the strings:

Code:
"[COLOR="Red"]"[/COLOR]" & "[COLOR="red"]ILoveJane[/COLOR]" & "[COLOR="red"]"[/COLOR]"

The resulting string is:

"ILoveJane"

So that's why there are two concatenators (&). To glue three strings together you need two glues.

Chris

By the way, I often have to help fixed colleagues PC's. And when I do I have to ask their password. It's surprising how many of the younger girls passwords are of the format "ILoveXXXX" where XXXX is the latest boyfriend. Hence the tongue in cheek choice of password :)
 
And the full correct expression is:
Code:
"SELECT * FROM tblStudentInfo WHERE ID =" & intStdNo & " AND Password='" & txtPassword & "'"

Yes I'm starting to understand now. Just one more step!

at this point Password = ' " & txtPassword & " '

What are the two inner double quotes for? Are they to show vbs that txtPassword is a string?
 
They are not inner quotes at all. txtPassword is a variable and therefore sits outside the quotes. Consider the slightly larger expression:

" AND Password='" & txtPassword & "'"

The red bits are text (including the single quotes. The blue bits are the double quote pairs for the text.

Hopefully you can see that you are sandwiching the value from txtPassword between two bits of text.

hth
Chris
 

Users who are viewing this thread

Back
Top Bottom