473,738 Members | 11,192 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Challenge: SQL STMT for Retrieving Content(SQL) of Saved Queries

I understand it's easy to list all saved queries of a given Access
database via Msysobjects system table. However, I have not seen any
posting over this NG or other similar ones that also include SQL
statement(conte nt) of these queries, though I've noticed some VB code
for that. Is that because it's simply impossible to get a query
content (not query resultset) from a SQL stmt?

Thanks in advance.

Nov 13 '05 #1
35 2548
huh? You want the SQL query typed out and the resulting recordset it
describes?
Where to? You can loop through the querydefs collection and print the
querydef's SQL

dim qdf as dao.querydef 'redundant, cuz ado doesn't have a querydef
object
for each qdf in dbengine(0)(0). querydefs
debug.print qdf.name
debug.print qdf.SQL
debug.print 'blank line
next qdf

Nov 13 '05 #2
Very good and I appreciate it. Now, two things here.
(A) The given Access database has tons of queries, over 200, so, the
debugging runs out of its default capacity, it hasn't finished even B
yet;
(B) So, is it feasible to interface with File System, that is, write
each query to a single file (query name as file name, query content as
file content and so on and so forth)?

Good job, again thanks.

Nov 13 '05 #3
Take a look at what Arvin Meyer has at
http://www.datastrat.com/Code/DocDatabase.txt

That will dump not only the queries but the forms, reports, modules and
macros, each into its own file.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"NickName" <da****@rock.co m> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
Very good and I appreciate it. Now, two things here.
(A) The given Access database has tons of queries, over 200, so, the
debugging runs out of its default capacity, it hasn't finished even B
yet;
(B) So, is it feasible to interface with File System, that is, write
each query to a single file (query name as file name, query content as
file content and so on and so forth)?

Good job, again thanks.

Nov 13 '05 #4
Douglas,

Thank you very much for the pointer. It seems great for the idea of
what I intend to do with this db, and we're getting close. With a
little tweak I can get what I need, however,
(A) the undocumented command/function of Application.Sav eAsText can't
seem to handle other object other than dbs.QueryDefs(i ).Name,
as the following
Application.Sav eAsText acQuery, dbs.QueryDefs(i ).{otherThanNam eObject--
you get the idea}, "C:\aFolder\its Sub\" & dbs.QueryDefs(i ).Name &
".txt"
what alternative may I apply?
(B) What if I want to save all the queries into a big AllQueries file?

Again I truly appreciate it.

Nov 13 '05 #5
SaveAsText needs the name of the query (or whatever) as its 2nd parameter.
What would you want to put there instead? (Have you looked at what gets
saved?)

No, I don't believe it would be possible using SaveAsText to put everything
into a single file. You could write each one to a file, append that file to
the "master file", then delete the first file you created. Messy, and
probably not much better than looping writing the text out yourself.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"NickName" <da****@rock.co m> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Douglas,

Thank you very much for the pointer. It seems great for the idea of
what I intend to do with this db, and we're getting close. With a
little tweak I can get what I need, however,
(A) the undocumented command/function of Application.Sav eAsText can't
seem to handle other object other than dbs.QueryDefs(i ).Name,
as the following
Application.Sav eAsText acQuery, dbs.QueryDefs(i ).{otherThanNam eObject--
you get the idea}, "C:\aFolder\its Sub\" & dbs.QueryDefs(i ).Name &
".txt"
what alternative may I apply?
(B) What if I want to save all the queries into a big AllQueries file?

Again I truly appreciate it.

Nov 13 '05 #6
OK, what I meant by
//
Application.Sav eAsText acQuery,
dbs.QueryDefs(i ).{otherThanNam *eObject--
you get the idea}, "C:\aFolder\its Sub\" & dbs.QueryDefs(i ).Name &
".txt"
//
is
Application.Sav eAsText acQuery, dbs.QueryDefs(i ).SQL,
"C:\aFolder\its Sub\" & dbs.QueryDefs(i ).Name &
".txt"
so as to get a query's content (sql statement), however,
Application.Sav eAsText method won't do the job.
Is there another way to output dbs.QueryDefs(i ).SQL to file?

Thanks.

Nov 13 '05 #7
How about something like this:
Public Sub WriteSQLsToFile (ByVal strFile As String)
On Error Resume Next

Dim qdf As DAO.QueryDef
Dim intFileNumber As Integer

