Command button to individual rpt by record??

D

DrewBe

I have a report for all records in a table. I input and review data in form
view, but when I am ready to print the report of the record which is
displayed in form view I must close form enter report and search for the
record I once had displayed.

I am trying to build a command button (in form view) to print ONLY the
record associated to what is displayed, and not everything that is with in
the report. Without a parameter how might I ask the command button to pull
the record in rpt view of ONLY the record that is displayed in frm view???
 
K

Klatuu

Use the Where argument of the OpenReport method to filter your report to the
current record. Assuming the primary key field of the table is EmployeeID
and the control on your form is txtEmpID, it would be:
Docmd.OpenReport "MyReportName", , ,"[EmployeeID] = " & Me.txtEmpID
(assumes EmployeeID is a numeric field. Add the correct delimiters if it is
text)
 
F

fredg

DrewBe said:
I have a report for all records in a table. I input and review data in form
view, but when I am ready to print the report of the record which is
displayed in form view I must close form enter report and search for the
record I once had displayed.

I am trying to build a command button (in form view) to print ONLY the
record associated to what is displayed, and not everything that is with in
the report. Without a parameter how might I ask the command button to pull
the record in rpt view of ONLY the record that is displayed in frm view???

Your table should have a unique prime key field.
In my example it is named [RecordID].
Change [RecordID] below to whatever your actual field name is.

In the command button's Click event write:

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]

The above assumes a [RecordID] field that is a Number Datatype.

If, however, [RecordID] is Text Datatype, then use:


DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

as the Where clause.
For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "
Change [RecordID] to whatever the actual field name is that you are
using.

See VBA Help files for:
OpenReport method .... and ....
Where Clause + Restrict data to a subset of records'
 
D

DrewBe

Fredg... I have done what you've suggested... It is now sending error, asking
for the Macro that is suggested by the "DoCmd"? What now?
--
DrewBe


fredg said:
DrewBe said:
I have a report for all records in a table. I input and review data in form
view, but when I am ready to print the report of the record which is
displayed in form view I must close form enter report and search for the
record I once had displayed.

I am trying to build a command button (in form view) to print ONLY the
record associated to what is displayed, and not everything that is with in
the report. Without a parameter how might I ask the command button to pull
the record in rpt view of ONLY the record that is displayed in frm view???

Your table should have a unique prime key field.
In my example it is named [RecordID].
Change [RecordID] below to whatever your actual field name is.

In the command button's Click event write:

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]

The above assumes a [RecordID] field that is a Number Datatype.

If, however, [RecordID] is Text Datatype, then use:


DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

as the Where clause.
For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "
Change [RecordID] to whatever the actual field name is that you are
using.

See VBA Help files for:
OpenReport method .... and ....
Where Clause + Restrict data to a subset of records'
 
F

fredg

Fredg... I have done what you've suggested... It is now sending error, asking
for the Macro that is suggested by the "DoCmd"? What now?

It's customary, when replying to a message, to include the relevant
portion of any previous message so that a reader need not attempt to
find the message that this one refers to.

Usually, it's because the DoCmd was written directly on the click
Event Line, rather than in the click Event code module.

Here is how to write code.

Open the Form in Design View.
Select the Command Button.
Display the Command Button's Property sheet.
Click on the Event Tab.
On the line that says On Click, write:
[Event Procedure]
Then click on the little button with the 3 dots that appears on that
line.
When the code window opens, the cursor will be flashing between 2
already existing lines of code.
Between those 2 lines, write:

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]


The above assumes [RecordID] is a Number datatype.
Save the changes.
 
D

DrewBe

Sorry Fredg... Thanks for the tips... This is all very new to me... Young
student trying to learn some in's and out's!!! But... I did just what you'd
recommended in VBA, but now it is having a hard time reading through the &.
It gets hung up there. The only thing I've done different is changed the
sources. Oh by the way I'm using your text box recommendation. Which is as
follows...

