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?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

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,136,328
Messages
5,675,137
Members
419,551
Latest member
thangxpm

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
Top