By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,027 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,027 IT Pros & Developers. It's quick & easy.

Event Code

P: n/a
I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table. The
Second Field is "BeginDate." The Third field is "EndDate."

I then have a command button. I want to put a code in this buttons on click
event. The code should open up a report called "EmpRpt."

The report will have the three fields just like the form. I want those three
fields to appear with the data the user entered in the form. I would also
like this report to open in print preview.

Can someone please help me with a code? Thank you!

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200708/1

Aug 9 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
See:
Print the record in the form
at:
http://allenbrowne.com/casu-15.html

Note that your table must have a primary key so you can be certain the
report has the same record.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"ladybug via AccessMonster.com" <u21071@uwewrote in message
news:767016588cee4@uwe...
>I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table.
The
Second Field is "BeginDate." The Third field is "EndDate."

I then have a command button. I want to put a code in this buttons on
click
event. The code should open up a report called "EmpRpt."

The report will have the three fields just like the form. I want those
three
fields to appear with the data the user entered in the form. I would also
like this report to open in print preview.

Can someone please help me with a code? Thank you!
Aug 9 '07 #2

P: n/a
ladybug via AccessMonster.com wrote:
I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table. The
Second Field is "BeginDate." The Third field is "EndDate."

I then have a command button. I want to put a code in this buttons on click
event. The code should open up a report called "EmpRpt."

The report will have the three fields just like the form. I want those three
fields to appear with the data the user entered in the form. I would also
like this report to open in print preview.

Can someone please help me with a code? Thank you!
You could create a query called EmpRpt. Add the 2 tables (since the
combo is linked) you use and drag down the fields you want from the 2
tables. Save the query.

Go to Reports, click New, and use the ReportWizard. Select the query
EmpRpt. Follow the steps to create the report. Save it as EmpRpt.

Now in your command button you open the property sheet to the event tab
and select the OnCLick event. You could enter
Docmd.OpenReport "EmpRpt",acViewPreview

You can filter it as well. Lets say you have a field called EmpID and
it is numeric. You could enter
Docmd.OpenReport "EmpRpt",acViewPreview,,"EmpID = " & Me.chrUserID
and this will display the report for the current empid.

Now it's possible you might open a report where all of the data has not
been saved. If this were a new record, the report might even say no
data exists! So sometimes you might want to save the record first
before displaying a report. Your code might look like this
If Me.Dirty then Me.Dirty = False
Docmd.OpenReport...
or
If Me.Dirty then DoCmd.RunCommand acCmdSaveRecord
Docmd.OpenReport...
Aug 9 '07 #3

P: n/a
I did what you gave me and I rcvd an error that I have entered an expression
that has an invalid reference to the property dirty. I took out that section
of the code and it opens the report, but no data is captured. Here is the
code I have:

Private Sub Command6_Click()
Dim strWhere As String

If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[chrUserId] = " & Me.[chrUserID]
DoCmd.OpenReport "EmpRpt", acViewPreview, , strWhere
End If
End Sub
Allen Browne wrote:
>See:
Print the record in the form
at:
http://allenbrowne.com/casu-15.html

Note that your table must have a primary key so you can be certain the
report has the same record.
>>I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table.
[quoted text clipped - 11 lines]
>>
Can someone please help me with a code? Thank you!
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200708/1

Aug 9 '07 #4

P: n/a
What two tables? There is no table for the dates they are just text fields.
Salad wrote:
>I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table. The
[quoted text clipped - 8 lines]
>>
Can someone please help me with a code? Thank you!

You could create a query called EmpRpt. Add the 2 tables (since the
combo is linked) you use and drag down the fields you want from the 2
tables. Save the query.

Go to Reports, click New, and use the ReportWizard. Select the query
EmpRpt. Follow the steps to create the report. Save it as EmpRpt.

Now in your command button you open the property sheet to the event tab
and select the OnCLick event. You could enter
Docmd.OpenReport "EmpRpt",acViewPreview

You can filter it as well. Lets say you have a field called EmpID and
it is numeric. You could enter
Docmd.OpenReport "EmpRpt",acViewPreview,,"EmpID = " & Me.chrUserID
and this will display the report for the current empid.

Now it's possible you might open a report where all of the data has not
been saved. If this were a new record, the report might even say no
data exists! So sometimes you might want to save the record first
before displaying a report. Your code might look like this
If Me.Dirty then Me.Dirty = False
Docmd.OpenReport...
or
If Me.Dirty then DoCmd.RunCommand acCmdSaveRecord
Docmd.OpenReport...
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200708/1

Aug 9 '07 #5

P: n/a
If the form does not have a Dirty property, then it is an unbound form.

If it is an unbound form, then there is no current record to print.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"ladybug via AccessMonster.com" <u21071@uwewrote in message
news:7670a6cdcc48f@uwe...
>I did what you gave me and I rcvd an error that I have entered an
expression
that has an invalid reference to the property dirty. I took out that
section
of the code and it opens the report, but no data is captured. Here is the
code I have:

Private Sub Command6_Click()
Dim strWhere As String

If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[chrUserId] = " & Me.[chrUserID]
DoCmd.OpenReport "EmpRpt", acViewPreview, , strWhere
End If
End Sub
Allen Browne wrote:
>>See:
Print the record in the form
at:
http://allenbrowne.com/casu-15.html

Note that your table must have a primary key so you can be certain the
report has the same record.
>>>I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table.
[quoted text clipped - 11 lines]
>>>
Can someone please help me with a code? Thank you!
Aug 9 '07 #6

