473,385 Members | 1,630 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,385 software developers and data experts.

Why does a VBA function call from a sub-query cause an infinite loop?

I have an SQL Query that looks like this. I simplified it for the
purpose of this example, but it's still a runnable query

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction(FundedPositions.PositionNumber))AS
EmployeeName
FROM FundedPositions

As you can see, it should return one record, and uses a subquery to get
the EmployeeName column. In the subquery, I call a VBA Function,
called TestMyFunction. The function contains one paramater, which I
pass from the outer query and returns the value that I will join with
a.assign_i. Here is what the function looks like, simplified again for
this example:

Public Function TestMyFunction(PositionNumber As String)

TestMyFunction = "1234"

End Function

For some reason, the function is called infinitely, making everything
hang. Why is this happening? When I put a braekpoint at the start of
the function, I see it go through once with the right value being
passed, but then it goes through again ad-nauseum, with the same value.

The problem doesn't seem to be with the query, since I tried replacing
the function call with a hard-coded value, and everything is fine.

The problem doesn't seem to be with the function either, since if I
pass a hardcoded value in the function call, everything is fine. For
example, the example below is fine (notice how I pass '5555'):

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction('55555'))AS EmployeeName
FROM FundedPositions

Can anyone help me? I can provide table descriptions if you want, but
I don't think the tables are the matter here.

Thanks in advance,

Marc

Dec 1 '06 #1
5 2393
Is it really an infinite loop?

How many records are there in [dbo_assign a] ?

My guess is that the function is being called that many times and will
complete. When all the records have been read the the query will return the
"TOP 1", but not before.

Apologies if I am way off beam.

"marcsirois" <ma********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>I have an SQL Query that looks like this. I simplified it for the
purpose of this example, but it's still a runnable query

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction(FundedPositions.PositionNumber))AS
EmployeeName
FROM FundedPositions

As you can see, it should return one record, and uses a subquery to get
the EmployeeName column. In the subquery, I call a VBA Function,
called TestMyFunction. The function contains one paramater, which I
pass from the outer query and returns the value that I will join with
a.assign_i. Here is what the function looks like, simplified again for
this example:

Public Function TestMyFunction(PositionNumber As String)

TestMyFunction = "1234"

End Function

For some reason, the function is called infinitely, making everything
hang. Why is this happening? When I put a braekpoint at the start of
the function, I see it go through once with the right value being
passed, but then it goes through again ad-nauseum, with the same value.

The problem doesn't seem to be with the query, since I tried replacing
the function call with a hard-coded value, and everything is fine.

The problem doesn't seem to be with the function either, since if I
pass a hardcoded value in the function call, everything is fine. For
example, the example below is fine (notice how I pass '5555'):

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction('55555'))AS EmployeeName
FROM FundedPositions

Can anyone help me? I can provide table descriptions if you want, but
I don't think the tables are the matter here.

Thanks in advance,

Marc


Dec 1 '06 #2
On 1 Dec 2006 12:39:07 -0800, "marcsirois" <ma********@gmail.com>
wrote:

David is right: most likely your function is called MANY times, and it
takes a while to process.
Then also realize that Access is smart enough to realize if a function
is returning a constant value like in your simplified example. It then
only calls your function once, and caches the "1234" result for all
rows.
Also realize that if the function argument is constant, Access again
assumes with a constant argument the function couldn't possibly be
returning diffent values every time (even though this is a WRONG
assumption, e.g. if the function returns Now()), and it caches the
first return value. To overcome this, additionally pass in the PK.

Do optimize your function for speed. For example if it is opening a
recordset, perhaps you can open this rs BEFORE you call this
procedure, and close it after it ends. Also don't use an error handler
which can be a real performance drag. Rather write perfect code :-)

-Tom.
>I have an SQL Query that looks like this. I simplified it for the
purpose of this example, but it's still a runnable query

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction(FundedPositions.PositionNumber))AS
EmployeeName
FROM FundedPositions