intFileNumber = FreeFile ' Get unused file number.
Kill strFile
Open strFile For Output As #intFileNumber ' Create file name.

For Each qdf In DBEngine(0)(0). QueryDefs
If Left$(qdf.Name, 1) <> "~" Then
Print #intFileNumber, qdf.Name
Print #intFileNumber, "-----------"
Print #intFileNumber, qdf.SQL
Print #intFileNumber, ""
Print #intFileNumber, ""
End If
Next qdf

Close #intFileNumber ' Close file.

MsgBox "All queries written to " & vbCrLf & strFile

End Sub
Oh, this is wrong then... You'd have to open your new file INSIDE the
loop, and close it there too. pass the queryname or something and
output. Maybe do something fun like a routine to strip out illegal
characters or whatever.... but I have to leave SOME of the fun for
you... I mean, otherwise, what do you learn from all of my work?
Nothing.

Nov 13 '05 #8
"NickName" <da****@rock.co m> wrote in
news:11******** **************@ g47g2000cwa.goo glegroups.com:
OK, what I meant by
//
Application.Sav eAsText acQuery,
dbs.QueryDefs(i ).{otherThanNam *eObject--
you get the idea}, "C:\aFolder\its Sub\" & dbs.QueryDefs(i ).Name &
".txt"
//
is
Application.Sav eAsText acQuery, dbs.QueryDefs(i ).SQL,
"C:\aFolder\its Sub\" & dbs.QueryDefs(i ).Name &
".txt"
so as to get a query's content (sql statement), however,
Application.Sav eAsText method won't do the job.
Is there another way to output dbs.QueryDefs(i ).SQL to file?


I think you have a fundamental misunderstand of what SaveAsText
does. It's a command that saves all the properties of Access objects
to text files. If you do:

application.sav eastext acquery, [queryname], [filename]

You'll get a lot of information, but it's not just the SQL.

If all you want is the SQL, then you've got to write your file with
I/O functions.

Actually, there's one other choice. Create a function that returns
the .SQL property of a querydef, and then use MSysObjects as your
source table. If your function is called ReturnSQL(), the query
would look like this:

SELECT MSysObjects.Nam e, ReturnSQL([Name]) AS SQL
FROM MSysObjects
WHERE (((MSysObjects. Type)=5) AND ((MSysObjects.N ame) Not Like
"~*")) ORDER BY MSysObjects.Nam e;

(if you want the temporary queries, then you can remove the filter
on LIKE "~*")

The function looks like this:

Public Function ReturnSQL(strQu eryName As String, _
Optional db As DAO.Database) As String
Dim strTemp As String
Dim bolInitializedD B As Boolean

If db Is Nothing Then
Set db = CurrentDb()
bolInitializedD B = True
End If
strTemp = db.QueryDefs(st rQueryName).SQL
ReturnSQL = strTemp
If bolInitializedD B Then Set db = Nothing
End Function

If called only from a query based on MSysObjects, you won't need any
error handling to deal with non-existent queries, but a
general-purpose function would check to see if the query exists, or
have an error handler to recover from that condition.

Once you have a query like this, you can export it as text, if you
like.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #9
Very nice, thanks. But I'm not Access programmer, so I did not know
what Application.Sav eAsText would do exactly.

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2293
by: jb | last post by:
Using ADO.NET from ASP.NET. Have a SqlConnection executing stuff (stored procedures) I would like to be able to retrieve informational messages from the SQL sps, which comment on what they are doing (updating tables etc.), and show them to the user/write them to a file. Is there any way I can get at anything that comes from a SQL PRINT statement in this context? osql utility lets this go to standard output, which can be captured; that's...
1
294
by: Vik | last post by:
What is a method to create a datatable containing the properties of an SQL Server table? Thank you.
0
1442
by: Priya | last post by:
hi all, I need to read the xml schema file and retrieve the sql:datatype element value in the xsd file. The schema file format is below <xsd:sequence> <xsd:element name="safe_job" type = "xsd:string" sql:datatype="varchar(255)" /> </xsd:sequence>
5
1438
by: samadams_2006 | last post by:
Hello, How do I retrieve SQL Data into an XML Document? I have the following code, which will retrieve SQL data and write it to the screen via the Response Object, but I'd like to be able to read it as XML Data, and use XSL and XSLT to display the data. Any suggestions? Code Snippets MORE than appreciated... :) =================================================================
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.