Cover | Table of Contents | Colophon
Private Sub CommandButton1_Click()
Dim xlws as Worksheet
Set xlws = ActiveSheet
xlws.PrintOut
Set xlws = nothing
End Sub
Excel.Worksheet and declare another variable as Excel.Application, but while in Excel this step is not needed). Next, you set this variable equal to the active worksheet—ActiveSheet
represents the current worksheet in the active workbook. Once there is a reference to the active worksheet, you can call any of the methods that are part of the object. In this example, you call the PrintOut method of the worksheet. There are several objects in Excel that have a PrintOut method; in each case, it simply prints the object. The final step sets the xlws variable to nothing, which tells Excel to no longer store a reference to the object. The xlws variable in this procedure still exists, even though you are no longer using it—if you were in a procedure that used several worksheets, you could set xlws to nothing and reuse that variable with any other worksheet.QueryDef that performs specific tasks in the book. The
Worksheet_Change event. To set up Excel to automatically refresh your query each time you change the criteria, from your worksheet, press Alt-F11 to open the Visual Basic Programming Environment. You will see several sections; focus on the Project Explorer (Figure 2-15). If you do not see it, press Ctrl-R. Next, double-click on your current worksheet in the Microsoft Excel Objects list. There should be no code in the code window on the right side of the screen. Going from left to right on the top of the code window, go to the first drop-down box and select Worksheet, and on the second drop-down box, select Change. Excel fills in the opening and closing lines of the procedure; although the opening and closing lines are shown in the following code excerpt, they should not be typed in. Refer to the code below and type it into your procedure (Figure 2-16).
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wks As Worksheet
Set wks = ActiveSheet
If Target.Row = 1 And Target.Column = 2 Then
wks.QueryTables(1).Refresh
End If
Set wks = Nothing
End Sub
ByRef). When you pass parameters to a procedure, you can either pass the value of the parameter or pass a reference to the variable. When you pass the value, the procedure gets the value and cannot change the original value for the calling procedure (provided that it is not a global variable). When you pass the variable ByRef, any action done on that variable in the called procedure is done on that variable in the calling procedure.x, and you want to pass it to a procedure to perform some math function on it. Then you want to use the result, and you do not need to know the original value later. You could write a function called DoMath and pass it your variable. You would create a variable in your original procedure to hold the value of the function that is returned. The other method is to create a procedure called DoMath but pass the variable x as ByRef. Then when you change the value of x in that procedure, the value of x is also updated in the original procedure. While that is a simplistic example, I hope it explains how this can be useful. In the project in the final chapter, you will see an example of how both of these concepts work in a sample application.QueryDef object. Once you have a DAO database connection, you can simply cycle through the QueryDefs collection with a For Each...Next loop and get information about each query; you can then make changes to the queries, if necessary. The QueryDefs collection contains every query in the Access database. When using DAO, you do not need to know what type of query you are dealing with to take actions on it.Procedures and Views collections. The other major difference between ADOX and DAO is that in ADOX, the Views are also part of the Tables collection, while in DAO they are not. The collection of tables in DAO is called the TableDefs collection. A single table is a TableDef.Views collection, you have access to the Command and CommandText. This is what gives you the SQL of the View. When you access a View as a Table, you do not have access to the Command or CommandText.CopyFromRecordset method places the results of a recordset for Excel Range objects in the range in an Excel worksheet. Sometimes, however, you bring in the results of a query but want only a few of the fields. You can accomplish this by looping through the records.MOVEFIRST method of a recordset to go to the first record, and then the MOVENEXT method to move through the recordset. There are several strategies to figure out when you have cycled through all of the records. When you move past the last record, the recordset's EOF flag is set to TRUE. I suggest using a While...Wend loop. Assuming your recordset is a variable named rs, you write the While...Wend loop like this:
rs.movefirst
While Not rs.eof
<Code to work with your Recordset>
rs.movenext
Wend
CopyFromRecordSet, let's take a look at an example of where you would want to loop through a recordset. Consider a table of employee information that has 15 fields. Assuming that you need only three of the fields (Last Name, First Name, and Salary), you have two choices for how to accomplish this.Columns that is available on a Range object and a Worksheet object. If your range is already defined in a variable called xlrng, and you want to perform formatting on the third column, refer to this column as xlrng.columns(3). If you do this, I suggest having an additional range object that you can refer to. If you have a range object called xlrng2, you can write a line that says set xlrng2 = xlrng.columns(3). I like to do this so that the range properties and methods will be available as I write the code.www.oreilly.com. In these sample files, you will find the Excel example above and the other necessary databases and Excel workbooks, although I encourage you to try to enter the code for yourself, as it will help you get a better feel for VBA.