PC Review


Reply
Thread Tools Rate Thread

Accessing the Data Connection Strings and Command Text (SQL) in VB

 
 
MChrist
Guest
Posts: n/a
 
      30th Apr 2010
I have a number of Excel files that have data connections to various sources,
and I want to be able to read the data connection strings and command text
(SQL) using VBA.

In Access this would be easy to do, creating a database object, and looking
at the QueryDefs. Is there something similar in Excel?

If someone could point me to an on-line reference or provide a code like the
one below, that would be great.

Thank you.

Mark

Private Sub Test()

Dim strMsg As String

Dim db As DAO.Database
Dim qds As DAO.QueryDefs
Dim qd As DAO.QueryDef

Set db = currentdb()
Set qds = db.QueryDefs

For Each qd In qds

Debug.Print qd.Connect & vbTab & qd.Sql

Next

End Sub


 
Reply With Quote
 
 
 
 
AB
Guest
Posts: n/a
 
      30th Apr 2010
In excel vba you can code pretty much the same way as in access vba.
If you want to have intellisense and use early bounding - add
refference to dao library (in vbe Tools>Refferences and then check the
most recent DAO lib taht you have or whichever want to use) and off
you go.

google for:
set DAO.Database object from excel
and will get loads of feeds.

On Apr 30, 2:45*pm, MChrist <MChr...@discussions.microsoft.com> wrote:
> I have a number of Excel files that have data connections to various sources,
> and I want to be able to read the data connection strings and command text
> (SQL) using VBA.
>
> In Access this would be easy to do, creating a database object, and looking
> at the QueryDefs. *Is there something similar in Excel?
>
> If someone could point me to an on-line reference or provide a code like the
> one below, that would be great.
>
> Thank you.
>
> Mark
>
> Private Sub Test()
>
> * Dim strMsg As String
>
> * Dim db As DAO.Database
> * Dim qds As DAO.QueryDefs
> * Dim qd As DAO.QueryDef
>
> * Set db = currentdb()
> * Set qds = db.QueryDefs
>
> * For Each qd In qds
>
> * * Debug.Print qd.Connect & vbTab & qd.Sql
>
> * Next
>
> End Sub


 
Reply With Quote
 
JLatham
Guest
Posts: n/a
 
      30th Apr 2010
See if this helps you out any

Sub ReadConnections()
Dim anySheet As Worksheet
Dim qTable As QueryTable

For Each anySheet In ThisWorkbook.Worksheets
If anySheet.QueryTables.Count > 0 Then
For Each qTable In anySheet.QueryTables
MsgBox qTable.Connection
Next
End If
Next
End Sub


"MChrist" wrote:

> I have a number of Excel files that have data connections to various sources,
> and I want to be able to read the data connection strings and command text
> (SQL) using VBA.
>
> In Access this would be easy to do, creating a database object, and looking
> at the QueryDefs. Is there something similar in Excel?
>
> If someone could point me to an on-line reference or provide a code like the
> one below, that would be great.
>
> Thank you.
>
> Mark
>
> Private Sub Test()
>
> Dim strMsg As String
>
> Dim db As DAO.Database
> Dim qds As DAO.QueryDefs
> Dim qd As DAO.QueryDef
>
> Set db = currentdb()
> Set qds = db.QueryDefs
>
> For Each qd In qds
>
> Debug.Print qd.Connect & vbTab & qd.Sql
>
> Next
>
> End Sub
>
>

 
Reply With Quote
 
AB
Guest
Posts: n/a
 
      30th Apr 2010
Perhaps JLatham understood your post correctly (as oppose to me).
I thought that you wanted to read sql strings in Access from Excel.
If you want to read SQL strings and Connections in excel sheets (i.e.,
querytables on excel tabs) then JLatham code is the way to go.
As per his code:
qTable.Connection - will give you the connection string
qTable.Commandtext - will give you the SQL string.

On Apr 30, 3:16*pm, JLatham <JLat...@discussions.microsoft.com> wrote:
> See if this helps you out any
>
> Sub ReadConnections()
> * Dim anySheet As Worksheet
> * Dim qTable As QueryTable
>
> * For Each anySheet In ThisWorkbook.Worksheets
> * * If anySheet.QueryTables.Count > 0 Then
> * * * For Each qTable In anySheet.QueryTables
> * * * * MsgBox qTable.Connection
> * * * Next
> * * End If
> * Next
> End Sub
>
>
>
> "MChrist" wrote:
> > I have a number of Excel files that have data connections to various sources,
> > and I want to be able to read the data connection strings and command text
> > (SQL) using VBA.

>
> > In Access this would be easy to do, creating a database object, and looking
> > at the QueryDefs. *Is there something similar in Excel?

>
> > If someone could point me to an on-line reference or provide a code like the
> > one below, that would be great.

>
> > Thank you.

>
> > Mark

