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

Jim DeMarco

"Pro Excel 2007 VBA"


12. Place the insertion point at the beginning of the line containing the Catch statement,
and press Enter.
13. Move the insertion point up into the blank line you just inserted.
14. Add the following code to trap for the FileNotFoundException:
Catch ex As System.IO.FileNotFoundException
MsgBox("File: " & sDB & " not found")
That??™s all the code for the GetData method. The completed subroutine looks like
Listing 9-6.
CHAPTER 9 n ACTIVEX AND .NET 338
Listing 9-6. Complete GetData Subroutine
Private Sub GetData()
Dim sDB As String = "C:\ExampleDBs\Northwind 2007.accdb"
Dim iCols As Integer
Dim i As Integer
Dim row As Integer
Try
With m_oNWind
.NwindPathFileName = sDB
m_oDS = .GetData("select * from employees")
End With
iCols = m_oDS.Tables("Table1").Columns.Count
For i = 0 To iCols - 1
m_oSheet.Cells(1, i + 1).Value = _
m_oDS.Tables("Table1").Columns(i).Caption
Next
row = 2
For Each RowIterator As DataRow In m_oDS.Tables("Table1").Rows
For i = 0 To iCols - 1
m_oSheet.Cells(row, i + 1).Value = _
RowIterator(m_oDS.Tables("Table1").Columns(i).Caption)
Next
row = row + 1
Next
Dim r As Excel.Range
m_oSheet.Select()
r = m_oSheet.Range("A1")
r.Select()
Application.Selection.CurrentRegion.Select()
Application.Selection.Columns.AutoFit()
r.Select()
Catch ex As System.IO.FileNotFoundException
MsgBox("File: " & sDB & " not found")
Catch ex As Exception
End Try
End Sub
15. Save the project, and then run it by selecting Debug ?¤ Start Debugging, or by pressing
the F5 key.


Pages:
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311