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

OpenRecordset problem when query includes parameters that reference to form controls

Hi,
I got wrong result When I open a query as recordset with code:

Expand|Select|Wrap|Line Numbers
  1. Set qdf = db.QueryDefs("[qryA]")
  2. For Each prm In qdf.Parameters
  3.         prm.Value = Eval(prm.name)
  4. Next prm
  5. Set rsB = qdf.OpenRecordset
SQL for [qryA] is:
Expand|Select|Wrap|Line Numbers
  1. SELECT [Account], ([RTM]-[LoanV]) AS [Mgncall],[RTM], LoanV FROM qry8_ChangeByGtor
  2. WHERE (([RTM]-[LoanV])>=[forms]![FrmRpt]![txtNHM1].[Value]);
What really confuses me is that I only got problem when the where clause includes the expression field ([RTM]-[LoanV]). When I remove the expression or form control, for example, change to "WHERE ([RTM]>=[forms]![FrmRpt]![txtNHM1].[Value]);" or "WHERE (([RTM]-[LoanV])>=500;)", the result will be all right.

Any idea is appreciated. Thanks!
Dec 9 '11 #1
12 2500
NeoPa
32,556 Expert Mod 16PB
Are you sure?

Adding .[Value] at the end of [forms]![FrmRpt]![txtNHM1] is both unnecessary and, as far as I've ever seen, incorrect SQL that doesn't run. SQL doesn't manage VBA objects in the way VBA code can. Any dot (.) in a name is treated as an unknown item.

Why don't you say exactly what you try and exactly what occurs when you do.
Dec 9 '11 #2
I think SQL only doesn't recognize forms("FrmRpt").txtNHM1.value , but it does recognize [forms]![FrmRpt]![txtNHM1].[Value].

Anyway I tried removing ".[Value]", but still the same. It seems the expression is the real reason causing problem.

Expand|Select|Wrap|Line Numbers
  1. SELECT [Account], ([RTM]-[LoanV]) AS [Mgncall],[RTM], LoanV FROM qry8_ChangeByGtor
  2. WHERE (([RTM]-[LoanV])>=[forms]![FrmRpt]![txtNHM1]);
