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

How to write a "where" condition for a report?

sueb
379 256MB
I have a report that I want to preview from a button on a form. This report should execute only for the record currently displayed in the form. Here's the button's code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Preview_This_18_1_Click()
  2. On Error GoTo Err_Preview_This_18_1_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.  
  7.     stLinkCriteria = "[18-1.Index]=" & Me![Index]
  8.  
  9.     stDocName = "18-1 State Form"
  10.     DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  11.     stDocName = "18-1 Continuation"
  12.     DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  13.  
  14. Exit_Preview_This_18_1_Click:
  15.     Exit Sub
  16.  
  17. Err_Preview_This_18_1_Click:
  18.     MsgBox Err.Description
  19.     Resume Exit_Preview_This_18_1_Click
  20.  
  21. End Sub
  22.  
I am failing to set the link criteria string correctly (it complains about how I'm bracketing it, regardless of what I try), but I think also that's not what a report calls for (as opposed to a form).
Apr 20 '11 #1

✓ answered by TheSmileyCoder

That all looks correct to me.

My first suggestion as I have noticed your quite active and engaged in Access, is to read NeoPa's debugging articles in the Bytes Insights section. Good debugging skills are essential to any serious programmer.

Next suggestion would be to insert BEFORE line 10 and line 12 the line:
Expand|Select|Wrap|Line Numbers
  1. Msgbox "About to open report:" & stDocName & " with filter:" & stLinkCriteria
If that is still not working, check that the field [18-1 index] is part of your reports recordsource.

8 6206
TheSmileyCoder
2,322 Expert Mod 2GB
Assuming 18-1 is your table, have you tried:
stLinkCriteria = "[18-1].[Index]=" & Me![Index]

You may be having trouble because I believe that Index is a reserved word. I do not really know what trouble can come from using reserved words as field names, since I simply do not use them.

The stLinkCriteria is simply a WHERE clause with the WHERE keyword. It works the same for reports as it does for forms, in limiting the underlying recordset to those fulfilling the where clause.
Apr 22 '11 #2
sueb
379 256MB
On your advice, SmileyCoder, I went through the entire database and changed all instances of the name "Index" to "[modulename] Index", so no more complaints about that--thanks! (I didn't realize "Index" was reserved.)

However, my report is still not grabbing the correct record. I have a form that looks just like the report (it displays the data with a background picture that looks the State form they are used to, so they can edit it in a manner similar to how they've always used pen-and-paper), and both that form and the report are called from separate buttons on the same overall form.

The form does display the correct record, but the report does not. The report seems to be simply displaying the first record in the table.

Here's the code for the form that looks like the report:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Edit_18_1_Click()
  2. On Error GoTo Err_Edit_18_1_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.  
  7.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  8.  
  9.     stDocName = "18-1"
  10.  
  11.     stLinkCriteria = "[18-1 Index]=" & Me![18-1 Index]
  12.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  13.  
  14. Exit_Edit_18_1_Click:
  15.     Exit Sub
  16.  
  17. Err_Edit_18_1_Click:
  18.     MsgBox Err.Description
  19.     Resume Exit_Edit_18_1_Click
  20.  
  21. End Sub
  22.  

and here's the code for the report:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Preview_This_18_1_Click()
  2. On Error GoTo Err_Preview_This_18_1_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.  
  7.     stLinkCriteria = "[18-1 Index]=" & Me![18-1 Index]
  8.  
  9.     stDocName = "18-1 State Form"
  10.     DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  11.     stDocName = "18-1 Continuation"
  12.     DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  13.  
  14. Exit_Preview_This_18_1_Click:
  15.     Exit Sub
  16.  
  17. Err_Preview_This_18_1_Click:
  18.     MsgBox Err.Description
  19.     Resume Exit_Preview_This_18_1_Click
  20.  
  21. End Sub
  22.  

It looks to me like this should be working, but it's not.
Apr 25 '11 #3
TheSmileyCoder
2,322 Expert Mod 2GB
That all looks correct to me.

My first suggestion as I have noticed your quite active and engaged in Access, is to read NeoPa's debugging articles in the Bytes Insights section. Good debugging skills are essential to any serious programmer.

Next suggestion would be to insert BEFORE line 10 and line 12 the line:
Expand|Select|Wrap|Line Numbers
  1. Msgbox "About to open report:" & stDocName & " with filter:" & stLinkCriteria
If that is still not working, check that the field [18-1 index] is part of your reports recordsource.
Apr 25 '11 #4
sueb
379 256MB
Thanks, SmileyCoder, I will certainly do both those things! I've always loved debugging; I just have no foundation in it at all in Access.
Apr 25 '11 #5
NeoPa
32,556 Expert Mod 16PB
There are a few possibilities I suppose Sue. We don't know about what RecordSources are used for the form and the report. Are they identical? Like Smiley, the code looks good to me, but neither of us can see what things are actually called in your database, so whereas the form may be bound to a RecordSource that includes a field called [18-1 Index], the report may not. It's mostly about having the relevant information available. This can be difficult to provide sometimes of course, as knowing what is relevant is not always easy unless you have the experience which would often mean there were no need to ask the question anyway.

Sometimes it can help to run the RecordSource query as a standalone query to see more clearly what is going on.

Let us know if you manage to sort this, or what else you need if not.

PS. The following indented comments may be of assistance :
One of the most popular (frequently occurring rather than best liked) problems we get is with SQL strings being manipulated in VBA code.

The reason this is so difficult is that all the work is being done at a level of redirection. What I mean by this is that the coder is never working directly with the SQL itself, but rather with code which in turn, is relied on to produce the SQL that they are envisaging is required. It's rather similar to the problems coders have historically had dealing with pointers.

Anyway, a technique I often suggest to coders struggling with this (at any level. This happens to experienced coders too.) is to use either the MsgBox() function, or Debug.Print into the Immediate Pane of the debugger window, to display the value of the SQL in the string before using it (That's assuming you're not good with debugging generally. Personally I would trace to the line, then display the value prior to allowing execution of the string - See Debugging in VBA). It's really much easier to appreciate what a SQL string is meant to do, and where there may be problems, when you can see it in its entirety, and in its true form, rather than as the code is about to create it.
Apr 26 '11 #6
sueb
379 256MB
Well, it's fixed, and the problem lay in an entirely different direction than how I presented it!

Because I thought the problem was with how I was calling the report, I told one of my users that I would print each page for her; I intended just to modify the query/VBA to directly point to the records that she wanted.

However, in trying to do this manually, I discovered that the links among the 4 tables were... well, just not correct. I guess the relationships were a little complicated, and I hadn't correctly defined the links and their properties.

Once I cleaned that up, it all worked great! Thanks for the thought you put into this. I'm still reading NeoPa's Debugging writeups (along with some others that I found scrolling through the Insights section--great stuff in there), so this has been, for me, an enormously useful exercise.
Apr 26 '11 #7
TheSmileyCoder
2,322 Expert Mod 2GB
Thats good to know. While it can be frustrating to spend hours on end getting nowhere, I try to remind myself that I did learn something, even if it came at the expense of some hair being pulled out at the roots.
Apr 26 '11 #8
NeoPa
32,556 Expert Mod 16PB
Very gratifying to hear Sue. You have a great attitude and it makes working with you so much easier :-)
Apr 27 '11 #9

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

Similar topics

9
by: LRW | last post by:
I'm not exactly sure how to even ask the question, and I know my terminology is not good as I'm a SQL beginner, but, here goes. I need to find a way to make an if statement within an array...or,...
1
by: Sean Dettrick | last post by:
Hi, I have several <vector>'s of the same length, with entries as follows: I= A= B= I want to use STL to make (I == 0) a mask to operate on the elements of A and B, i.e. I want to do this:
5
by: Rachel Weeden | last post by:
I'm working on an ASP Web application, and am having syntax issues in a WHERE statement I'm trying to write that uses the CInt Function on a field. Basically, I want to select records using...
2
by: Ellen Manning | last post by:
Using A2K. In my table I have the field "Grant" which can have a value or be null. I have a query that counts the number of records and has a Where clause on the Grant field. The query won't...
1
by: Tom Louchbaum | last post by:
Can someonw pls tell me how to reference the Where clause made by the VBA statement DoCmd.OPENREPORT "Report",acviewpreview,,where as a control within the Report itself? Thanks.
13
by: andro | last post by:
Hi everybody! I have several tables from which I want to exract the SAME value (along with other referenced data). All the values are in the same column within the tables. How can I achieve...
2
by: santoshsri | last post by:
Hello, Is there any by which I can write a SQL to get the 4th largest salary from a table without using "join" and without using "where" clause. Suppose the table is having two columns :...
1
by: itamar82 | last post by:
I am getting the following error: Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect syntax near the keyword 'WHERE'. for the sql below: SELECT TourId FROM (SELECT...
1
by: lyealain | last post by:
select * from cbomnew where (comCode= 'PCEC'or comCode='L0X'or comCode='CCEC') expert please help me on this... the comCode after WHERE statement are static(hardcoded).. PCEC,L0X,CCEC ... ...
2
by: dave jones | last post by:
Currently I have where zzz1234 is the encoded id number. If I take the "where" clause off, I get results. If I use the "where" clause, I receive an error>>"ODBC--call failed"
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: 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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
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...

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.