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

Passing variable to SQL query, no records returned.

23
I'm using Access 97 SR-2 on Windows XP to develop an Inventory style database that has some functionality, but having a problem making my query dynamic. I have a switchboard style form which I choose from a selection of 3 command buttons to select a Due Date for record reviews, choices being 30 days, 60 days, and 120 days. When I click on one of the buttons, A public variable, mqryDueDate, is set, for example, to today's date + 30 days, and is of 'Date' type in the declarations. This variable is then used in my query to pull all records which have a due date within the next 30 days. Simple stuff I thought, but the way I have my code and SQL statement, I have no records returned. If I have the query prompt me to manualy enter a date, the correct records ARE returned, but it's when trying to pass the variable data through to the query, nothing is returned. Using the command button's OnClick event, the following (relevant) VBA code is run...

Expand|Select|Wrap|Line Numbers
  1.  
  2. mqryDueDate = Date + 30
  3.  
  4. DoCmd.OpenReport "rptDueDate", acViewPreview
  5.  
  6.  
The report's record source is a query called qryDueDate, and contains a SQL statement which is supposed to capture the value of variable mqryDueDate and return records based on..

Expand|Select|Wrap|Line Numbers
  1.  
  2. WHERE (((AllInstructions.DueDate)< [Forms]![frmReportByDueDate]!mqryDueDate));
  3.  
  4.  
However, no results are returned, and no error is received.

I have had a look at various posts regarding this kind of situation, but no resolve was found. I am suspecting it's that my version of Access isn't able to do this, or I've typed in the WHERE statement wrong.

Any help would be appreciated, and if I've forgotten to provide any information (as per posting guidelines), I do apologise.

thanks...
Mar 10 '10 #1

✓ answered by TheSmileyCoder

Should be as simple as this:
Expand|Select|Wrap|Line Numbers
  1.   'Set Date
  2.     Me.txtqryDueDate=Date+30
  3.   'Open report
  4.     DoCmd.OpenReport "rptDueDate", acViewPreview 
And I was wrong about the # , they are not needed if your using a textbox.
Expand|Select|Wrap|Line Numbers
  1. WHERE (((AllInstructions.DueDate)< [Forms]![frmReportByDueDate]![txtqryDueDate]));

9 4155
TheSmileyCoder
2,322 Expert Mod 2GB
Is mqryDueDate bound? I suspect maybe if its unbound, access doesn't know that your storing a date type in the field, and formats it as a string instead? (this is just a guess)

You could try:
Expand|Select|Wrap|Line Numbers
  1. WHERE (((AllInstructions.DueDate)< & "#" & [Forms]![frmReportByDueDate]!mqryDueDate & "#" & ));
Mar 10 '10 #2
d4zza
23
Thanks for quick reply, however, I'm getting a syntax error (missing operator) when I cut and paste the amended SQL statement. I'm sure this is just something silly that I'm missing

Also, the variable is not bound. It's purely a public variable that is used and set only when the OnClick event occurs, and then used for query.

The OnClick vba code is simple only the two lines previously quoted (but obviously with start and end sub and error handler).

If it helps, here is the full SQL statement

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW AllInstructions.DocumentNumber, AllInstructions.IssueNumber, AllInstructions.DisciplineID,  AllInstructions.DocumentStatusID, AllInstructions.DueDate
  2. FROM AllInstructions
  3. WHERE (((AllInstructions.DueDate)< & "#" & [Forms]![frmReportByDueDate]!mqryDueDate & "#" & )); 
  4.  
and error...

Syntax error (missing operator) in query expressions '(((AllInstructions.DueDate)< & "#" & [Forms]![frmReportByDueDate]!mqryDueDate & "#" & ))'.
thanks...
Attached Images
File Type: jpg error.jpg (17.8 KB, 324 views)
Mar 10 '10 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Is mqryDueDate a variable or a textbox on the form? I dont think the SQL engine can see the variables, even if public (but im unsure, I have never tried)
Mar 10 '10 #4
TheSmileyCoder
2,322 Expert Mod 2GB
Well tried setting a public variable for the form, and referencing that from my query, but no such luck. I didn't get any error msg, just no returned records.
I created a unbound textbox (visible=false) and set that using code, then opening query, referencing the Hidden textbox in my criteria, and that worked just fine.
Mar 10 '10 #5
d4zza
23
It's a variable. The form has 3 command buttons for selecting a Due Date (and 1 to exit). When the command button is clicked, the OnClick event runs the VBA code to set mqryDueDate to the selected date.

It would make sense to me (disappointingly) that it doesn't work if the SQL engine can't see the variable, and I suppose I'll have to come up with another method of creating a dynamic query for reports.

I may even just have users enter the date manually, not ideal for my purposes, but whatever works will have to do.

thanks...
Mar 10 '10 #6
d4zza
23
I'll try using the unbound text box just now and post my result.

thanks...
Mar 10 '10 #7
d4zza
23
I created a unbound textbox (visible=false) and set that using code, then opening query, referencing the Hidden textbox in my criteria, and that worked just fine.
I've tried doing this, but it's not working. I do know, however, that it's because I'm not sure how to do this.

If I call the textbox txtqryDueDate, can you provide the code to do this please, and what the SQL query would then be?

thanks...
Mar 10 '10 #8
TheSmileyCoder
2,322 Expert Mod 2GB
Should be as simple as this:
Expand|Select|Wrap|Line Numbers
  1.   'Set Date
  2.     Me.txtqryDueDate=Date+30
  3.   'Open report
  4.     DoCmd.OpenReport "rptDueDate", acViewPreview 
And I was wrong about the # , they are not needed if your using a textbox.
Expand|Select|Wrap|Line Numbers
  1. WHERE (((AllInstructions.DueDate)< [Forms]![frmReportByDueDate]![txtqryDueDate]));
Mar 10 '10 #9
d4zza
23
Thanks, Smiley. That works ideally!!! :)

When I tried it first time, I missed the "me." when setting the value! yeah, silly, I know. :doh:

thanks...
Mar 10 '10 #10

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

Similar topics

2
by: Mike Poe | last post by:
Hi, Consider the following: <? $username = "foo"; $password = "bar"; $host = "db";
5
by: Jerome | last post by:
Hi, I've got the following problem: I want my ASP page to display a certain number (based on a COUNT query), it works fine if the result is at least 1! If there are no records to be counted...
4
by: shumaker | last post by:
I'm wondering how/why this query works. Trying to get my head wrapped around SQL. Basically the Query deletes from the Import table all records that are already in FooStrings so that when I do an...
2
by: Matthew Clement | last post by:
I'm currently building a form (called frmReports) to set the criteria for a query, but I'm having some trouble with syntax and hope that one of the guru's here can help me achieve what I'm do. ...
1
by: Michelle | last post by:
Hi all I am having problems creating an update query. I have 2 tables, tblPublishRoster and tblCCAgents_Changed_Shifts. I want to select all records from tblCCAgents_ChangedShifts where...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
3
by: etwebbox | last post by:
Hoping someone has some ideas on how to solve this issue: I have a macro in excel which runs a query against an Access DB (it actually calls a query inside of Access). The query is not bringing...
3
by: John Baker | last post by:
Hi:7 Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have named HireInput,...
2
by: Bob Sanderson | last post by:
I have a search form from which I hope to be able to select a record by field JobNumber and display it with an output form titled test.php <html> <head> <title>Job Database Search</title>...
14
by: Matt | last post by:
I need to add the following variable into an SQL statement and not sure how to do it. strGCID needs to be inserted into the following statement: SQL = "SELECT tblContacts.* FROM tblContacts...
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: 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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.