繁体   English   中英

使用表单过滤 - MS ACCESS

[英]Filtering using a Form - MS ACCESS

我希望能够使用表单过滤数据库。 我用不同的下拉列表开发了表单:

在此处输入图像描述

单击过滤器按钮时且仅当未确定此值(标记为红色)时

在此处输入图像描述

我收到以下错误:

在此处输入图像描述

用于过滤的代码如下:

Private Sub btnFilter_Click()
'---------------------------------------------------------------------------------------------------'
'
' When Button "Fitler" is Clicked
'---------------------------------------------------------------------------------------------------'
    Dim sSQL As String
    Dim rst As DAO.Recordset

On Error GoTo Fin
    
    sSQL = "SELECT * FROM R_Select_Emploi"
    sSQL = sSQL & vbCrLf & "WHERE (EMPLOI_entreprise_id) =1"

    'We create the SQL  query with different drop down lists shown before that can or cannot be selected

    If Not IsNull(comboBassin) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_bassin_id) =" & Me.comboBassin.Column(1)
    End If
    If Not IsNull(comboOrga2) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_orga2_id) =" & Me.comboOrga2.Column(1)
    End If
    If Not IsNull(comboOrga3) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_orga3_id) =" & Me.comboOrga3.Column(1)
    End If
    If Not IsNull(comboOrga4) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_orga4_id) =" & Me.comboOrga4.Column(1)
    End If
    If Not IsNull(comboOrga5) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_orga5_id) =" & Me.comboOrga5.Column(1)
    End If
    If Not IsNull(comboOrga6) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_orga6_id) =" & Me.comboOrga6.Column(1)
    End If
    If Not IsNull(comboEtab) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_etablissement_id) =" & Me.comboEtab.Column(1)
    End If
    If Not IsNull(comboEmploi) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_code_id) =" & Me.comboEmploi.Column(1)
    End If
    If Not IsNull(comboManagement) And comboManagement.Column(0) Like "Masquer*" Then
        sSQL = sSQL & vbCrLf & "And (CODE_EMPLOI_management) =False"
    ElseIf Not IsNull(comboManagement) And comboManagement.Column(0) Like "Afficher*" Then
        sSQL = sSQL & vbCrLf & "And (CODE_EMPLOI_management) =True"
    End If
    If Not IsNull(comboInstance) Then
        sSQL = sSQL & vbCrLf & "And (PILOTAGE_EMPLOI_instance_id) =" & Me.comboInstance.Column(1)
    End If
    If Not IsNull(comboPE) Then
        sSQL = sSQL & vbCrLf & "And (EMPLOI_peiv_id) =" & Me.comboPE.Column(1)
    End If
' Here is the code for the row causing a problem it is no different than the others bu for some resons it's causing it. 
'Can it be because of that -----------> (Look further below please)
    If Not IsNull(comboPO) Then
        sSQL = sSQL & vbCrLf & "And (CODE_EMPLOI_categorie_emploi_id) =" & Me.comboPO.Column(0)
    End If
    If Not IsNull(comboMouvement) And comboMouvement.Column(0) Like "*mouvement*" Then
        sSQL = sSQL & vbCrLf & "And ([Mouvement?]) =True"
    ElseIf Not IsNull(comboMouvement) And comboMouvement.Column(0) Like "*créations*" Then
        sSQL = sSQL & vbCrLf & "And ([Création?]) =True"
    ElseIf Not IsNull(comboMouvement) And comboMouvement.Column(0) Like "*successions*" Then
        sSQL = sSQL & vbCrLf & "And ([Succession?]) =True"
    End If
    If Not IsNull(comboRecrutement) And comboRecrutement.Column(0) Like "*recrutements*" Then
        sSQL = sSQL & vbCrLf & "And ([Recrutement?]) =True"
    End If
    If Not IsNull(comboEtatCoMob) Then
        sSQL = sSQL & vbCrLf & "And (PILOTAGE_EMPLOI_etat_emploi_id) =" & Me.comboEtatCoMob.Column(1)
    End If
    If Not IsNull(comboFiche) Then
        sSQL = sSQL & vbCrLf & "And (PILOTAGE_EMPLOI_statut_fiche_id) =" & Me.comboFiche.Column(1)
    End If
    
    'Here is some automatic filters depending on who is filtering and what is he autorized to see