gives incorrect result (records don't meet the where condition)
while
Expand|Select|Wrap|Line Numbers
  1. SELECT [Account], ([RTM]-[LoanV]) AS [Mgncall],[RTM], LoanV FROM qry8_ChangeByGtor
  2. WHERE (([RTM])>=[forms]![FrmRpt]![txtNHM1].[Value]);
produces correct result

By the way, the problem only happens when I run the query in VBA.

Thanks.
Dec 9 '11 #3
NeoPa
32,556 Expert Mod 16PB
Difei:
gives incorrect result (records don't meet the where condition)
I need more than this. I need to know what are the records which don't show but you think they ought to (values for [RTM] & [LoanV]) and what is the value of [forms]![FrmRpt]![txtNHM1] when you ran the test. I assume there may be a Null value somewhere you're not handling, but until I have some data to work with I cannot be much help.
Dec 9 '11 #4
Thanks NeoPa.

I made a sample DB(attachment) to illustrate the problem.

After running the code (Report button), Table2 and Query2should have the same records and so should Table3 and Query3. Apparently they don't. I see some pattern there, but just can not figure out why.
Attached Files
File Type: zip testQueryControl.zip (60.6 KB, 71 views)
Dec 9 '11 #5
ADezii
8,834 Expert 8TB
Is this what you re looking for?
Attached Files
File Type: zip testQueryControl_Revised.zip (48.7 KB, 83 views)
Dec 10 '11 #6
NeoPa
32,556 Expert Mod 16PB
I hope you find what you need in ADezii's attachment, as you haven't answered my questions. I'm afraid you may believe you have done so by posting an attachment, but while many people make that same mistake, it simply isn't answering the questions. We're on a forum. Attachments aren't posts, and answers to questions are not an attachment where I can find answers if I put in the effort that you don't seem prepared to put in. Answers to questions in a post are answers in a post. Attachments are almost entirely useless to the vast majority of people who read this thread, and should be used only when every effort has already been made to put the question or answer into words.

You should appreciate that this site is here for the benefit of many more users than ever become members and ask their own questions. If your thread becomes a private area where all the details are hidden in attachments then it becomes of little value to the site, and many experts may choose to ignore it on that basis, as well as the fact that having to download attachments increases the time and effort they need to spend in order to help you.

Attachments can, and often are, very helpful in padding out an answer or helping to clarify a question. In nearly all such cases they will be requested from you by an expert trying to help. If you ever get to answering questions they can also be helpful as examples to clarify your posts. Attachments on their own, unrequested, are rarely appreciated.

@ADezii
While that is also largely true of posting solutions, the problem that way around is much less of an issue. You are volunteering assistance, which may be limited to the very few (presumably including the OP) who will look at it if it's presented simply as an attachment, but the key word here is volunteering. This post is directed at the OP who decided that posting an attachment would be easier for them than answering the questions asked.
Dec 10 '11 #7
ADezii
8,834 Expert 8TB
@NeoPa:
Understood, and I do apologize for my inappropriate Response. I posted the Revised Attachment because I thought, but was not absolutely sure, that I had solved the problem. If the OP confirmed that the problem was resolved, I would have posted the details, if not, I would have asked for further, explicit information from the OP. In any event, you are correct in that a Revised Attachment, in the majority of cases, is useless and/or meaningless as far as resolving problems encountered by Users, and is also not beneficial to others as far as referencing.
Dec 10 '11 #8
NeoPa
32,556 Expert Mod 16PB
I don't really see it as my responsibility to criticise experts ADezii (not unless they do things which which are blatantly anti-Bytes which this is certainly not). Especially such prolific ones as yourself. However, I won't deny it's an important point that I'd like everyone at least to understand.
Dec 11 '11 #9
@NeoPa,
I apologize for the posting lacking of description - This is actually my first time posting a question in a forum. I was thinking a sample DB with all queries, forms and code would be the best to illustrate the problem, but I definitely learned a lesson.

@ADezii,
Your solution is perfect. Really appreciated.

@Anyone who runs into the same problem,
I'd like to summrize the problem and ADezii's solution a little bit to hopefully make it helpful to more people:
Problem:
I have a query containing parameters (numeric value) which reference to text box controls from a form. I got wrong result by running it in VBA (please refer to the code in the original post), although opening the query directly shows correct result.
Solution:
In the query, convert the text box value into numeric. e.g. use ">=Val([forms]![FrmRpt]![txtNHM1].[Value])" or ">=CCur([forms]![FrmRpt]![txtNHM1].[Value])" instead of ">=[forms]![FrmRpt]![txtNHM1].[Value]"

@ADezii,
Could you please add more details if I am missing anything? Thanks again.
Dec 12 '11 #10
NeoPa
32,556 Expert Mod 16PB
Difei Luo:
but I definitely learned a lesson.
And you post illustrates that. I'm very happy you got the point so well :-)

It's also good news that ADezii found a solution for you, and even better news that you understood it, so will know how to avoid it in future.
Dec 13 '11 #11
Stewart Ross
2,545 Expert Mod 2GB
For completeness, the problem here is the well-known one that VBA-executed queries cannot resolve references to form controls directly. ADezii's solution adopts the approach recommended by MS of setting the value of the missing parameters explicitly in code by using a querydef object to do so. This MSDN article provides background on the issue:

Everything About Using Parameters from Code

-Stewart
Dec 13 '11 #12
NeoPa
32,556 Expert Mod 16PB
Nice link Stewart. I wasn't aware of any of that. That explains how come Jet SQL can interpret form control links (at all).
Dec 14 '11 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John Hargrove | last post by:
I am building a database to manage test samples in an environmental laboratory. I am learning Access as I go and don't know much about the programming aspects. I hope to make the application...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
3
by: Bryan | last post by:
I've been looking over this newsgroup, but I can't find an answer to my problem. I see that a few others have posted this issue over the years, but none of them got a response. I'll give it...
2
by: jean_p | last post by:
I am trying to display the results in a form from a complex query that uses parameters. Both the outside and the inside query use parameters. I can get the outside query to run but when I use a...
14
by: 97T | last post by:
Well this is still bugging me. I know there are other ways around this, but for a number of reasons I would like to be able to do this one simple thing. I have a form with a number of controls...
1
by: vunderusaf | last post by:
I have a listbox on a form that is selecting using named FinalQuery: SELECT ., ., . FROM FinalQuery; Now I have a text field with a date on this form and I'd like to use that date as the...
7
by: jmp | last post by:
(I hope this isn't considered too far off-topic.) I work as a developer on a browser product found on handheld devices, and I'm trying to specify behavior for the browser to make it "as...
3
by: Jahangir | last post by:
Dear Fellows, I m in in Access VB, and working on a small project. Problem is that In main form (called search )I have made a textbox name text34, and a search button, which responsiblity is...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
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...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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: 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...

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.