P: n/a
ladybug via AccessMonster.com wrote:
What two tables? There is no table for the dates they are just text fields.
You stated "First field is "chrUserID," this is a combo box with a row
source from another table..."

So I figured 2 tables were in play. Sometimes combos can have multiple
columns and I figured the data in the combo might be used as well as the
other fields.
>
Salad wrote:
>>>I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table. The

[quoted text clipped - 8 lines]
>>>Can someone please help me with a code? Thank you!

You could create a query called EmpRpt. Add the 2 tables (since the
combo is linked) you use and drag down the fields you want from the 2
tables. Save the query.

Go to Reports, click New, and use the ReportWizard. Select the query
EmpRpt. Follow the steps to create the report. Save it as EmpRpt.

Now in your command button you open the property sheet to the event tab
and select the OnCLick event. You could enter
Docmd.OpenReport "EmpRpt",acViewPreview

You can filter it as well. Lets say you have a field called EmpID and
it is numeric. You could enter
Docmd.OpenReport "EmpRpt",acViewPreview,,"EmpID = " & Me.chrUserID
and this will display the report for the current empid.

Now it's possible you might open a report where all of the data has not
been saved. If this were a new record, the report might even say no
data exists! So sometimes you might want to save the record first
before displaying a report. Your code might look like this
If Me.Dirty then Me.Dirty = False
Docmd.OpenReport...
or
If Me.Dirty then DoCmd.RunCommand acCmdSaveRecord
Docmd.OpenReport...

Aug 9 '07 #7

P: n/a
So why do I get the error for the Dirty property?

Allen Browne wrote:
>If the form does not have a Dirty property, then it is an unbound form.

If it is an unbound form, then there is no current record to print.
>>I did what you gave me and I rcvd an error that I have entered an
expression
[quoted text clipped - 27 lines]
>>>>
Can someone please help me with a code? Thank you!
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200708/1

Aug 9 '07 #8

P: n/a
The dates do not come from a table. This is just a form which sets the
criteria that will be used when opening the report.

Salad wrote:
>What two tables? There is no table for the dates they are just text fields.

You stated "First field is "chrUserID," this is a combo box with a row
source from another table..."

So I figured 2 tables were in play. Sometimes combos can have multiple
columns and I figured the data in the combo might be used as well as the
other fields.
>>>>I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table. The
[quoted text clipped - 28 lines]
>>> If Me.Dirty then DoCmd.RunCommand acCmdSaveRecord
Docmd.OpenReport...
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200708/1

Aug 9 '07 #9

P: n/a
If the error is as you said - invalid reference to the property dirty - it
is because your form is unbound.

(If the error said you can't set the property, it's because the record
cannot be saved.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"ladybug via AccessMonster.com" <u21071@uwewrote in message
news:7671418c98553@uwe...
So why do I get the error for the Dirty property?

Allen Browne wrote:
>>If the form does not have a Dirty property, then it is an unbound form.

If it is an unbound form, then there is no current record to print.
>>>I did what you gave me and I rcvd an error that I have entered an
expression
[quoted text clipped - 27 lines]
>>>>>
Can someone please help me with a code? Thank you!
Aug 9 '07 #10

P: n/a
ladybug via AccessMonster.com wrote:
The dates do not come from a table. This is just a form which sets the
criteria that will be used when opening the report.
What help do you need?

Do you need help in creating a report?

Do you need help calling the report?

What do you want to print?

Allen's doing his best to help, I'm confused with your requirements.
You might want to tell us what you have, what you need, whether you are
using a table as the form's recordsource or if there is no recordsource.
>
Salad wrote:
>>>What two tables? There is no table for the dates they are just text fields.

You stated "First field is "chrUserID," this is a combo box with a row
source from another table..."

So I figured 2 tables were in play. Sometimes combos can have multiple
columns and I figured the data in the combo might be used as well as the
other fields.

>>>>>I have a form called "EmpRpt". It has three fields. First field is
>"chrUserID," this is a combo box with a row source from another table. The

[quoted text clipped - 28 lines]
>>>> If Me.Dirty then DoCmd.RunCommand acCmdSaveRecord
Docmd.OpenReport...

Aug 9 '07 #11

P: n/a
"ladybug via AccessMonster.com" <u21071@uwewrote in
news:76714e6932a13@uwe:
The dates do not come from a table. This is just a form which
sets the criteria that will be used when opening the report.
Your statement that the form has three fields is what leads
people astray. Forms do NOT have fields. Tables have fields.
The process of binding a form to a table or query means that the
fields appear in the textboxes. Had you said there are three
textboxes on the form, you would have been clear.

As to your problem of the report not opening, your data talks of
chrUserID. is this really a text field, or an ID Number field?
If it's a text field, you need to modify the way you pass the
info to the form
strWhere = "[chrUserId] = " & Me.[chrUserID] will work for a
numeric field.
strWhere = "[chrUserId] = '" & Me.[chrUserID] & "'" is
required for a text type ID.
>
Salad wrote:
>>What two tables? There is no table for the dates they are
just text fields.

You stated "First field is "chrUserID," this is a combo box
with a row source from another table..."

So I figured 2 tables were in play. Sometimes combos can have
multiple columns and I figured the data in the combo might be
used as well as the other fields.
>>>>>I have a form called "EmpRpt". It has three fields. First
>field is "chrUserID," this is a combo box with a row source
>from another table. The
[quoted text clipped - 28 lines]
>>> If Me.Dirty then DoCmd.RunCommand acCmdSaveRecord
Docmd.OpenReport...


--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Aug 9 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.