Cancel Refresh of External Data when not connected to network

uwlchemist

New Member
Joined
Aug 29, 2009
Messages
38
I have a Workbook that gets data from an Access database. I have the workbook refresh this data on open. Everything works great when connected to my work network, or to the vpn. However, when I am not connected to my work network I get prompts for entering network info or to cancel the refresh. I would like to have vba cancel the refresh command if the Access database is unavailable (not connected to the work network) but have been unable to accomplish this as yet. Any ideas?
 

Hermanito

Well-known Member
Joined
Apr 4, 2007
Messages
1,238
I can't shake the code out of my sleeve right away, but I think you'd need something like this:

- do not use the refresh on open feature.
- have the Workbook_Open procedure check if the Access-file is visible (just checking if the file exists is enough I guess, googling will surely find you code to do this)
- if it is, refresh the querytable(s) and/or pivottable(s). Easiest way to get the code to do that is the macro recorder. Record a new macro and manually refresh all data you want refreshed, then stop the recording and use the code accordingly...

I hope that helps...
 

uwlchemist

New Member
Joined
Aug 29, 2009
Messages
38
Thank you Hermanito. This is what I ended up doing. I am keeping the refresh on open as I think that is the best way for me (feel free to explain why it isn't a good way if like). I took your suggestion about checking to see if the file is visible, and found this code.

Original website (http://vbadud.blogspot.com/2007/04/vba-function-to-check-file-existence.html)

Function File_Exists(ByVal sPathName As String, Optional Directory As Boolean) As Boolean
'Returns True if the passed sPathName exist
'Otherwise returns False
On Error Resume Next
If sPathName <> "" Then
If IsMissing(Directory) Or Directory = False Then
File_Exists = (Dir$(sPathName) <> "")
Else
File_Exists = (Dir$(sPathName, vbDirectory) <> "")
End If
End If
End Function

Then it is just a simple task of calling this function to check if the file exists before refreshing. Works like a charm. Thank you for your suggestion...I like it much better than the dialog box I was using before.
 

Hermanito

Well-known Member
Joined
Apr 4, 2007
Messages
1,238
My suggestion to not use the refresh on open was purely based on what you told in your first post. Personally I don't like Excel asking me 'this file has connections to external data, do you want... blahblahblah' whenever I open such a file.
If you keep the refresh on open switched off, you will not get that message, all while the Workbook_Open event from within VBA can still check if the Access-file is available, and if so, you can do the refresh from there. In that way you never get nagged by any dialogboxes, nor will there be errors when the file is unavailable, it will just refresh the data if and only if it is available.
But if it works to your satisfaction now, there is no reason or need to change it :)
 

uwlchemist

New Member
Joined
Aug 29, 2009
Messages
38
I see what you are saying now. I have had that box unchecked from the beginning...I do all of the refreshing through code. I agree it is annoying. Thanks for the clarification.
 

Forum statistics

Threads
1,082,126
Messages
5,363,317
Members
400,725
Latest member
excelingtolearn

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top