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 295 | Next

Jim DeMarco

"Pro Excel 2007 VBA"

ACE.OLEDB.12.0;" ??
& "Data Source=" & m_sNwindName & ";"
cnn = New OleDb.OleDbConnection(sConnString)
12. Create a DataAdapter to hold the data and fill the DataSet by adding the following code:
Dim da As New OleDbDataAdapter(Which, cnn)
Next, we are going to fill the DataSet from the DataAdapter.We will add exception handling
to this process. As VBA programmers, we??™re used to using the age-old On Error Go To
syntax in our error handlers. In our managed code, we use Try...Catch blocks to catch any
exceptions our code throws.
13. Add the following code to fill the DataSet and watch for and handle exceptions:
Try
da.Fill(dsReturn, TABLE_NAME)
Catch ex As Exception
MsgBox(ex.Message)
End Try
This Try...Catch block includes the optional Finally clause. Any code inserted here will
always run regardless of errors. It??™s a good place for cleanup code. The last thing to do is return
our filled DataSet.
14. Add the following code after the Try...Catch block:
Return dsReturn
The complete GetData function looks like Listing 9-5.
Listing 9-5. Complete GetData Function
Public Function GetData(ByVal Which As String) As DataSet
Dim dsReturn As New DataSet()
Dim cnn As OleDbConnection
Dim sConnString As String
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;" ??
& "Data Source=" & m_sNwindName & ";"
cnn = New OleDb.OleDbConnection(sConnString)
Dim da As New OleDbDataAdapter(Which, cnn)
CHAPTER 9 n ACTIVEX AND .NET 334
Try
da.Fill(dsReturn, TABLE_NAME)
Catch ex As Exception
MsgBox(ex.


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