A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

sharing query created in VBA



 
 
Thread Tools Display Modes
  #1  
Old July 7th, 2009, 04:55 PM posted to microsoft.public.access
John B. Smotherman
external usenet poster
 
Posts: 55
Default sharing query created in VBA

I have a form with a couple of listboxes. The rowsource for the listboxes is
two queries that I build the SQL for in the form's module. I'd like to create
a report that also uses the same queries. Keeping in mind that the SQL for
these two could be rather lengthy, is there an easy way to share the queries
between the form and the report? I've thought about using openargs in the
DoCmd.OpenReport call, but is there a better way?

Thanks!
  #2  
Old July 7th, 2009, 05:21 PM posted to microsoft.public.access
marcos
external usenet poster
 
Posts: 2
Default sharing query created in VBA


"John B. Smotherman" escribió en
el mensaje de noticias
...
I have a form with a couple of listboxes. The rowsource for the listboxes
is
two queries that I build the SQL for in the form's module. I'd like to
create
a report that also uses the same queries. Keeping in mind that the SQL for
these two could be rather lengthy, is there an easy way to share the
queries
between the form and the report? I've thought about using openargs in the
DoCmd.OpenReport call, but is there a better way?

Thanks!


  #3  
Old July 7th, 2009, 06:07 PM posted to microsoft.public.access
Klatuu
external usenet poster
 
Posts: 7,074
Default sharing query created in VBA

Here is a function that will do it. You first need to create a stored query
with the name of the query you want to use for both. Then, the following
code will change the SQL in the stored query to the string you build:

Public Function MakeQuery(QueryName As String, QuerySQL As String) As Boolean
Dim qdf As QueryDef

On Error GoTo MakeQuery_Error

MakeQuery = True

Set qdf = dbs.QueryDefs(QueryName)
qdf.SQL = QuerySQL
qdf.Close

Set qdf = Nothing


MakeQuery_Exit:

Exit Function
On Error GoTo 0

MakeQuery_Error:

MakeQuery = False

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure MakeQuery of Module modUtilities"
GoTo MakeQuery_Exit

End Function

--
Dave Hargis, Microsoft Access MVP


"John B. Smotherman" wrote:

I have a form with a couple of listboxes. The rowsource for the listboxes is
two queries that I build the SQL for in the form's module. I'd like to create
a report that also uses the same queries. Keeping in mind that the SQL for
these two could be rather lengthy, is there an easy way to share the queries
between the form and the report? I've thought about using openargs in the
DoCmd.OpenReport call, but is there a better way?

Thanks!

  #4  
Old July 7th, 2009, 06:08 PM posted to microsoft.public.access
Klatuu
external usenet poster
 
Posts: 7,074
Default sharing query created in VBA

Sorry, syntax error in the previous post. Here is the correction:

Public Function MakeQuery(QueryName As String, QuerySQL As String) As Boolean
Dim qdf As QueryDef

On Error GoTo MakeQuery_Error

MakeQuery = True

Set qdf = CurrentDb.QueryDefs(QueryName)
qdf.SQL = QuerySQL
qdf.Close

Set qdf = Nothing


MakeQuery_Exit:

Exit Function
On Error GoTo 0

MakeQuery_Error:

MakeQuery = False

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure MakeQuery of Module modUtilities"
GoTo MakeQuery_Exit

End Function

--
Dave Hargis, Microsoft Access MVP


"John B. Smotherman" wrote:

I have a form with a couple of listboxes. The rowsource for the listboxes is
two queries that I build the SQL for in the form's module. I'd like to create
a report that also uses the same queries. Keeping in mind that the SQL for
these two could be rather lengthy, is there an easy way to share the queries
between the form and the report? I've thought about using openargs in the
DoCmd.OpenReport call, but is there a better way?

Thanks!

  #5  
Old July 9th, 2009, 12:53 AM posted to microsoft.public.access
James A. Fortune
external usenet poster
 
Posts: 903
Default sharing query created in VBA

Klatuu wrote:

Public Function MakeQuery(QueryName As String, QuerySQL As String) As Boolean
Dim qdf As QueryDef

On Error GoTo MakeQuery_Error

MakeQuery = True

Set qdf = CurrentDb.QueryDefs(QueryName)
qdf.SQL = QuerySQL
qdf.Close

Set qdf = Nothing


MakeQuery_Exit:

Exit Function
On Error GoTo 0

MakeQuery_Error:

MakeQuery = False

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure MakeQuery of Module modUtilities"
GoTo MakeQuery_Exit

End Function


klatuu,

I notice that you put the name of the module and procedure in your On
Error code. That's a good idea. Can you think of a situation, perhaps
something like multiple instances of a form, whe

Application.CodeContextObject.Name

might be helpful?

Thanks,

James A. Fortune

  #6  
Old July 9th, 2009, 01:01 PM posted to microsoft.public.access
Klatuu
external usenet poster
 
Posts: 7,074
Default sharing query created in VBA

I was not even aware of that object. I can see where it would be useful in a
generic error handler as well as some other situations.
--
Dave Hargis, Microsoft Access MVP


"James A. Fortune" wrote:

Klatuu wrote:

Public Function MakeQuery(QueryName As String, QuerySQL As String) As Boolean
Dim qdf As QueryDef

On Error GoTo MakeQuery_Error

MakeQuery = True

Set qdf = CurrentDb.QueryDefs(QueryName)
qdf.SQL = QuerySQL
qdf.Close

Set qdf = Nothing


MakeQuery_Exit:

Exit Function
On Error GoTo 0

MakeQuery_Error:

MakeQuery = False

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure MakeQuery of Module modUtilities"
GoTo MakeQuery_Exit

End Function


klatuu,

I notice that you put the name of the module and procedure in your On
Error code. That's a good idea. Can you think of a situation, perhaps
something like multiple instances of a form, whe

Application.CodeContextObject.Name

might be helpful?

Thanks,

James A. Fortune


 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:38 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.