Buttons in a form

V

Vicky

I have a button in my form that opens a report and that works, what i would
like to do now is open a specific report by the job number on the form. Can
this be done? if so how?
 
R

Rick Brandt

Vicky said:
I have a button in my form that opens a report and that works, what i
would like to do now is open a specific report by the job number on
the form. Can this be done? if so how?

Look at the existing code behind your button. You will almost certainly
find that it is using the OpenReport method of the DoCmd object. That
method has an optional argument for supplying a WHERE clause which will
filter the report being opened. The string you can place in that argument
would have the same syntax as a valid WHERE clause in a query only without
the word "WHERE" at the beginning.

All you need to do is insert an expression there that will create a WHERE
clause that filters on the job number currenlty displayed on the form...

DoCmd.OpenReport "ReportName", acViewPreview,, "[job number] = " & Me![job
number]

The above assumes [job number] is a numeric field. If it is a text field
then you would need quotes around the value like this...

DoCmd.OpenReport "ReportName", acViewPreview,, "[job number] = '" & Me![job
number] & "'"
 
V

Vicky

I can't get this to work what am i doing wrong? I put in the string that you
gave me, changing the relevant fields the report opens but i still get all of
them.

Rick Brandt said:
Vicky said:
I have a button in my form that opens a report and that works, what i
would like to do now is open a specific report by the job number on
the form. Can this be done? if so how?

Look at the existing code behind your button. You will almost certainly
find that it is using the OpenReport method of the DoCmd object. That
method has an optional argument for supplying a WHERE clause which will
filter the report being opened. The string you can place in that argument
would have the same syntax as a valid WHERE clause in a query only without
the word "WHERE" at the beginning.

All you need to do is insert an expression there that will create a WHERE
clause that filters on the job number currenlty displayed on the form...

DoCmd.OpenReport "ReportName", acViewPreview,, "[job number] = " & Me![job
number]

The above assumes [job number] is a numeric field. If it is a text field
then you would need quotes around the value like this...

DoCmd.OpenReport "ReportName", acViewPreview,, "[job number] = '" & Me![job
number] & "'"
 
V

Vicky

Private Sub CmdRepair_Click()
On Error GoTo Err_CmdRepair_Click

Dim stDocName As String

stDocName = "rptGQAD106"
DoCmd.OpenReport "rptGQAD106", acPreview

DoCmd.OpenReport "rptGQAD106", acViewPreview, , "[GRN Ref]=" & Me![GRN
Ref]

Exit_CmdRepair_Click:
Exit Sub

Err_CmdRepair_Click:
MsgBox Err.Description
Resume Exit_CmdRepair_Click

End Sub
 
R

Rick Brandt

Vicky said:
Private Sub CmdRepair_Click()
On Error GoTo Err_CmdRepair_Click

Dim stDocName As String

stDocName = "rptGQAD106"
DoCmd.OpenReport "rptGQAD106", acPreview

DoCmd.OpenReport "rptGQAD106", acViewPreview, , "[GRN Ref]=" &
Me![GRN Ref]

Exit_CmdRepair_Click:
Exit Sub

Err_CmdRepair_Click:
MsgBox Err.Description
Resume Exit_CmdRepair_Click

End Sub

You are opening the report twice and the second instance will NOT change the
filter settings applied by the first one which has no filter applied. If
you just delete the first OpenReport line it might work with what you have.
 
Top