why open report based on filter not working

siamandm
BronzeLounger
Posts: 1211
Joined: 01 May 2016, 09:58

why open report based on filter not working

Post by siamandm »

Hello All,

i have a report which it's data source is a select query
and I have a form with a combo box the data source is a table employee, I have chosen id and employeename

there is a button with a vba code in the on click event like below :
but it always bring the first employee and ignore the combo box selection

Code: Select all

Private Sub cmdPrtRpt_Click()
        
        
                Dim strWhere As String
                        
                If Not IsNull(Me.cboEmployeeFullName) Then
                stWhere = EmployeeFullName = Me.cboEmployeeFullName.Column(1)
                End If
                
        DoCmd.OpenReport REPORTNAME:="rptEmployee", view:=acViewReport, WhereCondition:=strWhere

End Sub

User avatar
HansV
Administrator
Posts: 78419
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: why open report based on filter not working

Post by HansV »

The where-condition must be a string:

Code: Select all

                stWhere = "EmployeeFullName = '" & Me.cboEmployeeFullName.Column(1) & "'"
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1211
Joined: 01 May 2016, 09:58

Re: why open report based on filter not working

Post by siamandm »

thank you very much for the quick reply,
still didn't work, I believe I have missed something in the report or in the query,
I have a simple select query without any criteria, and the report which gets data from it also does not have any criteria
any suggestions, please?

regards

User avatar
HansV
Administrator
Posts: 78419
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: why open report based on filter not working

Post by HansV »

Me.cboEmployeeFullName.Column(1) is the second column of the combo box. What is in that column?
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1211
Joined: 01 May 2016, 09:58

Re: why open report based on filter not working

Post by siamandm »

its employee full name
kindly see the attached database,
the form frmReport is the one which I want to use and rptEmployee ,

Regards
Last edited by siamandm on 06 Jun 2021, 16:09, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78419
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: why open report based on filter not working

Post by HansV »

It's a typo: you use stWhere instead of strWhere.
This could have been avoided if you had had Option Explicit at the top of the module.

Code: Select all

Private Sub cmdPrtRpt_Click()
    Dim strWhere As String
    If Not IsNull(Me.cboEmployeeFullName) Then
        strWhere = "EmpID = " & Me.cboEmployeeFullName
    End If
    DoCmd.OpenReport ReportName:="rptEmployee", View:=acViewReport, WhereCondition:=strWhere
End Sub
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1211
Joined: 01 May 2016, 09:58

Re: why open report based on filter not working

Post by siamandm »

thank you very much for your correction,

how to have Option Explicit at the top of the module, please?

Regards

User avatar
StuartR
Administrator
Posts: 12602
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: why open report based on filter not working

Post by StuartR »

siamandm wrote:
06 Jun 2021, 16:09
how to have Option Explicit at the top of the module, please?
You literally type
Option Exliciit
on a line by itself as the very first line in the module, before any other content.
StuartR


User avatar
HansV
Administrator
Posts: 78419
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: why open report based on filter not working

Post by HansV »

Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1211
Joined: 01 May 2016, 09:58

Re: why open report based on filter not working

Post by siamandm »

thank you very much for the informative link