As you can see, it should return one record, and uses a subquery to get
the EmployeeName column. In the subquery, I call a VBA Function,
called TestMyFunction. The function contains one paramater, which I
pass from the outer query and returns the value that I will join with
a.assign_i. Here is what the function looks like, simplified again for
this example:

Public Function TestMyFunction(PositionNumber As String)

TestMyFunction = "1234"

End Function

For some reason, the function is called infinitely, making everything
hang. Why is this happening? When I put a braekpoint at the start of
the function, I see it go through once with the right value being
passed, but then it goes through again ad-nauseum, with the same value.

The problem doesn't seem to be with the query, since I tried replacing
the function call with a hard-coded value, and everything is fine.

The problem doesn't seem to be with the function either, since if I
pass a hardcoded value in the function call, everything is fine. For
example, the example below is fine (notice how I pass '5555'):

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction('55555'))AS EmployeeName
FROM FundedPositions

Can anyone help me? I can provide table descriptions if you want, but
I don't think the tables are the matter here.

Thanks in advance,

Marc
Dec 2 '06 #3
For one thing, the "dbo_" indicates your records are on SQL Server. If the
function is a VBA function in Access, you may be forcing every record to be
retrieved and processed locally on the client to apply the function rather
than the entire SELECT operation being done on the server, with only 1
record being returned -- SQL Server cannot execute your Access VBA functions
for you.

Further, I do not know what effect your use of the subquery has on
performance, but my suspicion is that it would have a negative effect.

Larry Linson
Microsoft Access MVP

"marcsirois" <ma********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>I have an SQL Query that looks like this. I simplified it for the
purpose of this example, but it's still a runnable query

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction(FundedPositions.PositionNumber))AS
EmployeeName
FROM FundedPositions

As you can see, it should return one record, and uses a subquery to get
the EmployeeName column. In the subquery, I call a VBA Function,
called TestMyFunction. The function contains one paramater, which I
pass from the outer query and returns the value that I will join with
a.assign_i. Here is what the function looks like, simplified again for
this example:

Public Function TestMyFunction(PositionNumber As String)

TestMyFunction = "1234"

End Function

For some reason, the function is called infinitely, making everything
hang. Why is this happening? When I put a braekpoint at the start of
the function, I see it go through once with the right value being
passed, but then it goes through again ad-nauseum, with the same value.

The problem doesn't seem to be with the query, since I tried replacing
the function call with a hard-coded value, and everything is fine.

The problem doesn't seem to be with the function either, since if I
pass a hardcoded value in the function call, everything is fine. For
example, the example below is fine (notice how I pass '5555'):

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction('55555'))AS EmployeeName
FROM FundedPositions

Can anyone help me? I can provide table descriptions if you want, but
I don't think the tables are the matter here.

Thanks in advance,

Marc

Dec 2 '06 #4
Thanks for your insightful replies David, Tom and Larry.
What you are saying makes sense. This is why I will try to find
another approach to solving my problem, without using a VBA function.
As an MS Access newbie, I find the flexibility very limited. In SQL
Server, I could have solved my problem with a simple cursor inside a
stored procedure.

Thanks again for your help.
Marc
marcsirois wrote:
I have an SQL Query that looks like this. I simplified it for the
purpose of this example, but it's still a runnable query

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction(FundedPositions.PositionNumber))AS
EmployeeName
FROM FundedPositions

As you can see, it should return one record, and uses a subquery to get
the EmployeeName column. In the subquery, I call a VBA Function,
called TestMyFunction. The function contains one paramater, which I
pass from the outer query and returns the value that I will join with
a.assign_i. Here is what the function looks like, simplified again for
this example:

Public Function TestMyFunction(PositionNumber As String)

TestMyFunction = "1234"

End Function

For some reason, the function is called infinitely, making everything
hang. Why is this happening? When I put a braekpoint at the start of
the function, I see it go through once with the right value being
passed, but then it goes through again ad-nauseum, with the same value.

