VBScript Database Query
VBScript Database Query Lab
‘======================================================================
‘ NAME: ComputersDatabase.vbs
‘
‘ AUTHOR: jlmorgan ,
‘ DATE : 8/19/2011
‘
‘ COMMENT: Use 32 bit ODBC Microsoft Access Driver
‘
‘==========================================================================
recordsStr = “”
sqlStr = “SELECT * FROM Computers”
dataSource = “provider=Microsoft.ACE.OLEDB.12.0;” _
& “data source=C:\Scripts\Computers.accdb”
Set objConnection = CreateObject(“ADODB.Connection”)
objConnection.Open dataSource
Set objRecordSet = CreateObject(“ADODB.Recordset”)
objRecordSet.Open sqlStr , objConnection
objRecordSet.MoveFirst
‘ Display Headers
recordsStr = “Computer HostName Room_Num” & _
” CPU_Type Speed Num_CPUs Bit_Size OS_Type ” & _
” Memory HDD_Size” & vbCrLf & _
“============================================================” & _
“=============================” & vbCrLf
Do Until objRecordSet.EOF
recordsStr = recordsStr & objRecordSet.Fields.Item(“Computer”) & _
vbTab & pad(objRecordSet.Fields.Item(“HostName”),12) & _
vbTab & pad(objRecordSet.Fields.Item(“Room_Num”),14) & _
vbTab & objRecordSet.Fields.Item(“CPU_Type”) & _
vbTab & objRecordSet.Fields.Item(“Speed”) & _
vbTab & objRecordSet.Fields.Item(“Num_CPUs”) & _
vbTab & objRecordSet.Fields.Item(“Bit_Size”) & _
vbTab & pad(objRecordSet.Fields.Item(“OS_Type”),12) & _
vbTab & objRecordSet.Fields.Item(“Memory”) & _
vbTab & objRecordSet.Fields.Item(“HDD_Size”) & vbCrLf
objRecordSet.MoveNext
Loop
objRecordSet.Close
objConnection.Close
WScript.Echo recordsStr
function pad(ByVal strText, ByVal len)
pad = Left(strText & Space(len), len)
end Function
Objective
In this lab, students will complete the following objectives.
• Create a connection to an Access database.
• Create various SQL queries to extract information from a database.
• Format extracted data with column headers.
Element K Network Connections
For this lab, we will only need to connect to vlab-PC1. The computer vlab-PC1 is the computer on the left side while vlab-PC2 is on the right. If you leave the cursor on the PC icon for a few seconds, a tool-tip message will appear indicating the hostname of the PC. Open vlab-PC1 and log in as Administrator with the password password.
Lab Overview
Even though we are only using vlab-PC1 to complete our lab assignment, the database we will be accessing (Computers.accdb) is actually located on the computer vlab-PC2 in the directory C:\Database. This directory is shared as a ReadOnly network share by vlab-PC2. The Universal Naming Convention (UNC) name for this share is \\vlab-PC2\Database. Our VBScript program vlab-PC1 will have to open the \\vlab-PC2\Database share and map it to the local X: drive. The path specified fro the database will then be X:Computers.accdb.
The IT department maintains an Access database on vlab-PC2 that is used to inventory the computers in the various rooms. Fields in the database include: Computer Type, Hostname, Room Number, CPU Type, Number of Bits, Speed, Number of Processors, Operating System, Memory, and Hard Drive Size. We need to query this database to determine upgrades and replacements for existing computers.
Below (and on the following page) is a listing of the Computers.accdb database contents:
Task 1: Understanding the Net Use Commands in ComputerDatabase.vbs
• Open Notepad++. Use the menu option File/Open to open the VBScript program: C:\Scripts\ComputerDatabase.vbs.
Task 2: Understanding the ADODB.Connection and ADODB.Recordset Objects
• In NotePad++, look at the following code lines.
Line 11 contains the SQL Query String named sqlStr. This is the line you will have to modify to properly query the Computer database. The SQL Query “SELECT * FROM Computers” will select all fields from the database table Computers.
Lines 12 and 13 uses a string named dataSource to specify the Microsoft Driver and the name and location of the local database: X:Computers.accdb.
Line 14 Creates the “ADODB.Connection” object while line 15 opens the connection to the database.
Line 16 Creates the “ADODB.Recordset” object while line 17 provides access to the records using the SQL Query String and the Connection object. Line 18 moves the objRecordSet pointer to the first record.
Task 3: Displaying the Record Headers and Database Records
• In NotePad++, look at the following lines of the ComputerDatabase.vbs program.