PC Review


Reply
Thread Tools Rate Thread

Accessing SQL code in a query

 
 
EricY
Guest
Posts: n/a
 
      30th Jul 2008
Hello,

I need to be able to access the SQL code in 500 queries.

Using the code below I am able to access each query object, but I have been
unable to find the property that will enable me to extract the SQL. It's just
a test program that loads a listbox with the info:

Dim accObject As Access.AccessObject

For Each accObject In CodeData.AllQueries
Me.listObjects.AddItem "Query:;" & accObject.Name
Next

Any help greatly appreciated!

Eric.
 
Reply With Quote
 
 
 
 
Brendan Reynolds
Guest
Posts: n/a
 
      30th Jul 2008
"EricY" <(E-Mail Removed)> wrote in message
news:E178C2E0-F7DD-4A58-B998-(E-Mail Removed)...
> Hello,
>
> I need to be able to access the SQL code in 500 queries.
>
> Using the code below I am able to access each query object, but I have
> been
> unable to find the property that will enable me to extract the SQL. It's
> just
> a test program that loads a listbox with the info:
>
> Dim accObject As Access.AccessObject
>
> For Each accObject In CodeData.AllQueries
> Me.listObjects.AddItem "Query:;" & accObject.Name
> Next
>
> Any help greatly appreciated!
>
> Eric.



The AccessObject object doesn't have the property you need, you need either
a DAO.QueryDef object or an ADODB.Command object.

Heres a DAO example ...

Public Sub ListSqlDao()

Dim db As DAO.Database
Dim qdfs As DAO.QueryDefs
Dim qdf As DAO.QueryDef

Set db = CurrentDb
Set qdfs = db.QueryDefs
For Each qdf In qdfs
Debug.Print qdf.SQL
Next qdf

End Sub

I'm not very familiar with ADO, so the following example may not be the best
way to do it in ADO, but it seems to work. Requires references to the ADO
and ADOX object libraries ...

Public Sub ListSqlAdo()

Dim cat As ADOX.Catalog
Dim vws As ADOX.Views
Dim vw As ADOX.View
Dim cmd As ADODB.Command

Set cat = New ADOX.Catalog
Set cat.ActiveConnection = CurrentProject.Connection
Set vws = cat.Views
Debug.Print cat.Views.Count
For Each vw In vws
Set cmd = vw.Command
Debug.Print cmd.CommandText
Next vw

End Sub

--
Brendan Reynolds

 
Reply With Quote
 
Stefan Hoffmann
Guest
Posts: n/a
 
      30th Jul 2008
hi Eric,

EricY wrote:
> I need to be able to access the SQL code in 500 queries.

Are you having a .mdb?

Dim qdf As DAO.QueryDef

For Each qdf In CurrentDb.QueryDefs
Debug.Print qdf.SQL
Next qdf


mfG
--> stefan <--
 
Reply With Quote
 
Stuart McCall
Guest
Posts: n/a
 
      30th Jul 2008
"EricY" <(E-Mail Removed)> wrote in message
news:E178C2E0-F7DD-4A58-B998-(E-Mail Removed)...
> Hello,
>
> I need to be able to access the SQL code in 500 queries.
>
> Using the code below I am able to access each query object, but I have
> been
> unable to find the property that will enable me to extract the SQL. It's
> just
> a test program that loads a listbox with the info:
>
> Dim accObject As Access.AccessObject
>
> For Each accObject In CodeData.AllQueries
> Me.listObjects.AddItem "Query:;" & accObject.Name
> Next
>
> Any help greatly appreciated!
>
> Eric.


Dim s As String

For Each accObject In CodeData.AllQueries
s = accObject.SQL
'Do whatever you want to the string here.
accObject.SQL = s
Next


 
Reply With Quote
 
EricY
Guest
Posts: n/a
 
      30th Jul 2008
Thanks Brendan. I used the DAO method and it worked a treat.

Cheers!

Eric

"Brendan Reynolds" wrote:

> "EricY" <(E-Mail Removed)> wrote in message
> news:E178C2E0-F7DD-4A58-B998-(E-Mail Removed)...
> > Hello,
> >
> > I need to be able to access the SQL code in 500 queries.
> >
> > Using the code below I am able to access each query object, but I have
> > been
> > unable to find the property that will enable me to extract the SQL. It's
> > just
> > a test program that loads a listbox with the info:
> >
> > Dim accObject As Access.AccessObject
> >
> > For Each accObject In CodeData.AllQueries
> > Me.listObjects.AddItem "Query:;" & accObject.Name
> > Next
> >
> > Any help greatly appreciated!
> >
> > Eric.

>
>
> The AccessObject object doesn't have the property you need, you need either
> a DAO.QueryDef object or an ADODB.Command object.
>
> Heres a DAO example ...
>
> Public Sub ListSqlDao()
>
> Dim db As DAO.Database
> Dim qdfs As DAO.QueryDefs
> Dim qdf As DAO.QueryDef
>
> Set db = CurrentDb
> Set qdfs = db.QueryDefs
> For Each qdf In qdfs
> Debug.Print qdf.SQL
> Next qdf
>
> End Sub
>
> I'm not very familiar with ADO, so the following example may not be the best
> way to do it in ADO, but it seems to work. Requires references to the ADO
> and ADOX object libraries ...
>
> Public Sub ListSqlAdo()
>
> Dim cat As ADOX.Catalog
> Dim vws As ADOX.Views
> Dim vw As ADOX.View
> Dim cmd As ADODB.Command
>
> Set cat = New ADOX.Catalog
> Set cat.ActiveConnection = CurrentProject.Connection
> Set vws = cat.Views
> Debug.Print cat.Views.Count
> For Each vw In vws
> Set cmd = vw.Command
> Debug.Print cmd.CommandText
> Next vw
>
> End Sub
>
> --
> Brendan Reynolds
>

 
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
Accessing query fields from code with in a report Dennis Microsoft Access Reports 5 7th Mar 2009 03:28 AM
Accessing source code Martin Robins Microsoft Dot NET Compact Framework 4 6th Oct 2006 10:41 AM
What is the best case for accessing managed code from unmanaged code? Bae,Hyun-jik Microsoft VC .NET 4 28th Jul 2005 02:37 PM
accessing code behind ashish Microsoft ASP .NET 1 27th May 2004 05:35 PM
accessing code behind .. ashish Microsoft ASP .NET 2 26th May 2004 04:47 PM


Features
 

Advertising
 

Newsgroups
 


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