473,587 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global variables are slowing down the query

Hello,

I have the following query that I set up as a test, and it runs fine:

SELECT STATUSHISTORIE. *
FROM STATUSHISTORIE LEFT JOIN PROBLEM_DE ON STATUSHISTORIE. PROBLEM_ID =
PROBLEM_DE.PROB LEMNR
WHERE (((STATUSHISTOR IE.STATUSDATUM) <#1/1/2005#) AND
((PROBLEM_DE.DA TENBEREICH)='SP MO') AND
(((Left(([PROBLEM_DE].[MODULZUORDNUNG]),InStr([PROBLEM_DE].[MODULZUORDNUNG],"-")-2)))='K29')
AND ((PROBLEM_DE.ER LSTAND)<>"WEIF" ))
ORDER BY STATUSHISTORIE. PROBLEM_ID,
STATUSHISTORIE. STATUSDATUM;

I then set up two global variables ( a String and a Date) and
respective functions to return them - ReturnE( ) and ReturnKW( ). Now
my query looks like this, but takes ages to run:

SELECT STATUSHISTORIE. *
FROM STATUSHISTORIE LEFT JOIN PROBLEM_DE ON
[STATUSHISTORIE].[PROBLEM_ID]=[ PROBLEM_DE].[PROBLEMNR]
WHERE (((STATUSHISTOR IE.STATUSDATUM) <ReturnKW( ) ) AND
((PROBLEM_DE.DA TENBEREICH)='SP MO') AND
(((Left(([PROBLEM_DE].[MODULZUORDNUNG]),InStr([PROBLEM_DE].[MODULZUORDNUNG],"-")-2)))=ReturnE(
) ) AND ((PROBLEM_DE.ER LSTAND)<>"WEIF" ))
ORDER BY [STATUSHISTORIE].[PROBLEM_ID], [STATUSHISTORIE].[STATUSDATUM];

The tables are actually Views set up from an ODBC Data source. Can
anyone please tell me why these global variables are causing the
traffic jam? :-)

Thanks in advance
J

Nov 13 '05 #1
15 2615
Me again...should maybe just add my Public functions that return the
global variables

Public gstrE As String 'global variable: contains E used for query
Public gdatKW As Date

Public Function ReturnE()

ReturnE = gstrE

End Function

Public Function ReturnKW()

ReturnKW = gdatKW

End Function

Nov 13 '05 #2
It's true. In Access 2000 and later, any user-defined VBA function acts like
an anchor.

Could you avoid this situation by reading the values from controls on a form
instead of using variables?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jean" <je**********@h otmail.com> wrote in message
news:11******** ************@g4 3g2000cwa.googl egroups.com...
Hello,

I have the following query that I set up as a test, and it runs fine:

SELECT STATUSHISTORIE. *
FROM STATUSHISTORIE LEFT JOIN PROBLEM_DE ON STATUSHISTORIE. PROBLEM_ID =
PROBLEM_DE.PROB LEMNR
WHERE (((STATUSHISTOR IE.STATUSDATUM) <#1/1/2005#) AND
((PROBLEM_DE.DA TENBEREICH)='SP MO') AND
(((Left(([PROBLEM_DE].[MODULZUORDNUNG]),InStr([PROBLEM_DE].[MODULZUORDNUNG],"-")-2)))='K29')
AND ((PROBLEM_DE.ER LSTAND)<>"WEIF" ))
ORDER BY STATUSHISTORIE. PROBLEM_ID,
STATUSHISTORIE. STATUSDATUM;

I then set up two global variables ( a String and a Date) and
respective functions to return them - ReturnE( ) and ReturnKW( ). Now
my query looks like this, but takes ages to run:

SELECT STATUSHISTORIE. *
FROM STATUSHISTORIE LEFT JOIN PROBLEM_DE ON
[STATUSHISTORIE].[PROBLEM_ID]=[ PROBLEM_DE].[PROBLEMNR]
WHERE (((STATUSHISTOR IE.STATUSDATUM) <ReturnKW( ) ) AND
((PROBLEM_DE.DA TENBEREICH)='SP MO') AND
(((Left(([PROBLEM_DE].[MODULZUORDNUNG]),InStr([PROBLEM_DE].[MODULZUORDNUNG],"-")-2)))=ReturnE(
) ) AND ((PROBLEM_DE.ER LSTAND)<>"WEIF" ))
ORDER BY [STATUSHISTORIE].[PROBLEM_ID], [STATUSHISTORIE].[STATUSDATUM];

The tables are actually Views set up from an ODBC Data source. Can
anyone please tell me why these global variables are causing the
traffic jam? :-)

