473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

how to programmatical read the current sql statement if cuurent query ins't saved?

Hi All,
I'm new to this group and quite new to access/vba.
So, shortly after beginning to write a simple application for my wife,
I came across a blocking problem: I need to intercept the sql statement
that stay behind a current, but not yet saved query.
When I work on saved queries I use:

strCurrentName = CurrentObjectName
Dim dbsCurrent As Database
Set dbsCurrent = CurrentDb
sqlCurrent = dbsCurrent.QueryDefs(strCurrentName).SQL

But I need the same info when I work on query already designed but not
yet saved.
Is there a simple way to achive this?

Thank you for your time and efforts...
Ciao
Stefano

Feb 7 '06 #1
6 2223

sghi wrote:
Hi All,
I'm new to this group and quite new to access/vba.
So, shortly after beginning to write a simple application for my wife,
I came across a blocking problem: I need to intercept the sql statement
that stay behind a current, but not yet saved query.
When I work on saved queries I use:

strCurrentName = CurrentObjectName
Dim dbsCurrent As Database
Set dbsCurrent = CurrentDb
sqlCurrent = dbsCurrent.QueryDefs(strCurrentName).SQL

But I need the same info when I work on query already designed but not
yet saved.
Is there a simple way to achive this?


As far as I know, if you're working with an Access back-end, there are
only two ways to "design" a query - i.e. a current, but not yet saved
query.

In the query designer window, in which case you can drop down the "SQL
view" from the "View" button.

In code, writing in-line SQL. In which case, you can intercept it with
a breakpoint and examine it in the Immediate pane.

HTH

Edward

Feb 7 '06 #2
thank you Teddysn...,
but my problem is that i'd like to read in a string in a vba module the
sql statement that you can read in the "..."SQL view" from the "View"
button."

Ciao
Stefano

Feb 7 '06 #3

"sghi" <st**************@getronics.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
thank you Teddysn...,
but my problem is that i'd like to read in a string in a vba module the
sql statement that you can read in the "..."SQL view" from the "View"
button."


Help me understand why it is too much trouble to save the query.

Objects being designed but not yet saved are not as accessible as you might
like, but apparently The Boys And Girls In Redmond did not intend them to
be - they have had quite a number of iterations of Access to make them so
and haven't done it.

Larry Linson
Microsoft Access MVP
Feb 7 '06 #4

Sorry, I can't help you solve your problem but I still wonder what the
underlying reason for wanting to do this might be?

Stefano wrote:
I need to intercept the sql statement that stay behind
a current, but not yet saved query.


*** Sent via Developersdex http://www.developersdex.com ***
Feb 7 '06 #5
I can't for the life of me understand why you would need to do this but have
a look at my SQLComment project. It demonstrates how to read the contents of
the SQL View window.
http://www.lebans.com/addsqlcomments.htm
NEW - May 16/2004 A2KAddSQLComments.zip is an MDB containing functions to:

1) To allow the saving of Comments in the SQL View window.
2) To allow the saving/restoration of Comments in the SQL View window.

Here is the A97 version:A97AddSQLComments.zip

History

Version 1.3 May 16, 2004

As per a suggestion from Dimitri Furman, modified parsing function to allow
for multiple instances of the Access SQL string delimiter character(";")

Version 1.1 May 16, 2004

First release!

Here is the source code:

Option Compare Database
Option Explicit

'DEVELOPED AND TESTED UNDER MICROSOFT ACCESS 97, 2K, and 2K2 VBA
'
'Copyright: Stephen Lebans - Lebans Holdings 1999 Inc.
' Please feel free to use this code within your own projects,
' both private and commercial, with no obligation.
' You may not resell this code by itself or as part of a collection.
'
'
'Name: Add/Save/Modify Comments for the SQL View window
'
'Version: 1.1
'
'Purpose: 1) To allow the saving of Comments in the SQL View window.
' 2) To allow the saving/restoration of Comments in the SQL View window.
'
'Requires: The table named "SQL-Comments" included with this sample MDB
'
'Author: Stephen Lebans
'
'Email: St*****@lebans.com
'
'Web Site: www.lebans.com
'
'Date: May 16, 2004, 11:11:11 AM
'
'Credits: Yours for the taking!<grin.
'
'BUGS: Please report any bugs to:
' St*****@lebans.com
'
'What's Missing:
' DAO error handling
' All other Error handling
' Add it yourself!
'
'How it Works:
' Walk through the source code!<grin>
'
' Enjoy
' Stephen Lebans

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" Alias _
"FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function apiGetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) _
As Long

Private Declare Function GetFocus Lib "user32" () As Long

Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long

Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function IsWindowVisible Lib "user32" _
(ByVal hWnd As Long) As Long


Public Function fShowComments()
On Error GoTo Err_ShowComments

' This function can only be called from a
' ToolBar/Menu when the SQL Design window
' is in SQL view.

Dim varLength As Variant
Dim lngRet As Long
Dim hWndMDI As Long
Dim hWndOQry As Long
Dim hWndOKttbx As Long
Dim hwndODsk As Long
Dim hWndOSUI As Long
Dim s As String
Dim sComment As String
Dim sCaption As String
sComment = "/Comments Start Here: Remember to HIDE the comments before
Saving this Query!!!!!" & vbCrLf
' Recordset stuff
Dim rst As DAO.Recordset
Dim sSQL As String
Dim sSel As String
Dim db As DAO.Database

hWndOKttbx = GetFocus
If fGetClassName(hWndOKttbx) <> "OKttbx" Then
fShowComments = False
Exit Function
End If

