Docomd.OpenReport Opens Wrong Report (Sometimes) (1 Viewer)

TheSearcher

Registered User.
Joined
Jul 21, 2011
Messages
314
I have a command button that, when clicked, opens a report. I feed it a unique Trans_Id so that it opens the report with the data that pertains to the form that the user is viewing. The first time I open the report and Trans_Id = 14 it opens the correct report. If I change the Trans_Id to 15 it will sometimes open the 14 report instead (not always). What could be causing the first variable to get stuck? Here is my code:
Code:
Private Sub cmd_ViewNote_Click()

Globals.glb_TransID = Me.txt_TransID
DoCmd.OpenReport "N_Note", acViewPreview, , "Trans_ID = " & Globals.glb_TransID, acWindowNormal
Globals.glb_TransID = 0

End Sub

I, at first, used the Me.txt_TransID instead of the global variable in the DoCmd. Then I changed it to the global when this issue started happening but it didn't seem to have an effect.
Any help will be greatly appreciated.

Thanks in advance,
TS
 
Try to do a MsgBox first to view the value of your variable (TransID) before opening the report, just to make sure it's getting what you're expecting it to get and then see if the report still shows a different one.
 
Are you closing the report each time. I am not confident on this, but I believe if the report is open already and you do another docmd.openreport it just returns the focus to that report and does not reapply the new filter. Make sure it closes first.

Code:
Private Sub cmd_ViewNote_Click()

if me.currentProject.allreports("N_Note").isloaded then
  docmd.close acreport,"N_Note"
end if 
  DoCmd.OpenReport "N_Note", acViewPreview, , "Trans_ID = " & me.txt_transID, acWindowNormal
End Sub
 
MajP - I had to change your code to what's below. It seems to have solved my problem!
Thanks to both of you for helping me out!!!

Code:
If Application.CurrentProject.AllReports("N_Note").IsLoaded Then
  DoCmd.Close acReport, "N_Note"
End If
 
Oops. That was a mistake. Good catch.
 
If the report is already closed, calling close again does nothing, so you don't need the If block....
Code:
Private Const RN as String = "N_Note"

Private Sub cmd_ViewNote_Click()
    DoCmd.Close acReport, RN
    DoCmd.OpenReport RN, acViewPreview, , "Trans_ID = " & me.txt_transID, acWindowNormal
End Sub
 

Users who are viewing this thread

Top Bottom