Thanks in advance
J

Nov 13 '05 #3
Yes, I guess I could use that method.

Originally, I have the user select the string in a ComboBox, and the
date(s) from the second column in a ListBox. I have to iterate through
the date ListBox, as it is in Extended mode and a user can select
multiple entries.

But the point is: How do I reference the value of a form's control -
directly in SQL? Or does one do it in the QBE grid? I have never seen
this done so I would appreciate it if you could tell me.

Thanks for your support so far!

Nov 13 '05 #4
In the query design grid, typically in the Criteria row, you can refer to a
text box on an open form like this:
[Forms].[Form1].[Text0]

This becomes part of the SQL statement like this:
WHERE Table1.Field1 = [Forms].[Form1].[Text0]

Ideally you want to declare this as a parameter to the query, to ensure the
data type is interpreted correctly.

If you are comfortable with writing SQL, you could create the SQL string
dynamically, concatenating the value from the form or variable directly into
the SQL string, e.g.:
Dim strSql As String
strSql = "SELECT Table1.* FROM Table1 WHERE Table1.Field1 = " &
MyVariable & ";"

Then you can assign this to the RecordSource of a form:
Me.RecordSource = strSql
or to a saved query:
dbEngine(0)(0). QueryDefs("Quer y1").SQL = strSql

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jean" <je**********@h otmail.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Yes, I guess I could use that method.

Originally, I have the user select the string in a ComboBox, and the
date(s) from the second column in a ListBox. I have to iterate through
the date ListBox, as it is in Extended mode and a user can select
multiple entries.

But the point is: How do I reference the value of a form's control -
directly in SQL? Or does one do it in the QBE grid? I have never seen
this done so I would appreciate it if you could tell me.

Thanks for your support so far!

Nov 13 '05 #5
Jean wrote:
Yes, I guess I could use that method.

Originally, I have the user select the string in a ComboBox, and the
date(s) from the second column in a ListBox. I have to iterate through
the date ListBox, as it is in Extended mode and a user can select
multiple entries.
Since you were initially talking about slow queries, when you have a
multiselect listbox, you may want to go through all of the items and
create a string.

For each varItem in Me.ListBoxName. ItemsSelected
str = str & Me.ListBoxName. ItemData(varIte m) & ", "
Next
'remove comma/space at end and surround result in ()
str = "(" & Left(str,Len(st r)-2) & ")"

Let's assume the string now contains a bunch of IDs
(1,2,3,4,5)

Creating a dynamic query like Allen demonstrated, you could do something
like
strSQL = "Select * from Employee where ID In " & str
Me.Recordsource = strSQL

The ID In (1,2,3,4,5) method for your criteria will be extremely fast in
your query.
But the point is: How do I reference the value of a form's control -
directly in SQL? Or does one do it in the QBE grid? I have never seen
this done so I would appreciate it if you could tell me.
Oftentimes you create a query in the QBE grid and reference using the
Forms!FormName! ControlName. Sometimes you create the query in the grid,
and viewSQL from the menu and copy/paste that to a module for refining.

Thanks for your support so far!

Nov 13 '05 #6
"Jean" <je**********@h otmail.com> wrote in
news:11******** *************@g 47g2000cwa.goog legroups.com:
Me again...should maybe just add my Public functions that return
the global variables

Public gstrE As String 'global variable: contains E used for
query Public gdatKW As Date

Public Function ReturnE()

ReturnE = gstrE

End Function

Public Function ReturnKW()

ReturnKW = gdatKW

End Function


Well, for one, both of your functions have no return type declared.
Your first should be defined as:

Public Function ReturnE() As String

and your second as:

