NEVER concatenate user inputs into any SQL statement, use proper parameters instead.
This means your SQL should look like this:
sql = "SELECT [UserName], [Password] FROM tblLogin WHERE [UserName]=? AND [Password]=?;"
Note that there is no need to track whether a column wants single quotes or not, which makes things much more robust, not to mention much more secure. Also the unbalanced parentheses issue becomes irrelevant.
The sql
string should be used in some ADO Command
or DAO (? ...not really familiar with Access) QueryDef
object, as the command's CommandText
or the querydef's definition.
Then you add Parameters
to the ADO command and supply their values in the order they appear in the SQL command string, or set the named querydef parameters' respective Value
accordingly.
Exactly how that's done depends on what type of Connection
you're working with; this answer shows how you can use DAO QueryDefs
in Access to do this, and this answer shows how you can use ADO to do the same with a Command
and Parameter
objects.
As a security note, I need to mention that storing password in plain text in a database is a very bad idea. Best practice would be to salt+hash the passwords, and only store the resulting hashes in the database; login is successful not when the user input matches the stored password, but when the salted user input produces the same hash value as the one stored for the claimed login: neither the code, the database, nor the developer actually needs to know anyone's passwords. This is important, because humans have this tendency to reuse passwords elsewhere, so if weak security isn't a problem for this particular application, it becomes a problem when a user decides to reuse their Facebook login for it, or to reuse their password for [other app whose security is actually important].