' Get Parent
hWndOQry = GetParent(hWndOKttbx)
' If not "OQry" then we are not in Query Design window
If hWndOQry = 0 Then Exit Function

If fGetClassName(hWndOQry) <> "OQry" Then
fShowComments = False
Exit Function
End If

' Get the Caption of the SQL Design window
sCaption = Space(512)
lngRet = GetWindowText(hWndOQry, sCaption, 256)
sCaption = Left(sCaption, lngRet)
If Len(sCaption & vbNullString) = 0 Then Exit Function

' Find the window of class ODsk
hwndODsk = FindWindowEx(hWndOQry, 0&, "ODsk", vbNullString)
' If does nto exist then we are not in the Query Design window
If hwndODsk = 0 Then Exit Function
' If this Window is Visible then we are not in the
' SQL View window!
lngRet = IsWindowVisible(hwndODsk)
If lngRet <> 0 Then Exit Function

' Get the Text of the SQL Design window
s = Space(4096)
lngRet = GetWindowText(hWndOKttbx, s, 2048)
s = Left(s, lngRet)
If Len(s & vbNullString) = 0 Then Exit Function
' See if there are already Comments in place.
' If so REMOVE THEM
varLength = InStr(1, s, "/Comment", vbTextCompare)
If varLength <> 0 Then
' Remove comments
' Save Comments to Disk first!!

' Grab a ref to the CurrentDB
Set db = CurrentDb()
' Setup our SQL string
sSQL = "SELECT [SQL-Comments].QueryName, [SQL-Comments].Comment FROM
[SQL-Comments] WHERE [SQL-Comments].QueryName = " & """" & sCaption & """"
Set rst = db.OpenRecordset(sSQL, dbOpenDynaset)

' Empty?
If rst.RecordCount = 0 Then
' No existing record. Create one.
rst.AddNew
Else
rst.Edit
End If
rst.Fields("QueryName") = sCaption
rst.Fields("Comment") = Mid(s, varLength)
rst.Update

' Free our RecordSet
Set rst = Nothing
db.Close
Set db = Nothing

' Remove our Comment from the SQL View window
' Remove everything after the SQL EOF marker ";"
s = Left(s, InStr(1, s, ";", vbTextCompare))
lngRet = SetWindowText(hWndOKttbx, s)
Exit Function
End If

' If we arrive here then no comments are displayed.
' Let's check our table and see if there is an existing
' entry for this saved Query. If so then display it!
' Grab a ref to the CurrentDB
Set db = CurrentDb()
' Setup our SQL string
sSQL = "SELECT [SQL-Comments].QueryName, [SQL-Comments].Comment FROM
[SQL-Comments] WHERE [SQL-Comments].QueryName = " & """" & sCaption & """"
Set rst = db.OpenRecordset(sSQL, dbOpenDynaset)

' Empty?
If rst.RecordCount = 0 Then
' Add our New Comment Header
s = s & vbCrLf & vbCrLf & sComment
lngRet = SetWindowText(hWndOKttbx, s)
Else
' Load and display our stored comment
s = s & vbCrLf & vbCrLf & rst.Fields("Comment")
lngRet = SetWindowText(hWndOKttbx, s)
End If

' Free our RecordSet
Set rst = Nothing
db.Close
Set db = Nothing

'DoCmd.Beep
Exit_Err_ShowComments:
Exit Function

Err_ShowComments:
MsgBox Err.Description
Resume Exit_Err_ShowComments

End Function
' From Dev Ashish's Site
' The Access Web
' http://www.mvps.org/access/

'******* Code Start *********
Private Function fGetClassName(hWnd As Long)
Dim strBuffer As String
Dim lngLen As Long
Const MAX_LEN = 255
strBuffer = Space$(MAX_LEN)
lngLen = apiGetClassName(hWnd, strBuffer, MAX_LEN)
If lngLen > 0 Then fGetClassName = Left$(strBuffer, lngLen)
End Function
'******* Code End *********
--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.

"sghi" <st**************@getronics.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
thank you Teddysn...,
but my problem is that i'd like to read in a string in a vba module the
sql statement that you can read in the "..."SQL view" from the "View"
button."

Ciao
Stefano

Feb 7 '06 #6
Thank you for the answer, i'll make good use of it.
The reason for my request is that i want to transform "on the fly" a
query in a make table query (putting a "INTO tempTable" before "FROM")
depending on the query content.

Ciao
Stefano

Feb 8 '06 #7

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

Similar topics

4
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the...
7
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
2
by: Melissa | last post by:
I have a single (not continuous) form with an Undo button for entering finished projects. On the form is also a subform that lists all finished projects for reference. When I enter the...
13
by: MLH | last post by:
Suppose I have this simple SQL string... SELECT tblDrivers.DriverID, tblDrivers.DName FROM tblDrivers WHERE (((tblDrivers.DName) Like "N*")) ORDER BY tblDrivers.DriverID; And suppose that...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
0
by: Dominique | last post by:
Hello, I have the following code: ===================================================== Public Sub LoadWriteNetworkDetails(ConnectString As String) ' Write the information to the table...
2
by: deekay | last post by:
Im trying to update a database where our users have been entering/editing all data using queries to now use forms instead. The first step that I want to take is to convert the queries to forms in...
3
by: israphelr | last post by:
Hi all. I have to put together some code that reads high scores from a saved file, then gives the user the opportunity to add their name and score to the high scores list, which is then saved. ...
1
by: danibecr | last post by:
I'm trying to make a table that will daily count the records imported and save them to a seperate table along with the date imported. But as of now after all the processing is complete I delete...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.