Public Function ReturnKW() As Date

Furthermore, your query would be greatly improved by declaring
parameters for these functions, so that the query processor wouldn't
have to figure out the data types at runtime.

In the QBE view, you can define parameters from the QUERY menu --
PARAMETERS is on that menu.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #7
"Allen Browne" <Al*********@Se eSig.Invalid> wrote in
news:42******** *************** @per-qv1-newsreader-01.iinet.net.au :
It's true. In Access 2000 and later, any user-defined VBA function
acts like an anchor.

Could you avoid this situation by reading the values from controls
on a form instead of using variables?


But the functions being used don't have types declared, and the
query itself does not have parameters defined. Changing either of
those to explicit data types is likely to cause the query to be
compiled differently, and my bet is that it will lead to the whole
thing being processed more efficiently.

Perhaps it has not occurred to most people to declare parameters for
functions? I do the same for references to controls on forms, and it
seems to me to speed things up.

But the functions should definitely have return types, instead of
being generic variants.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #8
Thank you all for your support. I am so full of new ideas now, and
really appreciate your feedback and experiences.

I first of all tried David's suggestion - and it notably did the
trick! I guess I was a bit slow on catching on that I had no return
type defined for my functions.

,,In the QBE view, you can define parameters from the QUERY menu --
PARAMETERS is on that menu."

,,Changing either of those to explicit data types is likely to cause
the query to be
compiled differently, and my bet is that it will lead to the whole
thing being processed more efficiently."
I still have not applied parameters to my query (and dont need to
anymore), but I will look up on this topic in the future.

Salad and Alan, I have to build SQL strings anyway later in my program
that are based on this query. I just thought that if I cannot get this
query optimised, then all the future stuff will be even slower. I am
using the method that Salad suggested in order to return values from
the ListBox.

Once again, thanks for all your input.

Kind Regards,

J

P.S. I am using Google Groups Beta - how do I add quoted text to my
post with the ">" charater appended to the rows?

Nov 13 '05 #9
On Tue, 09 Aug 2005 15:49:35 -0500, "David W. Fenton"
<dX********@bwa y.net.invalid> wrote:
"Jean" <je**********@h otmail.com> wrote in
news:11******* **************@ g47g2000cwa.goo glegroups.com:
Me again...should maybe just add my Public functions that return
the global variables

Public gstrE As String 'global variable: contains E used for
query Public gdatKW As Date

Public Function ReturnE()

ReturnE = gstrE

End Function

Public Function ReturnKW()

ReturnKW = gdatKW

End Function


Well, for one, both of your functions have no return type declared.
Your first should be defined as:

Public Function ReturnE() As String

and your second as:

Public Function ReturnKW() As Date

Furthermore, your query would be greatly improved by declaring
parameters for these functions, so that the query processor wouldn't
have to figure out the data types at runtime.

In the QBE view, you can define parameters from the QUERY menu --
PARAMETERS is on that menu.


Not only would the query not have to determine the types at run-time, I'm
betting it's so slow now because it calls the function multiple times.
Whenever JET has to make calls into a user-defined function, there's a big
time hit.
Nov 13 '05 #10

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

Similar topics

8
7643
by: Dan | last post by:
Quick question about passing variables to subs to reduce the need for publicly declared variables in VB6. If I have an event sub (MouseDown) that runs a few lines of code, how can I use a variable in that sub without making it global? I tried adding the variable to the arguments of the sub, but VB doesn't seem to like anything other than the...
1
4345
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of doing it - is it not? Once the application has loaded the page it is cached and is immediately available for other pages. With global variables -...
8
2527
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables...
35
721
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation) I want to dynamically create a multidimensional array and write code to manipulate that. Essentially I have the following setup:
7
3119
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
41
10652
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
10
2637
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a...
8
3810
by: rottmanj | last post by:
. In order to teach my self more. I have started to convert some of my cf scheduled tasks to perl applications. One area where things are kind of fuzzy is setting up global variables that can be called from any module with in an application. So far I have created a farily standard module that will act as my global config. This module will...
112
5392
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
8347
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...
0
8220
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...
1
5718
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...
0
5394
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...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.