Commit aef4fa54 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9781565924154

0 → 120000
+1 −0
Changes for 9781565924154: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9781565924154
 No newline at end of file

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
	  
# ADO:  ActiveX Data Objects, by Jason Roff
	  
[![ADO:  ActiveX Data Objects, by Jason Roff](http://akamaicovers.oreilly.com/images/9781565924154/cat.gif)](https://www.safaribooksonline.com/library/view/title/1565924150//)
	  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
	  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
	  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
	  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
	  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

ado.appa.txt

0 → 100644
+143 −0
Changes for ado.appa.txt: 143 added lines, 0 removed lines.
Original line number Diff line number Diff line
SELECT DISTINCT Make
FROM Automobile
ORDER BY Make;

Make
BMW
Chevy
Ford
Mercury

SELECT Make, Model, Color
FROM Automobile
ORDER BY Color DESC, Make ASC;

Make     Model    Color
Chevy    Nova     Red
Ford     Mustang  Red
Mercury  Capri    Brown
Chevy    Camaro   Blue
Mercury  Sable    Blue 
BMW      323I     Black
---

SELECT Employees.FirstName,
       Employees.LastName,
       Automobile.Make
FROM Employees
LEFT JOIN Automobile
ON (Employees.EmployeeID = Automobile.EmployeeID);

FirstName  LastName   Make
Jason      Roff       Mercury
Jason      Roff       Ford
Jason      Roff       Chevy
Jason      Roff       Chevy
Jason      Roff       Mercury
Tammi      Roff       
Kimberly   Roff       BMW
John       DelWhatta  
John       Katsosing  
---

SELECT Employees.FirstName,
       Employees.LastName,
       Automobile.Make
FROM Employees
LEFT JOIN Automobile
ON (Employees.EmployeeID = Automobile.EmployeeID);

FirstName  LastName   Make
Jason      Roff       Mercury
Jason      Roff       Ford
Jason      Roff       Chevy
Kimberly   Roff       BMW
---

TABLE Employees
UNION ALL
(SELECT *
 FROM Employees
 WHERE (TotalRevenue < 20));

EmployeeID  CrossStitch  Hammocks  Automobiles  TotalRevenue
100         1012         1234      456          21100
101         450          345       45           62758
102         21018        3234      765          1154001
200         17           32        143          991
201         3            1         0            17
---


PARAMETERS
[First Name Parameter] AS STRING,
[Last Name Parameter] AS STRING;
SELECT FirstName, 
       LastName
FROM Employees
WHERE ((FirstName LIKE [First Name Parameter])
       AND (LastName LIKE [Last Name Parameter]);

[First Name Parameter] = "*"
[Last Name Parameter] = "Roff"

FirstName  LastName
Jason      Roff
Tammi      Roff
Kimberly   Roff
---


PROCEDURE ReturnEmployeeID
[First Name Parameter] AS STRING,
[Last Name Parameter] AS STRING;
SELECT FirstName, 
       LastName,
       EmployeeID
FROM Employees
WHERE ((FirstName LIKE [First Name Parameter])
       AND (LastName LIKE [Last Name Parameter]);

[First Name Parameter] = "Jason"
[Last Name Parameter] = "Roff"

FirstName  LastName  EmployeeID
Jason      Roff      100
---


PROCEDURE GetFinancialInformation
[First Name Parameter] AS STRING,
[Last Name Parameter] AS STRING;
SELECT Employees.FirstName,
       Employees.LastName,
       Sales.TotalRevenue
FROM Employees
INNER JOIN Sales
ON (Employees.EmployeeID = Sales.EmployeeID)
WHERE ((Employees.FirstName = [First Name Parameter])
       AND (Employees.LastName = [Last Name Parameter]))
WITH OWNERACCESS OPTION;

[First Name Property] = "Kimberly"
[Last Name Property] = "Roff"

FirstName  LastName  TotalRevenue
Kimberly   Roff      1154001
---

INSERT INTO Automobile
            (EmployeeID, 
             Year,
             Make,
             Model,
             Color,
             Cost)
VALUES      (100,
             1997,
             Chevy
             Cavalier,
             Teal,
             14000);
---

ado.appb.txt

0 → 100644
+345 −0
Changes for ado.appb.txt: 345 added lines, 0 removed lines.
Original line number Diff line number Diff line
Private Sub cmdConnection_Click()

    frmConnection.Show vbModal
    If (frmConnection.Canceled) Then Exit Sub
    
    Set m_oCon = New Connection
    m_oCon.ConnectionString = frmConnection.ConnectionString
    m_oCon.CursorLocation = frmConnection.CursorLocation
    Unload frmConnection
    
    m_oCon.Open
    
    cmdRecordset.Enabled = True
    cmdCommand.Enabled = True
    cmdField.Enabled = False
    Set m_oRst = Nothing
    
    DisplayProperties m_oCon, "Connection"
    
End Sub
---

 Private Sub cmdRecordset_Click()
    
    frmRecordset.Show vbModal
    If (frmRecordset.Canceled) Then Exit Sub
    
    Set m_oRst = New ADODB.Recordset
    
    m_oRst.ActiveConnection = m_oCon
    m_oRst.CursorType = frmRecordset.CursorType
    m_oRst.LockType = frmRecordset.LockType
    m_oRst.Source = frmRecordset.Source
    Unload frmRecordset
    
    m_oRst.Open
    
    cmdField.Enabled = True
    
    DisplayProperties m_oRst, "Recordset"

End Sub
---

Private Sub cmdCommand_Click()
    
    frmCommand.Show vbModal
    If (frmCommand.Canceled) Then Exit Sub
    
    Set m_oCom = New ADODB.Command
    Set m_oRst = New ADODB.Recordset
    
    m_oCom.ActiveConnection = m_oCon
    m_oCom.CommandText = frmCommand.CommandText
    Unload frmCommand
    
    Set m_oRst = m_oCom.Execute
        
    cmdField.Enabled = True
    
    DisplayProperties m_oCom, "Command"

End Sub
---

Private Sub DisplayProperties(oObject As Object, _
                              sUsing As String)
    
    Dim lColumnCount As Long
    Dim lColumnWidth As Long
    
    Dim oProperty As Property
    Dim oListItem As ListItem
    
    lblObject.Caption = sUsing
    
    lColumnWidth = lvwProperties.Width / 4
    For lColumnCount = 1 To 4
        lvwProperties.ColumnHeaders.Add
        lvwProperties.ColumnHeaders(lColumnCount).Width = lColumnWidth
    Next lColumnCount
    
    lvwProperties.ListItems.Clear
    
    For Each oProperty In oObject.Properties
        Set oListItem = lvwProperties.ListItems.Add()
        oListItem.Text = oProperty.Name
        oListItem.SubItems(1) = oProperty.Value & ""
        oListItem.SubItems(2) = GetPropertyType(oProperty.Type)
        oListItem.SubItems(3) = GetAttributes(oProperty.Attributes)
    Next oProperty
    
End Sub
---

Private Function GetAttributes(lAttributes As Long) As String

    If (lAttributes And adPropNotSupported) Then _
        GetAttributes = "Not Supported, "
    
    If (lAttributes And adPropRequired) Then _
        GetAttributes = GetAttributes & "Required, "
    
    If (lAttributes And adPropOptional) Then _
        GetAttributes = GetAttributes & "Optional, "
    
    If (lAttributes And adPropRead) Then _
        GetAttributes = GetAttributes & "Read, "
    
    If (lAttributes And adPropWrite) Then _
        GetAttributes = GetAttributes & "Write, "
    
    If (Right$(GetAttributes, 2) = ", ") Then _
        GetAttributes = Left$(GetAttributes, Len(GetAttributes) - 2)

End Function
---

Private Function GetPropertyType(lType As Long) As String
    Select Case (lType)
        Case adBigInt:              GetPropertyType = "BigInt"
        Case adBinary:              GetPropertyType = "Binary"
        Case adBoolean:             GetPropertyType = "Boolean"
        Case adBSTR:                GetPropertyType = "BSTR"
        Case adChar:                GetPropertyType = "Char"
        Case adCurrency:            GetPropertyType = "Currency"
        Case adDate:                GetPropertyType = "Date"
        Case adDBDate:              GetPropertyType = "DBDate"
        Case adDBTime:              GetPropertyType = "DBTime"
        Case adDBTimeStamp:         GetPropertyType = "DBTimeStamp"
        Case adDecimal:             GetPropertyType = "Decimal"
        Case adDouble:              GetPropertyType = "Double"
        Case adEmpty:               GetPropertyType = "Empty"
        Case adError:               GetPropertyType = "Error"
        Case adGUID:                GetPropertyType = "GUID"
        Case adIDispatch:           GetPropertyType = "IDispatch"
        Case adInteger:             GetPropertyType = "Integer"
        Case adIUnknown:            GetPropertyType = "IUnknown"
        Case adLongVarBinary:       GetPropertyType = "LongVarBinary"
        Case adLongVarChar:         GetPropertyType = "LongVarChar"
        Case adLongVarWChar:        GetPropertyType = "LongVarWChar"
        Case adNumeric:             GetPropertyType = "Numeric"
        Case adSingle:              GetPropertyType = "Single"
        Case adSmallInt:            GetPropertyType = "SmallInt"
        Case adTinyInt:             GetPropertyType = "TinyInt"
        Case adUnsignedBigInt:      GetPropertyType = "UnsignedBigInt"
        Case adUnsignedInt:         GetPropertyType = "UnsignedInt"
        Case adUnsignedSmallInt:    GetPropertyType = "UnsignedSmallInt"
        Case adUnsignedTinyInt:     GetPropertyType = "UnsignedTinyInt"
        Case adUserDefined:         GetPropertyType = "UserDefined"
        Case adVarBinary:           GetPropertyType = "VarBinary"
        Case adVarChar:             GetPropertyType = "VarChar"
        Case adVariant:             GetPropertyType = "Variant"
        Case adVarWChar:            GetPropertyType = "VarWChar"
        Case adWChar:               GetPropertyType = "WChar"
        Case Else:                  GetPropertyType = "Unknown"
    End Select
End Function
---

Option Explicit

Private m_bCanceled As Boolean

Private Sub cmdCancel_Click()
    m_bCanceled = True
    Me.Hide
End Sub

Private Sub cmdOpen_Click()
    m_bCanceled = False
    Me.Hide
End Sub

Public Property Get Canceled() As Boolean
    Canceled = m_bCanceled
End Property
---

Private Sub Form_Load()

    optConnectionString(0).Caption = "DSN=BiblioDSN"
    
    optConnectionString(1).Caption = "driver={SQL Server}; " _
                                   & "server=JROFF_LAPTOP; " _
                                   & "uid=sa; " _
                                   & "database=Northwind"
    
    optConnectionString(2).Caption = "DSN=OtherDSN"
    
    optConnectionString(0).Value = True
    optCursorLocation(0).Value = True

End Sub
---

Public Property Get ConnectionString() As String

    If (optConnectionString(0).Value = True) Then _
        ConnectionString = optConnectionString(0).Caption
    
    If (optConnectionString(1).Value = True) Then _
        ConnectionString = optConnectionString(1).Caption
    
    If (optConnectionString(2).Value = True) Then _
        ConnectionString = optConnectionString(2).Caption
    
    If (optConnectionString(3).Value = True) Then _
        ConnectionString = txtConnectionString.Text
        
End Property
---

Option Explicit

Private m_bCanceled As Boolean

Private Sub cmdCancel_Click()
    m_bCanceled = True
    Me.Hide
End Sub

Private Sub cmdOpen_Click()
    m_bCanceled = False
    Me.Hide
End Sub

Public Property Get Canceled() As Boolean
    Canceled = m_bCanceled
End Property
---

Private Sub Form_Load()
    
    optSource(0).Caption = "Authors"
    
    optSource(1).Caption = "SELECT * FROM Authors;"
    
    optSource(2).Caption = "SELECT * FROM Orders;"
    
    optSource(0).Value = True
    optCursorType(0).Value = True
    optLockType(0).Value = True

End Sub
To enable the text box when the user selects the last option button in the Source control array, enter the following code:
Private Sub optSource_Click(Index As Integer)
    If (Index = 3) Then
        txtSource.Enabled = True
        txtSource.BackColor = vbWhite
    Else
        txtSource.Enabled = False
        txtSource.BackColor = &H8000000F  ' light grey
    End If
End Sub
---

Public Property Get Source() As String

    If (optSource(0).Value = True) Then 
        Source = optSource(0).Caption
    
    If (optSource(1).Value = True) Then _
        Source = optSource(1).Caption
    
    If (optSource(2).Value = True) Then _
        Source = optSource(2).Caption
    
    If (optSource(3).Value = True) Then _
        Source = txtSource.Text
        
End Property
---

Option Explicit

Private m_bCanceled As Boolean

Private Sub cmdCancel_Click()
    m_bCanceled = True
    Me.Hide
End Sub

Private Sub cmdOpen_Click()
    m_bCanceled = False
    Me.Hide
End Sub

Public Property Get Canceled() As Boolean
    Canceled = m_bCanceled
End Property
---

Private Sub Form_Load()

    optCommandText(0).Caption = "SELECT * FROM Authors;"
    
    optCommandText(1).Caption = "DELETE * FROM Authors _
                                 WHERE (Author = 'Jason');"
    
    optCommandText(2).Caption = "SELECT * " _
                     & "FROM Customers " _
                     & "WHERE (ContactTitle = 'Sales Representative') " _
                     & "AND (Country = 'USA'); "
   
    optCommandText(0).Value = True

End Sub
---

Public Property Get CommandText() As String

    If (optCommandText(0).Value = True) Then _
        CommandText = optCommandText(0).Caption
    
    If (optCommandText(1).Value = True) Then _
        CommandText = optCommandText(1).Caption
    
    If (optCommandText(2).Value = True) Then _
        CommandText = optCommandText(2).Caption
    
    If (optCommandText(3).Value = True) Then _
        CommandText = txtCommandText.Text
        
End Property
---

Option Explicit

Private m_bCanceled As Boolean

Private Sub cmdCancel_Click()
    m_bCanceled = True
    Me.Hide
End Sub

Private Sub cmdOk_Click()
    m_bCanceled = False
    Me.Hide
End Sub

Public Property Get Canceled() As Boolean
    Canceled = m_bCanceled
End Property
---
 No newline at end of file

ado.appc.txt

0 → 100644
+193 −0
Changes for ado.appc.txt: 193 added lines, 0 removed lines.
Original line number Diff line number Diff line
Private Sub Form_Load()
    PopulateErrors
End Sub

Private Sub PopulateErrors()
        
    AddToErrorList "Bad connection", BAD_CONNECTION_ERROR
    AddToErrorList "SQL Server not started", SQL_SERVER_NOT_STARTED_ERROR
    AddToErrorList "Bad user name", BAD_USER_NAME_ERROR
    AddToErrorList "Invalid command text value", INVALID_COMMANDTEXT_VALUE_ERROR
    AddToErrorList "Table not available", TABLE_NOT_AVAILABLE_ERROR
    AddToErrorList "Not an ADO error", NOT_AN_ADO_ERROR
    lstErrors.ListIndex = 0
    
End Sub

Private Sub AddToErrorList(sError As String, _
                           lErrorIndex As Long)
                            
    lstErrors.AddItem sError
    lstErrors.ItemData(lstErrors.ListCount - 1) = lErrorIndex

End Sub
---

Private Sub GenerateError(lErrorIndex As Long)
On Error GoTo ERR_GenerateError:

    Dim con As ADODB.Connection
    
    Set con = New ADODB.Connection

    Select Case (lErrorIndex)
        
        Case BAD_CONNECTION_ERROR:
            con.Open "Bad Connection"
    
        Case SQL_SERVER_NOT_STARTED_ERROR:
            MsgBox "Make sure that the SQL Server service is not started.", _
                   vbInformation + vbOKOnly, _
                   "Information"
            con.Open "DSN=NorthwindDSN"
            
        Case BAD_USER_NAME_ERROR:
            MsgBox "Make sure that the SQL Server service is started.", _
                   vbInformation + vbOKOnly, _
                   "Information"
            con.Open "DSN=NorthwindDSN", "INVALID USER"
            
        Case INVALID_COMMANDTEXT_VALUE_ERROR:
            con.Open "DSN=BiblioDSN"
            con.Execute "INVALID COMMANDTEXT VALUE"
            
        Case TABLE_NOT_AVAILABLE_ERROR:
            con.Open "DSN=BiblioDSN"
            con.Execute "MISSINGTABLE", _
                        , _
                        adCmdTable
            
        Case NOT_AN_ADO_ERROR:
            con.Execute "SELECT * FROM Authors;"
            
    End Select

    Set con = Nothing

Exit Sub

ERR_GenerateError:
    
    If (Not frmDisplayADOError.IsAnADORelatedError(Err, con.Errors)) Then
        
        MsgBox Err.Number & ": " & Err.Description, _
               vbExclamation + vbOKOnly, _
               "General Error"
    
    Else
        Resume Next
    End If

End Sub
---

Option Explicit

Private Type TVBAError
    Number As Long
    Description As String
    LastDllError As Long
    Source As String
    HelpContext As Long
    HelpFile As String
End Type

Private m_oVBAError As TVBAError
Private m_oADOErrors As ADODB.Errors

Private Const VBA_ERROR = 1
Private Const ADO_ERROR = 2
---

Private Function AddErrorToList(lErrorNumber As Long, _
                                lErrorType As Long)
                                 
    Dim sErrorType As String
    
    Select Case (lErrorType)
        Case VBA_ERROR: sErrorType = "(VBA)"
        Case ADO_ERROR: sErrorType = "(ADO)"
    End Select
        
    lstErrors.AddItem CStr(lErrorNumber) & " " & sErrorType
    lstErrors.ItemData(lstErrors.ListCount - 1) = lErrorType
        
End Function
---

Public Function IsAnADORelatedError(oVBAError As VBA.ErrObject, _
                                    oADOErrors As ADODB.Errors) As Boolean
    
    Dim oADOError As ADODB.Error
    
    If (oADOErrors.Count = 0) Then
    
        IsAnADORelatedError = False
        Unload Me
    
    Else
    
        m_oVBAError.Number = oVBAError.Number
        m_oVBAError.Description = oVBAError.Description
        m_oVBAError.LastDllError = oVBAError.LastDllError
        m_oVBAError.Source = oVBAError.Source
        m_oVBAError.HelpContext = oVBAError.HelpContext
        m_oVBAError.HelpFile = oVBAError.HelpFile
        
        AddErrorToList m_oVBAError.Number, VBA_ERROR
        
        Set m_oADOErrors = oADOErrors
        
        For Each oADOError In m_oADOErrors
            AddErrorToList oADOError.Number, ADO_ERROR
        Next oADOError
    
        lstErrors.ListIndex = 0
        
        IsAnADORelatedError = True
        Me.Show
    
    End If
                
End Function
---

Private Sub lstErrors_Click()
        
    Dim lIndex As Long
    
    lIndex = lstErrors.ListIndex
    
    Select Case (lstErrors.ItemData(lIndex))
        
        Case VBA_ERROR:
            
            With m_oVBAError
                txtNumber.Text = .Number
                txtDescription.Text = .Description
                txtLastDLLError = .LastDllError
                txtNativeNumber = ""
                txtSQLState = ""
                txtSource = .Source
                txtHelpContext.Text = .HelpContext
                txtHelpFile.Text = .HelpFile & ""
            End With
            
        Case ADO_ERROR:
            
            With m_oADOErrors(lIndex - 1)
                txtNumber.Text = .Number
                txtDescription.Text = .Description
                txtLastDLLError = ""
                txtNativeNumber = .NativeError
                txtSQLState = .SQLState
                txtSource = .Source
                txtHelpContext.Text = .HelpContext
                txtHelpFile.Text = .HelpFile & ""
            End With
              
    End Select
    
End Sub
---
Loading