>
> > Private Sub Test()

>
> > * Dim strMsg As String

>
> > * Dim db As DAO.Database
> > * Dim qds As DAO.QueryDefs
> > * Dim qd As DAO.QueryDef

>
> > * Set db = currentdb()
> > * Set qds = db.QueryDefs

>
> > * For Each qd In qds

>
> > * * Debug.Print qd.Connect & vbTab & qd.Sql

>
> > * Next

>
> > End Sub- Hide quoted text -

>
> - Show quoted text -


 
Reply With Quote
 
JLatham
Guest
Posts: n/a
 
      30th Apr 2010
Upon further thought, this might be of more use to you, gives more info:

Sub ReadConnections()
Dim anySheet As Worksheet
Dim qTable As QueryTable

For Each anySheet In ThisWorkbook.Worksheets
If anySheet.QueryTables.Count > 0 Then
For Each qTable In anySheet.QueryTables
With qTable
Debug.Print .Name
Debug.Print .Connection
Debug.Print .CommandText
Debug.Print ' separating line
End With
Next
End If
Next
End Sub


"MChrist" wrote:

> I have a number of Excel files that have data connections to various sources,
> and I want to be able to read the data connection strings and command text
> (SQL) using VBA.
>
> In Access this would be easy to do, creating a database object, and looking
> at the QueryDefs. Is there something similar in Excel?
>
> If someone could point me to an on-line reference or provide a code like the
> one below, that would be great.
>
> Thank you.
>
> Mark
>
> Private Sub Test()
>
> Dim strMsg As String
>
> Dim db As DAO.Database
> Dim qds As DAO.QueryDefs
> Dim qd As DAO.QueryDef
>
> Set db = currentdb()
> Set qds = db.QueryDefs
>
> For Each qd In qds
>
> Debug.Print qd.Connect & vbTab & qd.Sql
>
> Next
>
> End Sub
>
>

 
Reply With Quote
 
JLatham
Guest
Posts: n/a
 
      30th Apr 2010
And maybe you got it right! Until we hear back from the OP, probably won't
know. Often 2 or 3 people read the same request and each has a slightly
different interpretation of the needs/desires.
In either case, hopefully he gets the help he's looking for.

"AB" wrote:

> Perhaps JLatham understood your post correctly (as oppose to me).
> I thought that you wanted to read sql strings in Access from Excel.
> If you want to read SQL strings and Connections in excel sheets (i.e.,
> querytables on excel tabs) then JLatham code is the way to go.
> As per his code:
> qTable.Connection - will give you the connection string
> qTable.Commandtext - will give you the SQL string.
>
> On Apr 30, 3:16 pm, JLatham <JLat...@discussions.microsoft.com> wrote:
> > See if this helps you out any
> >
> > Sub ReadConnections()
> > Dim anySheet As Worksheet
> > Dim qTable As QueryTable
> >
> > For Each anySheet In ThisWorkbook.Worksheets
> > If anySheet.QueryTables.Count > 0 Then
> > For Each qTable In anySheet.QueryTables
> > MsgBox qTable.Connection
> > Next
> > End If
> > Next
> > End Sub
> >
> >
> >
> > "MChrist" wrote:
> > > I have a number of Excel files that have data connections to various sources,
> > > and I want to be able to read the data connection strings and command text
> > > (SQL) using VBA.

> >
> > > In Access this would be easy to do, creating a database object, and looking
> > > at the QueryDefs. Is there something similar in Excel?

> >
> > > If someone could point me to an on-line reference or provide a code like the
> > > one below, that would be great.

> >
> > > Thank you.

> >
> > > Mark

> >
> > > Private Sub Test()

> >
> > > Dim strMsg As String

> >
> > > Dim db As DAO.Database
> > > Dim qds As DAO.QueryDefs
> > > Dim qd As DAO.QueryDef

> >
> > > Set db = currentdb()
> > > Set qds = db.QueryDefs

> >
> > > For Each qd In qds

> >
> > > Debug.Print qd.Connect & vbTab & qd.Sql

> >
> > > Next

> >
> > > End Sub- Hide quoted text -

> >
> > - Show quoted text -

>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trim data and search for text strings NDBC Microsoft Excel Programming 5 7th Sep 2009 11:26 PM
Can't get Text connection strings to work Maury Markowitz Microsoft Access 0 25th Mar 2008 08:38 PM
Accessing the text of a form command button from underlying macro Chrisso Microsoft Excel Programming 6 15th Sep 2007 03:02 PM
Querying against two data sources/connection strings =?Utf-8?B?TWF1cnkgTWFya293aXR6?= Microsoft Access 1 7th May 2007 10:09 PM
Allow only numeric data in certain cells & only text strings in others mikeburg Microsoft Excel Programming 6 1st Oct 2005 11:40 PM


Features
 

Advertising
 

Newsgroups
 


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