Your Script:
DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

My Script:
DoCmd.OpenReport "rpt__SeymourDistrict_Entire_Lat/Long", acViewPreview,
,"[Large Culvert #] = '" &
[Large Culvert #] & "'"


HELP!!!
--
DrewBe


fredg said:
Fredg... I have done what you've suggested... It is now sending error, asking
for the Macro that is suggested by the "DoCmd"? What now?

It's customary, when replying to a message, to include the relevant
portion of any previous message so that a reader need not attempt to
find the message that this one refers to.

Usually, it's because the DoCmd was written directly on the click
Event Line, rather than in the click Event code module.

Here is how to write code.

Open the Form in Design View.
Select the Command Button.
Display the Command Button's Property sheet.
Click on the Event Tab.
On the line that says On Click, write:
[Event Procedure]
Then click on the little button with the 3 dots that appears on that
line.
When the code window opens, the cursor will be flashing between 2
already existing lines of code.
Between those 2 lines, write:

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]


The above assumes [RecordID] is a Number datatype.
Save the changes.
 
F

fredg

Sorry Fredg... Thanks for the tips... This is all very new to me... Young
student trying to learn some in's and out's!!! But... I did just what you'd
recommended in VBA, but now it is having a hard time reading through the &.
It gets hung up there. The only thing I've done different is changed the
sources. Oh by the way I'm using your text box recommendation. Which is as
follows...

Your Script:
DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

My Script:
DoCmd.OpenReport "rpt__SeymourDistrict_Entire_Lat/Long", acViewPreview,
,"[Large Culvert #] = '" &
[Large Culvert #] & "'"

HELP!!!

This should all be on one line:
DoCmd.OpenReport "rpt__SeymourDistrict_Entire_Lat/Long",
acViewPreview, ,"[Large Culvert #] = '" & [Large Culvert #] & "'"

If you wish to 'break' a line for clarity, you must use the
space underscore combination, i.e.

DoCmd.OpenReport "MyReportName", _
acViewPreview, , "Large Culvert #] ='" _
& etc..... "'"
 
D

DrewBe

Fredg,

Hate to bother you, but you seem to be helping alot of people with alot of
issues... Can you try another for me?

I would like to refine my abilities to search within the records with a
command button in form view. I need it to only search in the PRIMARY KEY
field. not in the field which the cursor is in...

Thanks again for your advise on the Command button... It is truly a time
saver!!!
--
DrewBe

--
DrewBe


fredg said:
Sorry Fredg... Thanks for the tips... This is all very new to me... Young
student trying to learn some in's and out's!!! But... I did just what you'd
recommended in VBA, but now it is having a hard time reading through the &.
It gets hung up there. The only thing I've done different is changed the
sources. Oh by the way I'm using your text box recommendation. Which is as
follows...

Your Script:
DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

My Script:
DoCmd.OpenReport "rpt__SeymourDistrict_Entire_Lat/Long", acViewPreview,
,"[Large Culvert #] = '" &
[Large Culvert #] & "'"

HELP!!!

This should all be on one line:
DoCmd.OpenReport "rpt__SeymourDistrict_Entire_Lat/Long",
acViewPreview, ,"[Large Culvert #] = '" & [Large Culvert #] & "'"

If you wish to 'break' a line for clarity, you must use the
space underscore combination, i.e.

DoCmd.OpenReport "MyReportName", _
acViewPreview, , "Large Culvert #] ='" _
& etc..... "'"
 
F

fredg

Fredg,

Hate to bother you, but you seem to be helping alot of people with alot of
issues... Can you try another for me?

I would like to refine my abilities to search within the records with a
command button in form view. I need it to only search in the PRIMARY KEY
field. not in the field which the cursor is in...

Thanks again for your advise on the Command button... It is truly a time
saver!!!

You have already posted this new question in another new thread. As
long as this question is NOT related to this thread, I'll consider
this thread completed.
 

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