' This authorizations should be added to what he selected previously 
'if the person didn't select anything then these filters are the only thing that should be applied regarding that line
' -------> Here : 
    If bNonContract _
            Or bInfPO6 _
            Or bPO6etPO7 _
            Or bSupPO6 _
            Then
        'We create the SQL query depending on persons authorizations 
        sSQL = sSQL & vbCrLf & "And ((CATEGORIE_EMPLOI_categorie) ='toto'" 'uniquement pour ajouter à la requete SQL la clause And
        If bNonContract Then sSQL = sSQL & vbCrLf & "Or (CATEGORIE_EMPLOI_categorie) ='NC'"
        If bInfPO6 Then sSQL = sSQL & vbCrLf & "Or (CATEGORIE_EMPLOI_categorie) ='Emploi PO < 6'"
        If bPO6etPO7 Then sSQL = sSQL & vbCrLf & "Or (CATEGORIE_EMPLOI_categorie) ='Emploi  PO 6 & 7'"
        If bSupPO6 Then sSQL = sSQL & vbCrLf & "Or (CATEGORIE_EMPLOI_categorie) ='Emploi PO > 7'"
        sSQL = sSQL & ")"
    End If
    

    'On termine la requête SQL
    sSQL = sSQL & ";"
    
    'On vérif s'il y a des enregistrements
    Set rst = CurrentDb.OpenRecordset(sSQL, dbReadOnly)
    If rst.EOF And rst.BOF Then
                MsgBox "Le jeu de donné est vide!" & vbCrLf _
                & "    - Soit vous n'avez pas les autorisations pour afficher le/les emploi(s) " & vbCrLf _
                & "    - Soit une erreur est survenue " & vbCrLf _
                & "Merci de contacter l'administrateur"
    Else
        'On ouvre la vue emploi avec les données de la recherche
        DoCmd.OpenForm "F_Vue_Emploi"
        With Forms("F_Vue_Emploi")
            .RecordSource = sSQL
            .Requery
        End With
    End If
    
Fin:
    'Message d'erreur si une erreur est levée
    If err.Number <> 0 Then
        MsgBox "Erreur 'btnFilter_Click'  " & err.Number & "  " & err.Description
    End If

    'On ferme la popup de filtrage
    DoCmd.Close acForm, "F_Popup_Filtre_Emploi"
    
    'On passe le focus sur la vue emploi
    If IsOpenForm("F_Vue_Emploi") Then Forms!F_Vue_Emploi.SetFocus
    
    
    'Fermeture du Recordset
    rst.Close: Set rst = Nothing

End Sub

我的想法

是单独开发人 select 他想要的选择的部分:

If Not IsNull(comboPO) Then
        sSQL = sSQL & vbCrLf & "And (CODE_EMPLOI_categorie_emploi_id) =" & Me.comboPO.Column(0)
    End If

以及他有权查看的内容:

If bNonContract _
            Or bInfPO6 _
            Or bPO6etPO7 _
            Or bSupPO6 _
            Then
        'On créé la requête SQL par rapport aux autorisations
        sSQL = sSQL & vbCrLf & "And ((CATEGORIE_EMPLOI_categorie) ='toto'" 'uniquement pour ajouter à la requete SQL la clause And
        If bNonContract Then sSQL = sSQL & vbCrLf & "Or (CATEGORIE_EMPLOI_categorie) ='NC'"
        If bInfPO6 Then sSQL = sSQL & vbCrLf & "Or (CATEGORIE_EMPLOI_categorie) ='Emploi PO < 6'"
        If bPO6etPO7 Then sSQL = sSQL & vbCrLf & "Or (CATEGORIE_EMPLOI_categorie) ='Emploi  PO 6 & 7'"
        If bSupPO6 Then sSQL = sSQL & vbCrLf & "Or (CATEGORIE_EMPLOI_categorie) ='Emploi PO > 7'"
        sSQL = sSQL & ")"
    End If

是问题背后的原因

你能帮我解决这个问题吗?

谢谢 !!

编辑为了使我的请求更容易:

我怎么能说如果空白然后跳过这个

If Not IsNull(comboPO) Then
            sSQL = sSQL & vbCrLf & "And (CODE_EMPLOI_categorie_emploi_id) =" & Me.comboPO.Column(0)
        End If

问题解决了。 问题不在于我的代码,而在于我在关于该过滤器的属性表中放置并忘记了它的标准。

感谢您的时间 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM