views:

61

answers:

2

I have a program in Delphi 2010 that uses a JET (mdb) database via ADO. I would like to be able to extract the definitions of some of the queries in the database and display them to the user. Is this possible either via SQL, some ADO interface, or by interrogating the database itself (I don't seem to have rights to MSysObjects).

+1  A: 

Some of that information is available via ADOX calls. There is an overview of the api with some examples (unfortunately not in Delphi) on the MSDN website.

Basically what you will want to do is to is to import the ADOX type library, and then use the wrapper that is generated for you to access the underlying API. From there its as simple as navigating the hierarchy to get at the data you need.

You will need to access the specific View object, and from there get the command property.

skamradt
Thanks. I found some Delphi specific ADOX material here: http://delphi.about.com/od/database/l/aa072401a.htm. Will read it and see if it does what I need. Although I'm really hoping for something I can push through the regular ADOCommand object or interrogate over the ADOConnection object.
Larry Lustig
I believe the ADOX connection is an ADO connection.
skamradt
Note that views would be only SELECT queries, and not DML queries, so it would be an incomplete result for all potential saved QueryDefs in an Access database.
David-W-Fenton
A: 

Via DAO, it's pretty easy. You just extract the SQL property of each QueryDef. In DAO from within Access, that would be:

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

  Set db = DBEngine.OpenDatabase("[path/name of database]")
  For Each qdf In db
    Debug.Print qdf.SQL
  Next qdf
  Set qdf = Nothing
  db.Close
  Set db = Nothing

I don't know how to translate that, but I think it's the simplest method once you're comfortable with using DAO instead of ADOX.

I don't use ADO at all, but I'm guessing that it has a collection of views and the SQL property would work for SELECT queries. However, if you're interested in getting the SQL for all saved QueryDefs, you'd also need to look at the DML queries, so you'd have to look at the stored procedures. I would have to look up the syntax for that, but I'm pretty certain that's how you'd get to the information via ADO.

David-W-Fenton