A form's onload is cancelling a openreport's where condition (1 Viewer)

gojets1721

Registered User.
Local time
Yesterday, 20:09
Joined
Jun 11, 2019
Messages
429
Bit of an odd question........so I have DB full of complaints dating back years. My main form which looks at one complaint at a time has onload code (see below) so that it automatically only opens complaints from 2022.

Elsewhere in the DB, I have a button that I want to open the form using different criteria and I'm using a where condition to do that (see below code). But since I also have onload code, it (understandably) ignores the where condition and opens the form using the onload code.

Before I change things around, I wanted to see if there was an way around this. Probably not, as access is behaving how it should but I thought I'd at least check.

Thanks!

Onload code:
Code:
Private Sub Form_Load()
    Me.Filter = "YEAR([ComplaintDate])= 2022"
    DoCmd.SetOrderBy "[ComplaintDate] ASC"
    Me.FilterOn = True
End Sub

Where condition code from openform button:
Code:
Private Sub txtInProgressComplaints_Click()

    DoCmd.Close acForm, "frmLaunch"
    DoCmd.OpenForm "frmComplaints", acNormal, "", "Status = 'In Progress'", , acNormal

End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 20:09
Joined
Aug 30, 2003
Messages
36,125
I'd guess it's not really ignoring the wherecondition so much as replacing it. One thought that comes to mind is sending something in OpenArgs and test for that in your load code. If it's present, abort, if not filter.
 

LarryE

Active member
Local time
Yesterday, 20:09
Joined
Aug 18, 2021
Messages
589
Bit of an odd question........so I have DB full of complaints dating back years. My main form which looks at one complaint at a time has onload code (see below) so that it automatically only opens complaints from 2022.

Elsewhere in the DB, I have a button that I want to open the form using different criteria and I'm using a where condition to do that (see below code). But since I also have onload code, it (understandably) ignores the where condition and opens the form using the onload code.

Before I change things around, I wanted to see if there was an way around this. Probably not, as access is behaving how it should but I thought I'd at least check.

Thanks!

Onload code:
Code:
Private Sub Form_Load()
    Me.Filter = "YEAR([ComplaintDate])= 2022"
    DoCmd.SetOrderBy "[ComplaintDate] ASC"
    Me.FilterOn = True
End Sub

Where condition code from openform button:
Code:
Private Sub txtInProgressComplaints_Click()

    DoCmd.Close acForm, "frmLaunch"
    DoCmd.OpenForm "frmComplaints", acNormal, "", "Status = 'In Progress'", , acNormal

End Sub
You can try the following which allows two filtering options when the OnLoad event fires:
Code:
Private Sub Form_Load()
Dim Reply As Variant
Reply = MsgBox("DO YOU WANT TO SHOW IN-PROGRESS?", vbYesNo)
If Reply = 7 Then 'NO
    Me.Filter = "YEAR([ComplaintDate])= 2022"
    DoCmd.SetOrderBy "[ComplaintDate] ASC"
    Me.FilterOn = True
End If
If Reply = 6 Then 'Yes
    Me.Filter = "[Status]='In Progress'"
    Me.FilterOn = True
End If
Me.Requery
Exit Sub
End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom