LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default Querytable Class Module

On Jun 19, 6:04 am, "Stevek" wrote:
Thank you Leith,

I did exactly as you suggested. My class now is QueryTableClass. The code in
the class is as you suggested. And in the workbook_open() event I added the
suggested code.
I would expect that each time the query table refreshes a Message Box would
popup with the text "Refreshed". But it does not.

"Leith Ross" wrote in message

ups.com...

On Jun 18, 6:07 pm, "Stevek" wrote:
Hello All,


This is my first time trying create a Class Module with events. I cannot
seem to get the AfterRefresh Event to fire.
Any help would be appreciated. TIA


Steve


Here's what my Class Module (ClsModQt) looks like:
================================================== =============================
Public WithEvents qtQueryTable As QueryTable


Sub InitQueryEvent(qt As Object)
Set qtQueryTable = qt
End Sub


Private Sub qtQueryTable_AfterRefresh(ByVal Success As Boolean)
MsgBox ("Refreshed")
End Sub
================================================== =============================


Here what my Sub looks like:
================================================== =============================
Private Sub CreateQueryTable(ByVal SQLString, ByVal SheetName)


Dim Destination As Range
Dim qt As QueryTable
Dim MyQt As New ClsModQT


Set Destination = Worksheets(SheetName).Range("A5")
Worksheets(SheetName).Range("A5:Z400").ClearConten ts


' -- Delete the existing names on the sheet <--
For Each nm In Worksheets(SheetName).Names
nm.Delete
Next


' -- Delete the existing query tables on the sheet <--
For Each qt In Worksheets(SheetName).QueryTables
qt.Delete
Next


MyQt.InitQueryEvent _
qt:=Worksheets(SheetName).QueryTables(1)


ConnString = "ODBC;DSN=MyDSN;UID=MyID;PWD=MyPWD;Database=My Db;"


With Worksheets(SheetName).QueryTables.Add(Connection:= ConnString,
Destination:=Destination, Sql:=SQLString)
.Name = SheetName
.RefreshStyle = xlInsertDeleterows
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = False
.Refresh
End With


End Sub


================================================== =============================


Hello Stevek,


Doing this is confusing. FIrst you need to name your Class module
"QueryTableClass". In the VBE display the Class module code. Bring up
the Properties window (type F4 if it isn't visible). Type
"QueryTableClass" in to the right of the "Name" property. Your Class
module code should look like this...
================================================== ====================
Public WithEvents qt As QueryTable


Private Sub qt_AfterRefresh(ByVal Success As Boolean)


MsgBox "Refreshed"


End Sub
================================================== ====================


Next you need to create a separate Standard module to initialize your
New Class. Place this code in your Workbook_Open() event. This code
needs to run before the start of the Query. Here is the code...
================================================== ====================
Dim QT1 As New QueryTableClass 'This is in the Declarations section
of the module


Sub Initialize_QT1()


' Note: If you have more than one Query Table on your Worksheet
change the index
' for QueryTables(n) to match.


Set QT1.qt = ThisWorkbook.Worksheets("Sheet1").QueryTables(1)


End Sub
================================================== ====================


Sincerely,
Leith Ross


Hello Stevek,

If you can email your workbook, I will look over the code for you. You
can email me at .

Sincerely,
Leith Ross

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CLASS MODULE & SIMPLE MODULE FARAZ QURESHI Excel Discussion (Misc queries) 1 September 7th 07 09:32 AM
Class Module Bill Martin[_2_] Excel Programming 6 January 25th 06 08:56 PM
Class Module Todd Huttenstine Excel Programming 2 May 21st 04 11:17 PM
Variable from a sheet module in a class module in XL XP hglamy[_2_] Excel Programming 2 October 14th 03 05:48 PM


All times are GMT +1. The time now is 11:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"