Create a Refreshable Listobject via VBA

Schwimms

New Member
Joined
Jan 31, 2008
Messages
49
Hi All,

I am able to select data out of my SQL server database into a ListOjbect via VBA. My problem is I cannot make the ListOjbect Refresh. Meaning I would like to be able to select the list object in Excel and right click and choose refresh (currently grayed out) or refresh it via VBA (receive an error).

Here is my code to produce the listobject in VBA:

Code:
[B]Sub refresh()
Dim oCn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String
Dim qt As QueryTable
ConnString = "Provider=SQLOLEDB;Integrated Security=SSPI;" & _
   "Data Source=NCXXCHAR9S\PDS_SSQL02; Database=MWPMO;" & _
    "Persist Security Info=False;"
Set oCn = New ADODB.Connection
oCn.ConnectionString = ConnString
oCn.Open
SQL = "SELECT * from Rpt_RFD"
Set oRS = New ADODB.Recordset
oRS.Source = SQL
oRS.ActiveConnection = oCn
oRS.Open
Set qt = ActiveSheet.ListObjects.Add( _
    SourceType:=XlListObjectSourceType.xlSrcQuery, _
        Source:=oRS, _
            Destination:=ActiveSheet.Range("A3")).QueryTable
With qt
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = True
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .refresh BackgroundQuery:=True
End With
End Sub
[/B]
Please help if you can.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Thanks for the post. I tried that change with NO luck. My goal with this code is to pull data from sql into excel and keep the format (conditonal formats, row highlights, column/row sizes, data formats) of the table each time the data is pulled. If there are any other ways in doing please let me know.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top