The problem doesn't seem to be with the query, since I tried replacing
the function call with a hard-coded value, and everything is fine.

The problem doesn't seem to be with the function either, since if I
pass a hardcoded value in the function call, everything is fine. For
example, the example below is fine (notice how I pass '5555'):

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction('55555'))AS EmployeeName
FROM FundedPositions

Can anyone help me? I can provide table descriptions if you want, but
I don't think the tables are the matter here.

Thanks in advance,

Marc
Dec 4 '06 #5
As an MS Access newbie, I find the flexibility very limited....

Just perhaps that could be translated into "I do not know how to do this in
Access".
If you look through the Access newsgroups you will find practically every
manipulation you could imagine done to names without recourse to custom VBA
functions.

What problem were you trying to solve?

"marcsirois" <ma********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
Thanks for your insightful replies David, Tom and Larry.
What you are saying makes sense. This is why I will try to find
another approach to solving my problem, without using a VBA function.
As an MS Access newbie, I find the flexibility very limited. In SQL
Server, I could have solved my problem with a simple cursor inside a
stored procedure.

Thanks again for your help.
Marc
marcsirois wrote:
>I have an SQL Query that looks like this. I simplified it for the
purpose of this example, but it's still a runnable query

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction(FundedPositions.PositionNumber))AS
EmployeeName
FROM FundedPositions

As you can see, it should return one record, and uses a subquery to get
the EmployeeName column. In the subquery, I call a VBA Function,
called TestMyFunction. The function contains one paramater, which I
pass from the outer query and returns the value that I will join with
a.assign_i. Here is what the function looks like, simplified again for
this example:

Public Function TestMyFunction(PositionNumber As String)

TestMyFunction = "1234"

End Function

For some reason, the function is called infinitely, making everything
hang. Why is this happening? When I put a braekpoint at the start of
the function, I see it go through once with the right value being
passed, but then it goes through again ad-nauseum, with the same value.

The problem doesn't seem to be with the query, since I tried replacing
the function call with a hard-coded value, and everything is fine.

The problem doesn't seem to be with the function either, since if I
pass a hardcoded value in the function call, everything is fine. For
example, the example below is fine (notice how I pass '5555'):

SELECT TOP 1
FundedPositions.PositionNumber AS [Position],
(select top 1
assign_i
from dbo_assign a
where a.scenario_i=1
and a.assign_i = TestMyFunction('55555'))AS EmployeeName
FROM FundedPositions

Can anyone help me? I can provide table descriptions if you want, but
I don't think the tables are the matter here.

Thanks in advance,

Marc


Dec 4 '06 #6

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

Similar topics

2
by: neptune | last post by:
I have limited knowledge of VBA, but I've learned a lot here. I can't figure out how to pass 2 parameters in a sub? I call PassDate and pass 1 date. Public Sub PassDate() Calculate...
2
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit...
6
by: grist2mill | last post by:
I want to create a standard tool bar that appears on all pages that is a control. The toolbar has a button 'New'. What I wolud like when the user clicks on 'New' depends on the page they are on. I...
1
by: Bruce | last post by:
I use btnSave.Attributes.Add("onclick", "ShowMessage()") to link my web control button to a JavaScript function. It works well until I added a Validation control into the page. After that,...
3
by: Sam Learner | last post by:
Hello everyone, I am developping an application, I create a thread for the application because it is about to download a large file, and wanted it to do it inside of a thread... Now, the function...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
0
by: mike | last post by:
Hi, When I programatically Bind a DataSource to DetailsView it does not fire "ModeChanged" event. This is first time i am trying to use ASP.NET DetailsView control. I have played with some of the...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
1
by: rodriguez.a.christian | last post by:
I cannot make my update command work for a datagrid: Public Class subcatinsert Inherits System.Web.UI.Page Protected WithEvents SqlCon As System.Data.SqlClient.SqlConnection Protected...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.