Microsoft KB Archive/182735

= XL97: How to Use the Query Before and AfterRefresh Events =

Article ID: 182735

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q182735





SUMMARY
In Microsoft Excel, you can automatically run a macro before or after updating (refreshing) worksheet query data. You can do this with the BeforeRefresh and AfterRefresh Visual Basic for Applications events. This article contains an example of how to use the two events to run a macro.



MORE INFORMATION
Before you can use the sample macro, retrieve data from Microsoft Query to a Microsoft Excel worksheet. To create the sample data, follow these steps:

 On the Data menu, point to Get External Data, and then click Create New Query. In the Choose Data Source dialog box, click , clear the "Use the Query Wizard to create/edit queries" check box, and then click OK. In the Create New Data Source dialog box, type test in the first box. Click the drop-down for the second combo box, and then click Microsoft dBase Driver (*.dbf). Click Connect. In the ODBC dBase Setup dialog box, clear the Use Current Directory check box, and then click Select Directory.</li> In the Select Directory dialog box, locate the following folder:

Program Files\Microsoft Office\Office

The Customer.dbf, Employee.dbf, and Orders.dbf files should be listed in this folder. If they are not, run the Office Setup program and install them.</li> Click OK.</li> Click OK in the ODBC dBase Setup dialog box.</li> Click OK in the Create New Data Source dialog box.</li> Make sure the "test" data source is selected in the Choose Data Source dialog box, and then click OK.

Microsoft Query is started and the Add Tables dialog box is displayed.</li> Click Customer.dbf, and then click Add.</li> Click Close.</li> Double-click the * listed in the Customer table.

This step adds all the fields to the result set in the Data pane.</li> On the File menu, click Return Data to Microsoft Excel.</li></ol>

This creates the query table to use with the sample macro.

Programming Example
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

In this example you set up a Visual Basic for Applications macro that responds to the BeforeRefresh or AfterRefresh event in a worksheet. To set up the event handler, follow these steps:

<ol> Switch to the worksheet that contains the data that is returned from Microsoft Query.</li> Start the Visual Basic Editor (press ALT+F11).</li> If the Project Explorer window is not visible, click Project Explorer on the View menu.</li> On the Insert menu, click Class Module.</li>  Click in the code window for the Class Module and enter the following code: Public WithEvents qt As QueryTable

Private Sub qt_BeforeRefresh(Cancel As Boolean)

' Declare variables.

Dim a As Integer Dim My_Prompt As String

' Initialize prompt text for message box. My_Prompt = "Data will be refreshed."

' Get YES or NO result from the message box a = MsgBox("Do you want to refresh the data now?", vbYesNoCancel)

' Check to see wheather YES or NO was selected. If a = vbNo Then

' Change prompt text for message box. My_Prompt = "Data will not be refreshed."

' Cancels the Query Refresh. Cancel = True

End If

' Displays message box before refresh (or non-refresh) occurs. MsgBox My_Prompt

End Sub NOTE: To use the AfterRefresh event you can replace the macro name "qt_BeforeRefresh(Cancel As Boolean)" with qt_AfterRefresh(ByVal Success As Boolean) in the above example. Also, you should change the message box text to an appropriate post Data Refresh message. </li> On the Insert menu, click Module.</li> <li> Click in the Code window for the Module and enter the following code: Dim X As New Class1

Sub Initialize_It Set X.qt = Thisworkbook.Sheets(1).QueryTables(1) End Sub </li> <li>Switch to Microsoft Excel (ALT+F11).</li> <li>Click any cell in the worksheet query data.</li> <li>On the Tools menu, point to Macro, click Macros, and then run the Initialize_It macro.</li> <li>On the Data menu, click Refresh Data.</li></ol>

The BeforeRefresh event should now run before the actual query data is refreshed. If you used the AfterRefresh event, the event runs after the actual query data has been refreshed.

NOTE: For the BeforeRefresh and AfterRefresh events to work, the Initialize_It macro must be run anytime the workbook is opened. You may want to use the name Auto_Open rather than Initialize_It. This will allow the two events to automatically function as expected when you refresh the query table data.

<div class="references_section">