DSN

M

Mike Grove

I have a workbook that has a database connection for
refreshing the data. It was initialy set up using a DSN.
Now we have moved the database and I changed the DSN to
point to the new Server but the sheet still goes to the
old server. Does excel keep the connection info
internally somewhere after the initial setup and not use
the DSN. I have about 500 workbooks this way and would
like to write a quick applet to go through all the files
and update the connection info.
 
J

Jake Marx

Hi Mike,

Mike said:
I have a workbook that has a database connection for
refreshing the data. It was initialy set up using a DSN.
Now we have moved the database and I changed the DSN to
point to the new Server but the sheet still goes to the
old server. Does excel keep the connection info
internally somewhere after the initial setup and not use
the DSN. I have about 500 workbooks this way and would
like to write a quick applet to go through all the files
and update the connection info.

I'm not sure why this is happening. AFAIK, the old connection info is not
cached, but I don't know for sure.

Here's some (untested) code that may help you convert the connection string
for all query tables that match a given connection string in a folder of
Excel files. To use it, you must set a connection to Microsoft Scripting
Runtime in Tools | Options in the VBE.

Private Sub mConvertCxStr(rsWBPath As String, _
rsOldCxStr As String, rsNewCxStr As String)
Dim ws As Worksheet
Dim qt As QueryTable
Dim wbTarget As Workbook

On Error GoTo ErrHandler

Set wbTarget = Workbooks.Open(rsWBPath)

For Each ws In wbTarget.Worksheets
For Each qt In ws.QueryTables
If StrComp(qt.Connection, rsOldCxStr, _
vbTextCompare) = 0 Then
qt.Connection = rsNewCxStr
End If
Next qt
Next ws

ExitRoutine:
If Not wbTarget Is Nothing Then
wbTarget.Close False
Set wbTarget = Nothing
End If
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, _
vbExclamation, "ERROR"
Resume ExitRoutine
End Sub

Public Sub BatchConvertCxStr(rsFolder As String)
Dim fso As Scripting.FileSystemObject
Dim fil As Scripting.File

Set fso = New Scripting.FileSystemObject

If fso.FolderExists(rsFolder) Then
For Each fil In fso.GetFolder(rsFolder).Files
If fil.Type = "Microsoft Excel Worksheet" Then
mConvertCxStr fil.Path, "OldCXString", _
"NewCXString"
End If
Next fil
End If
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
J

Jake Marx

Mike,

After looking at this again, I missed something important. Namely, you need
to save the workbook after you change the connection string. A simple .Save
will work, but you may want to save it only if you changed the connection
string, in which case you can use a Boolean variable to track whether you've
made any changes or not.

Jake said:
For Each ws In wbTarget.Worksheets
For Each qt In ws.QueryTables
If StrComp(qt.Connection, rsOldCxStr, _
vbTextCompare) = 0 Then
qt.Connection = rsNewCxStr
End If
Next qt
Next ws

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
M

Mike Grove

Thanks, I'll give er a go
-----Original Message-----
Mike,

After looking at this again, I missed something important. Namely, you need
to save the workbook after you change the connection string. A simple .Save
will work, but you may want to save it only if you changed the connection
string, in which case you can use a Boolean variable to track whether you've
made any changes or not.

Jake said:
For Each ws In wbTarget.Worksheets
For Each qt In ws.QueryTables
If StrComp(qt.Connection, rsOldCxStr, _
vbTextCompare) = 0 Then
qt.Connection = rsNewCxStr
End If
Next qt
Next ws

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top