SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 294 | Next

Jim DeMarco

"Pro Excel 2007 VBA"

The sNwindName variable will hold the path to the database.
Next, let??™s add a public property to the data access class to store and retrieve the location of
the database.
8. Add the following property to the NWindData class module:
Public Property NwindPathFileName() As String
Get
Return m_sNwindName
End Get
Set(ByVal value As String)
If System.IO.File.Exists(value) Then
m_sNwindName = value
Else
Throw New System.IO.FileNotFoundException
End If
End Set
End Property
The Property Get is very straightforward in that it??™s just returning the value from the private
variable. The Property Set has a bit of validation code. We??™re checking to see if the file
exists before we assign the new value to the private variable. If it does not exist, the class will
throw a System.IO.FileNotFoundException error to the client code.
nNote Managed code does not raise errors as classic VBA and VB did. In the .NET world, exceptions are
thrown by our code and caught in exception handling blocks.
Now we??™ll add a method that accepts a SQL statement to get the data and returns the data
in the form of a .NET DataSet object.
9. Add a new function named GetData to the NWindData class, as follows:
Public Function GetData(ByVal Which As String) As DataSet
CHAPTER 9 n ACTIVEX AND .NET 333
10. Add the following variable declarations:
Dim dsReturn As New DataSet()
Dim cnn As OleDbConnection
Dim sConnString As String
11. Create the connection to the data by adding the following code:
sConnString = "Provider=Microsoft.


Pages:
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306