Printing one record on a report from a command button in a form

J

Jenny

Hi Klatuu

Many thanks for your help. I have followed your advice but am still stuck
on one thing (sorry if its v basic - I fear it is!) I can't figure out what
I am supposed to type where you have written "Me.txtOne" and "Me.txtTwo".
Obviously it's not the field value, if I type the field name here it still
returns the report for all records.

Please help!
Thank you

Klatuu said:
The first problem is you have a syntax error. The Where condition is the 4th
argument, not the 3rd.
DoCmd.OpenReport strDocName, acPreview, strWhere
Should be
DoCmd.OpenReport strDocName, acPreview, ,strWhere

If you want to filter on two fields, then you need to specify both in strWhere
strWhere = "[FirstField] = '" & Me.txtOne & "' And [NextField] = '" &
Me.txtTwo & "'"

Jenny said:
Hello

I am using the following code to try to get a report to show a single record
by clicking a command button in a sub form (perhaps it doesnt work because
its a sub form not a form?) The mainform holds the primary key and subform
displays the data (I also have 2 other sub forms nested in the subform!)

My other complication is that the primary key is a combination of 2 fields,
however, before I even get to this part, when I enter a different field name
in the RunID part of the code which is unique, the report previews, but for
all my records.

So I have 2 problems I guess
1 - Why is it showing all records and not just one?
2 - is there a code that I can borrow for when the RunID needs to be a
primary key made up of 2 fields?

Thanks a lot scripting dudes


Dim strDocName As String
Dim strWhere As String
strDocName = "myreport"
strWhere = "[RunID]=" & Me!RunID
DoCmd.OpenReport strDocName, acPreview, strWhere
 
J

Jenny

Thanks have solved it now!
Happy Friday

Jenny said:
Hi Klatuu

Many thanks for your help. I have followed your advice but am still stuck
on one thing (sorry if its v basic - I fear it is!) I can't figure out what
I am supposed to type where you have written "Me.txtOne" and "Me.txtTwo".
Obviously it's not the field value, if I type the field name here it still
returns the report for all records.

Please help!
Thank you

Klatuu said:
The first problem is you have a syntax error. The Where condition is the 4th
argument, not the 3rd.
DoCmd.OpenReport strDocName, acPreview, strWhere
Should be
DoCmd.OpenReport strDocName, acPreview, ,strWhere

If you want to filter on two fields, then you need to specify both in strWhere
strWhere = "[FirstField] = '" & Me.txtOne & "' And [NextField] = '" &
Me.txtTwo & "'"

Jenny said:
Hello

I am using the following code to try to get a report to show a single record
by clicking a command button in a sub form (perhaps it doesnt work because
its a sub form not a form?) The mainform holds the primary key and subform
displays the data (I also have 2 other sub forms nested in the subform!)

My other complication is that the primary key is a combination of 2 fields,
however, before I even get to this part, when I enter a different field name
in the RunID part of the code which is unique, the report previews, but for
all my records.

So I have 2 problems I guess
1 - Why is it showing all records and not just one?
2 - is there a code that I can borrow for when the RunID needs to be a
primary key made up of 2 fields?

Thanks a lot scripting dudes


Dim strDocName As String
Dim strWhere As String
strDocName = "myreport"
strWhere = "[RunID]=" & Me!RunID
DoCmd.OpenReport strDocName, acPreview, strWhere
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top