Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / December 2006

Tip: Looking for answers? Try searching our database.

Problem getting data to refresh

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ksminor@yahoo.com - 28 Dec 2006 16:11 GMT
Hi All.
I am using access to open, refresh, and close an excel spreadsheet. The
code below works fine as far as opening and closing the spreadsheet,
but it doesn't refresh the data. Any idea why?
Thanks in advance for any help.

Private Sub Command2_Click()
Dim MyXL As Object, xlFileAttribute As String
Dim xlFile As String, xlpw As String

   DoCmd.SetWarnings True

   xlFile = "c:\ActualHires.xls"

  'Set file attribute
   xlFileAttribute = GetAttr(xlFile)    ' Returns 1.
   If xlFileAttribute = 1 Then
       SetAttr (xlFile), vbNormal
   End If

   'Defer error trapping.
   'If the application isn't running, an error occurs.
   On Error Resume Next
   Set MyXL = GetObject(, "Excel.Application")
   Set MyXL = CreateObject("Excel.Application")

   'Clear Err object in case error occurred.
   Err.Clear

   'Disarm all warnings
   MyXL.Application.DisplayAlerts = False
   MyXL.Application.AlertBeforeOverwriting = False
   MyXL.Application.Visible = True
   MyXL.Workbooks.Open FileName:=xlFile, Password:="ABCD"

   'Update Pivottables and Save the File

   MyXL.Application.ActiveWorkbook.Sheets("sheet1").Select
   MyXL.Application.ActiveSheet.Range("A16").Value = "opened"
   MyXL.Application.ActiveWorkbook.Save
   MyXL.Application.ActiveWorkbook.Close
'
'
'    'Save Excel file, close & destroy all Excel objects
'    'Arm all warnings before quiting Excel
   MyXL.Application.DisplayAlerts = True
   MyXL.Application.AlertBeforeOverwriting = True
   MyXL.Application.Quit
   Set MyXL = Nothing

End Sub
Martin Fishlock - 29 Dec 2006 04:29 GMT
Hi KS,

From what I see in your code you are updating source data that is linked to
a pivot table in the workbook.

You therefore update the table but you omitted to refresh the pivot table

This is done by

MyXL.Application.ActiveWorkbook.Sheets( _
   "sheet2").PivotTables("PivotTable1").PivotCache.Refresh

also it may be better to write the code as follows:

Private Sub Command2_Click()
   Dim MyXL As Object, xlFileAttribute As String
   Dim xlFile As String, xlpw As String

   DoCmd.SetWarnings True

   xlFile = "c:\ActualHires.xls"

  'Set file attribute
   xlFileAttribute = GetAttr(xlFile)    ' Returns 1.
   If xlFileAttribute = 1 Then
       SetAttr (xlFile), vbNormal
   End If

   'Defer error trapping.
   'If the application isn't running, an error occurs.
   On Error Resume Next
   Set MyXL = GetObject(, "Excel.Application")
   Set MyXL = CreateObject("Excel.Application")

   'Clear Err object in case error occurred.
   Err.Clear

   'Disarm all warnings
   With MyXL.Application
       .DisplayAlerts = False
       .AlertBeforeOverwriting = False
       .Visible = True
   End With
   
   MyXL.Workbooks.Open Filename:=xlFile, Password:="ABCD"

' Update Pivottables and Save the File
   
   With MyXL.Application.ActiveWorkbook
       .Worksheets("sheet1").Range("A16").Value = "opened"
'>>>>>>> check the names here >>>>>>>>>>>>>>
       .Worksheets("sheet2").PivotTables("PivotTable1").PivotCache.Refresh
       .Close SaveChanges:=True
   End With
'
'    'Save Excel file, close & destroy all Excel objects
'    'Arm all warnings before quiting Excel
   With MyXL.Application
       .DisplayAlerts = True
       .AlertBeforeOverwriting = True
       .Quit
   End With
   Set MyXL = Nothing

End Sub

Signature

Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.

> Hi All.
> I am using access to open, refresh, and close an excel spreadsheet. The
[quoted text clipped - 47 lines]
>
> End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.