access database field names in Javascript

G

Guest

Hi;

I trying to get information from a simple MS Access database named users
which consists of a single table called user_info with three fields
(username, password and stylesheet). Username and password are string fields
and stylesheet is a simple integer. I've created a Javascript form called
user_form with two fields: username and password. The form also has two
buttons; reset and submit.

When clicked, the submit button will ensure that both form fields are not
empty or null then create a sql select statement to locate the info in the
database. The database select statement is as follows:

SQLstr = "SELECT [stylesheet] FROM users WHERE
(([username] = document.user_form.username.value) AND
([password] = document.user_form.password.value))";

The fields in square brackets are the database fields. What I'm trying to do
is get the stylesheet number (name of the stylesheet like 2.css ) of the
appropriate record and pass it to a webpage so that the webpage will obey the
statements in the stylesheet. The other fields i.e.
document.user_form.username.value are the field values from the form.

Are the square bracket fields correct or should they be something like
users.user_info. username, etc. ?

The database connection I'm using and where the SQLstr will be used is:

// connection string construction
var strConn = "Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Server.MapPath("user.mdb");

// connection object creation
var dbConn = Server.CreateObject("ADODB.Connection");

// open the connection
dbConn.Open(strConn);

// Execute the query with constructed SQLstr
var rs = dbConn.Execute(SQLstr);

Any help is greatly appreciated.
 
B

Brendan Reynolds

You need quotes around the string values, and you need to concatenate the
values of your control references into the SQL statement rather than the
references themselves. In the example below, I've inserted spaces between
single and double quotes. This solely for illustration, so that you can see
which is which - the spaces should not exist in your actual code. The square
brackets are not required unless there are spaces or other problematic
characters in the field names, but they should not cause any problems.

SQLstr = "SELECT [stylesheet] FROM users WHERE
(([username] = ' " + document.user_form.username.value + " ') AND
([password] = ' " + document.user_form.password.value + " '))";
 
G

Guest

Brendan Reynolds said:
You need quotes around the string values, and you need to concatenate the
values of your control references into the SQL statement rather than the
references themselves. In the example below, I've inserted spaces between
single and double quotes. This solely for illustration, so that you can see
which is which - the spaces should not exist in your actual code. The square
brackets are not required unless there are spaces or other problematic
characters in the field names, but they should not cause any problems.

SQLstr = "SELECT [stylesheet] FROM users WHERE
(([username] = ' " + document.user_form.username.value + " ') AND
([password] = ' " + document.user_form.password.value + " '))";

--
Brendan Reynolds (MVP)


jjfjr said:
Hi;

I trying to get information from a simple MS Access database named users
which consists of a single table called user_info with three fields
(username, password and stylesheet). Username and password are string
fields
and stylesheet is a simple integer. I've created a Javascript form called
user_form with two fields: username and password. The form also has two
buttons; reset and submit.

When clicked, the submit button will ensure that both form fields are not
empty or null then create a sql select statement to locate the info in the
database. The database select statement is as follows:

SQLstr = "SELECT [stylesheet] FROM users WHERE
(([username] = document.user_form.username.value) AND
([password] = document.user_form.password.value))";

The fields in square brackets are the database fields. What I'm trying to
do
is get the stylesheet number (name of the stylesheet like 2.css ) of the
appropriate record and pass it to a webpage so that the webpage will obey
the
statements in the stylesheet. The other fields i.e.
document.user_form.username.value are the field values from the form.

Are the square bracket fields correct or should they be something like
users.user_info. username, etc. ?

The database connection I'm using and where the SQLstr will be used is:

// connection string construction
var strConn = "Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Server.MapPath("user.mdb");

// connection object creation
var dbConn = Server.CreateObject("ADODB.Connection");

// open the connection
dbConn.Open(strConn);

// Execute the query with constructed SQLstr
var rs = dbConn.Execute(SQLstr);

Any help is greatly appreciated.
Brendan;

Thanks for the response. I put the changes in as you suggested as follows:

SQLstr = "SELECT [stylesheet] FROM users WHERE
(([username] = '" + document.user_form.username.value + "') AND
([password] = '" + document.user_form.password.value + "'))";

When I try to run the page I get an unterminated string constant error at
line 22, column 48. This location is at the second "m" in
"document.user_form.username.value" and I'm at a loss as to why this has
occurred. Any ideas?

Thanks;

JJFJR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top