Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Link, drop link, relink

Hi,
I have a connection to an Access 2010 query that is linked to an Excel 2010 table. I would like to use VBA to establish the link to the Access query, refresh the Excel table, then drop the link. This way Excel users have current data (updated monthly) and I don't have to be concerned that they might be connected to the database while the monthly updates to Access are in progress.

I could import but the Access database is in a remote location so it would be slower. Thanks for any ideas.

Doug
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Link, drop link, relink

Doug,

The first thing you might try is to change the Mode that Excel uses to open the connection. I think that it defaults to "Mode=Share Deny Write", which will prevent users from writing to the Access file while Excel is open. If you go to the Connection properties and change this setting to "Mode=Share Deny None" it should allow changes while Excel is open.

If you would still prefer to drop the link, then your code will need to recreate the connection each time you run it, since unlinking will remove the query settings from the table. The code below is what I use to do something similar. Note that you may have to record a macro where you add the connection in order to find the appropriate settings for items such as the connection string or command text.

Hope this helps,

Ben

Sub GetAccessData()
Application.ScreenUpdating = False
Dim sDBName As String 'Database file location
Dim wsDest As Worksheet 'Destination Worksheet
Dim rDest As Range 'Destination Range
Dim sCommand As String 'Table/Query Name
Dim sDisplay As String 'Display name for table

'Assign variables
sDBName = "C:\Desktop\My Database.accdb"
Set wsDest = ActiveSheet
Set rDest = wsDest.Range("$A$6")
sCommand = "My Table"
sDisplay = "My_Table"

'Get data from Access table
Application.DisplayAlerts = False
On Error Resume Next
wsDest.ListObjects(sDisplay).Delete 'Clears existing table
On Error GoTo 0
With wsDest.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password= """";User ID=Admin;Data Source=" & _
sDBName & ";Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";" & _
"Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;" & _
"Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;" & _
"Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;" & _
"Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;" & _
"Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False", Destination:=rDest).QueryTable
.CommandType = xlCmdTable
.CommandText = Array(sCommand)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = sDBName
.ListObject.DisplayName = sDisplay
.Refresh BackgroundQuery:=False
End With

'Remove link to Access and apply an autofilter to the range
Set rDest = wsDest.Range(sDisplay & "[#All]")
wsDest.ListObjects(sDisplay).Unlink
rDest.AutoFilter
Set rDest = Nothing
Set wsDest = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Link, drop link, relink

On Wednesday, December 19, 2012 11:41:21 PM UTC-7, wrote:
Hi,

I have a connection to an Access 2010 query that is linked to an Excel 2010 table. I would like to use VBA to establish the link to the Access query, refresh the Excel table, then drop the link. This way Excel users have current data (updated monthly) and I don't have to be concerned that they might be connected to the database while the monthly updates to Access are in progress.



I could import but the Access database is in a remote location so it would be slower. Thanks for any ideas.



Doug


Thanks, Ben. Just what I was looking for!
Reply
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
How to Link drop-down list? deepak Excel Discussion (Misc queries) 2 January 28th 09 10:07 AM
To Link drop-down list deepak Excel Discussion (Misc queries) 1 January 27th 09 12:50 PM
how do I link a drop down list entry to a new drop down cell? lmunzen Excel Discussion (Misc queries) 1 August 15th 06 04:59 PM
How do you link 2 drop-down menus? jd1655 Excel Programming 1 November 3rd 05 08:44 PM
Link To Drop Down Menu JANA Excel Worksheet Functions 5 October 26th 05 01:34 PM


All times are GMT +1. The time now is 01:01 PM.

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"