- The connection to the database is a required part of VBScript database programming. The connection holds the values of the username and password, server name and database used in the application. Below is the syntax for creating a connection to the database in VBScript:
<%
Set connection = Server.CreateObject("ADODB.Connection")
FilePath = Server.MapPath("MyCustomers.mdb")
connection.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & FilePath & ";"
%>
Notice the "%>" in the lines of code. This string of characters is used in the HTML files of VBScript to denote the usage of back-end code. Anything placed within the "<% %>" characters are processed on the server prior to rendering the HTML page for the user. Additionally, the code within these markers is not seen by the user, so the sensitive data like username and password is secure. - Now that the connection has been made, a query can be used to retrieve some data. In this example, a list of employee first names is retrieved from the database. Below is an example of a query assigned to a string variable:
query = "select first_name from employees"
Set records = connection.Execute(query)
The first line of code is the proper syntax for a SQL query. It simply retrieves a list of first names for employees. The second line of code calls the server, retrieves the data and assigns it to the records variable. Once the data has been assigned, the variable can be used to display values to the user. - The records variable holds the data for display, so now the application can print it to the HTML page. Below is an example of printing the first record to an HTML element:
<p><%=records("first_name")%></p>
Notice how the VBScript is surrounded by the "<% %>" characters again. This is how the programmer inserts VBScript code within an HTML element. The VBScript is processed prior to rendering the HTML, but it is still wrapped within the HTML paragraph tags by the browser.
previous post