Database Command - SQL String with SELECT

Command: SQL String with SELECT
   
Description: The SQL String is a string passed to the .Open method of the recordset object (rst). This string defines the recordset, i.e. - which table of the database we will be using, which fields we will be using from the table, etc. SELECT designates which fields we want included in the recordset. FROM designates which table we will use. We can further define the recordset by using the WHERE keyword.

SELECT <* for all fields or select fields separated by commas> FROM TableA WHERE KeyField <expression value1>
   

Example1:
    Program
        'This example will select records from the MailBoxes table whose BoxNumber is equal to the caller’s input.
        'Single Quotes are required around Code in the Select statement as BoxNumber is defined as a text data type in the Mailboxes table.
        ' Setup database connection.  Change your server and database name in your connection string
        strConnect = “Provider=SQLOLEDB;Server=<server>;DATABASE=CALLMasterSQL;UID=;PWD=;Integrated Security=SSPI;”
        Code = $tt
        Set CMvarSet = New Recordset
        strSQL = “SELECT * FROM MailBoxes WHERE BoxNumber ='”
        strSQL = strSQL & Code
        strSQL = strSQL & “'”
        ' The SELECT is normally followed by an Open command to open the database and find record
        CMvarSet.Open strSQL strConnect
        ‘Add other processing statements to get the data then close the recordset and database.

    EndProgram

 

Example 2:
    Program
        'This example will select records from the MailBoxes table whose BoxStatus is equal to the caller’s input.
        'Single Quotes are NOT required around BoxStat in the Select statement as BoxStatus is defined as a number data type in the Mailboxes table.
        ' Setup database connection.  Change your server and database name in your connection string
        strConnect = “Provider=SQLOLEDB;Server=<server>;DATABASE=CALLMasterSQL;UID=;PWD=;Integrated Security=SSPI;”
        BoxStat = $tt
        Set CMvarSet = New Recordset
        strSQL = “SELECT * FROM MailBoxes WHERE BoxStatus =”
        strSQL = strSQL & BoxStat
        strSQL = strSQL & “”
        ' The SELECT is normally followed by an Open command to open the database and find record
        CMvarSet.Open strSQL strConnect
        ‘Add other processing statements to get the data then close the recordset and database.

    EndProgram