Article ID: 149027
Article Last Modified on 10/11/2006
Sub SQLOpen_Example()
Dim Chan As Variant
Dim NumberOfColumns As Variant, NumberOfRows As Variant
Dim ContentsProtected As Boolean
Dim ObjectsProtected As Boolean, ScenariosProtected As Boolean
' Open the Nwind data source and check for connection errors
Chan = SQLOpen("DSN=Nwind")
If IsError(channel) Then
MsgBox SQLError()(3)
Exit Sub
End If
' Execute the query statement and check for SQL statement errors
NumberOfColumns = SQLExecQuery(Chan, "SELECT * FROM Customer")
If IsError(NumberOfColumns) Then
MsgBox SQLError()(3)
SQLClose channel
Exit Sub
End If
' Set an object variable to reference Sheet1
Set thesheet = Worksheets("Sheet1")
With thesheet
' If sheet is protected, then store the current values
' of the ProtectDrawingObjects and ProtectScenarios
' properties so they can be reset later.
If .ProtectContents = True Then
ContentsProtected = True
ObjectsProtected = .ProtectDrawingObjects
ScenariosProtected = .ProtectScenarios
' Unprotect the sheet and assume no password is needed
.Unprotect
End If
End With
' Retrieve data to worksheet and check for retrieval errors
NumberOfRows = SQLRetrieve(Chan, Range("Sheet1!A1"), , , True)
If IsError(NumberOfRows) Then
MsgBox SQLError()(3)
End If
' Close the connection
SQLClose Chan
' If the sheet was previously protected, then reprotect it
' with the original settings. No password is used in this
' example.
If ContentsProtected = True Then
thesheet.Protect DrawingObjects:=ObjectsProtected, _
Contents:=True, Scenarios:=ScenariosProtected
End If
End Sub
SQLRetrieve
Protect
-or-
Unprotect
Additional query words: 5.00a 5.00c 7.00a 8.00 97 sheet protection book workbook XL
Keywords: kbdtacode kbpending kbprb kbprogramming KB149027