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

TheSearcher

Registered User.
Local time
Yesterday, 20:05
Joined
Jul 21, 2011
Messages
304
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
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:05
Joined
Oct 29, 2018
Messages
21,467
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.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:05
Joined
May 21, 2018
Messages
8,527
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
 

TheSearcher

Registered User.
Local time
Yesterday, 20:05
Joined
Jul 21, 2011
Messages
304
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
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:05
Joined
May 21, 2018
Messages
8,527
Oops. That was a mistake. Good catch.
 

MarkK

bit cruncher
Local time
Yesterday, 17:05
Joined
Mar 17, 2004
Messages
8,181
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