473,466 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Create 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.PROBLEMNR
WHERE (((STATUSHISTORIE.STATUSDATUM)<#1/1/2005#) AND
((PROBLEM_DE.DATENBEREICH)='SPMO') AND
(((Left(([PROBLEM_DE].[MODULZUORDNUNG]),InStr([PROBLEM_DE].[MODULZUORDNUNG],"-")-2)))='K29')
AND ((PROBLEM_DE.ERLSTAND)<>"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 (((STATUSHISTORIE.STATUSDATUM)<ReturnKW( ) ) AND
((PROBLEM_DE.DATENBEREICH)='SPMO') AND
(((Left(([PROBLEM_DE].[MODULZUORDNUNG]),InStr([PROBLEM_DE].[MODULZUORDNUNG],"-")-2)))=ReturnE(
) ) AND ((PROBLEM_DE.ERLSTAND)<>"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 2601
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**********@hotmail.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.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.PROBLEMNR
WHERE (((STATUSHISTORIE.STATUSDATUM)<#1/1/2005#) AND
((PROBLEM_DE.DATENBEREICH)='SPMO') AND
(((Left(([PROBLEM_DE].[MODULZUORDNUNG]),InStr([PROBLEM_DE].[MODULZUORDNUNG],"-")-2)))='K29')
AND ((PROBLEM_DE.ERLSTAND)<>"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 (((STATUSHISTORIE.STATUSDATUM)<ReturnKW( ) ) AND
((PROBLEM_DE.DATENBEREICH)='SPMO') AND
(((Left(([PROBLEM_DE].[MODULZUORDNUNG]),InStr([PROBLEM_DE].[MODULZUORDNUNG],"-")-2)))=ReturnE(
) ) AND ((PROBLEM_DE.ERLSTAND)<>"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("Query1").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**********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.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(varItem) & ", "
Next
'remove comma/space at end and surround result in ()
str = "(" & Left(str,Len(str)-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**********@hotmail.com> wrote in
news:11*********************@g47g2000cwa.googlegro ups.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*********@SeeSig.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********@bway.net.invalid> wrote:
"Jean" <je**********@hotmail.com> wrote in
news:11*********************@g47g2000cwa.googlegr oups.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
Steve Jorgensen wrote:
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.


IME if the function has no parameters Access will call it only once.
Although I've not used one in the criteria so that may be the difference.
Nov 13 '05 #11
On Wed, 10 Aug 2005 08:08:06 +0100, Trevor Best <no****@nospam.invalid> wrote:
Steve Jorgensen wrote:
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.


IME if the function has no parameters Access will call it only once.
Although I've not used one in the criteria so that may be the difference.


My experience matches yours. I'm just not sure if one can rely on that rule
in all cases.
Nov 13 '05 #12
Steve Jorgensen <no****@nospam.nospam> wrote in
news:su********************************@4ax.com:
On Tue, 09 Aug 2005 15:49:35 -0500, "David W. Fenton"
<dX********@bway.net.invalid> wrote:
"Jean" <je**********@hotmail.com> wrote in
news:11*********************@g47g2000cwa.googleg roups.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.


No, it will only call it once, since it isn't going to return a
different value. It would only get called multiple times if it were
accepting a value from each row as an input.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #13
Steve Jorgensen <no****@nospam.nospam> wrote in
news:90********************************@4ax.com:
On Wed, 10 Aug 2005 08:08:06 +0100, Trevor Best
<no****@nospam.invalid> wrote:
Steve Jorgensen wrote:
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.


IME if the function has no parameters Access will call it only
once. Although I've not used one in the criteria so that may be
the difference.


My experience matches yours. I'm just not sure if one can rely on
that rule in all cases.


If it's in the criteria and it doesn't take an argument why in the
world would the optimizer call it more than once?

Also, having a function in the criteria won't cause all the
processing to be client-side (instead of server-side), since once
the functions have been evaluated, the result can be sent to the
server as the criteria to filter on.

It is only when you have Access functions that *can't* be processed
before sending to the server that everything gets pulled to the
client and processed there. An example would be sorting on a
function. Since the function is client-side, all the data has to be
pulled and sorted. And if there are criteria on a function, same
thing.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #14
"Jean" <je**********@hotmail.com> wrote in
news:11**********************@g43g2000cwa.googlegr oups.com:
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.
Well, there was an implicit AS VARIANT there. That slows things down
in any context, since VARIANT is the most complex data type you can
have.
,,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."
Yes, definitely. By defining parameters you're telling the Jet query
optimizer up front that the results of those parameters can be
evaluated first before sending them off to the server for
processing. Once you have non-variant return types on your
functions, the query processor would probably be able to figure that
out, too, but you're speeding things up by taking that task away
from the query optimizer and telling it off the bat that it can
evaluate the functions before sending the request to the server.
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.


It might speed it up even more. Even if it does not, you would
probably doing yourself a favor by declaring the parameters because
you're making life easier for the query optimizer. By doing so,
you're also eliminating the remote possibility of the query
optimizer guessing wrong. Secondly, you're going to speed up the
execution of the query after a compact of the front end, which
discards the compiled optimization.

In general, though, I don't use parameters in saved queries, because
most of my dynamic recordsources are defined in code, where I
resolve the criteria before writing the SQL.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #15
David W. Fenton wrote:
Steve Jorgensen <no****@nospam.nospam> wrote in
news:90********************************@4ax.com:

On Wed, 10 Aug 2005 08:08:06 +0100, Trevor Best
<no****@nospam.invalid> wrote:
IME if the function has no parameters Access will call it only
once. Although I've not used one in the criteria so that may be
the difference.


My experience matches yours. I'm just not sure if one can rely on
that rule in all cases.

If it's in the criteria and it doesn't take an argument why in the
world would the optimizer call it more than once?


I didn't say it would, I said I never tried it. I wouldn't expect it to
but then also IME Access doesn't always do things the way I expect.
Nov 13 '05 #16

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

Similar topics

8
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...
1
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...
8
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...
35
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)...
7
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...
41
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 =...
10
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...
8
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...
112
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.