דילוג לתוכן
  • דף הבית
  • קטגוריות
  • פוסטים אחרונים
  • משתמשים
  • חיפוש
  • חוקי הפורום
כיווץ
תחומים

תחומים - פורום חרדי מקצועי

💡 רוצה לזכור קריאת שמע בזמן? לחץ כאן!
  1. דף הבית
  2. תכנות
  3. יצירת מסנן לדו"ח באקסס עם תנאים

יצירת מסנן לדו"ח באקסס עם תנאים

מתוזמן נעוץ נעול הועבר תכנות
12 פוסטים 4 כותבים 397 צפיות 4 עוקבים
  • מהישן לחדש
  • מהחדש לישן
  • הכי הרבה הצבעות
תגובה
  • תגובה כנושא
התחברו כדי לפרסם תגובה
נושא זה נמחק. רק משתמשים עם הרשאות מתאימות יוכלו לצפות בו.
  • hp079H hp079

    יש לי דו"ח באקסס שמכיל נתונים עם (בין היתר) השדות הבאים:

    מדינה = טקסט
    סוכן מטפל = טקסט מתוך רשימה
    מאושר - תיבת סימון
    סיים טיפול = תיבת סימון

    ואני רוצה שבלחיצה על "צור דוח" ייבדוק עם האובייקטים (כל אחד בנפרד)

    filter_co, agent, set_ok, done

    מכילים נתונים או ריקים, או במקרה של ב' האחרונים אם תיבת הסימון לא מוגדרת (עם ריבוע, לא וי ולא ריק).

    ואם filter_co מכיל נתונים ייצור מסנן לדו"ח שייפתח רק עם הרשומות שהנתון של השדה Country שווים לנתון שמכיל filter_co.
    וכן לתיבת הרשימה המשולבת של agent.
    ואם תיבת הסימון set_ok מסומנת בוי או באיקס יוסיף למסנן גם סינון של השדה Approval_status וכן לאוביקט done.

    ולאחמ"כ ייפתח את הדו"ח.

    וזאת שאלתי לאלופי האקסס דכאן האם או כיצד ניתן לעשות זאת באמצעות vba? רק אדגיש שאני רוצה לדעת איך לעשות את זה, לכן אם אי מי יביא את הקוד כתוב אשמח שגם יבאר מה מו מי.

    נ.ב. אני מעוניין גם לעשות שיהיה אפשר ליצור דו"ח עבור כלל הסוכנים יחד ע"י שהשרשור של ערך התנאי של agent יהיה בלולאה על עמודת שמות הסוכנים בטבלת הסוכנים, ובמרה הזה במקום לפתוח את הדו"ח בתצוגת הדפסה ייצא את הדו" לPDF אל תיקיית משנה בתיקיית קובץ האקסס האם גם זה אפשרי?

    OdedDvirO מנותק
    OdedDvirO מנותק
    OdedDvir
    כתב ב נערך לאחרונה על ידי OdedDvir
    #2

    @hp079 אפשר.
    הואיל ואתה רוצה להבין את התהליך, אתן לך את הכיוון, ומשם תוכל להמשיך לבד.
    בתחביר פתיחה של דוח, ניתן להגדיר מסנן או תנאי:

    OpenReport ReportName, View, FilterName, WhereCondition, WindowMode, OpenArgs
    

    מה שצריך זה לבנות בקוד את התנאי באופן דינאמי כמחרוזת, ואז לפתוח את הדוח עם התנאי.
    לדוגמא:

    Dim DynamicCondition As String
    DynamicCondition=""
    ' Add first condition
    If (Not IsNull(chkSet_ok)) Then
       DynamicCondition="Ok=" & CStr(chkSet_ok)
    End If
    ' Add second condition:
    If (Not IsNull(chkDone)) Then
       DynamicCondition=IIF(Len(DynamicCondition)>0, DynamicCondition & " AND ", "") & "Done=" & CStr(chkDone)
    End If
    ' Add more conditions
    If (Only_Bar_Mitsva) Then
       DynamicCondition=IIF(Len(DynamicCondition)>0, DynamicCondition & " AND ", "") & "Age>=13"
    End If
    ...
    ' Open report with all my conditions:
    DoCmd.OpenReport "MyReport", acViewNormal, WhereCondition:=DynamicCondition
    
    hp079H תגובה 1 תגובה אחרונה
    5
    • OdedDvirO OdedDvir

      @hp079 אפשר.
      הואיל ואתה רוצה להבין את התהליך, אתן לך את הכיוון, ומשם תוכל להמשיך לבד.
      בתחביר פתיחה של דוח, ניתן להגדיר מסנן או תנאי:

      OpenReport ReportName, View, FilterName, WhereCondition, WindowMode, OpenArgs
      

      מה שצריך זה לבנות בקוד את התנאי באופן דינאמי כמחרוזת, ואז לפתוח את הדוח עם התנאי.
      לדוגמא:

      Dim DynamicCondition As String
      DynamicCondition=""
      ' Add first condition
      If (Not IsNull(chkSet_ok)) Then
         DynamicCondition="Ok=" & CStr(chkSet_ok)
      End If
      ' Add second condition:
      If (Not IsNull(chkDone)) Then
         DynamicCondition=IIF(Len(DynamicCondition)>0, DynamicCondition & " AND ", "") & "Done=" & CStr(chkDone)
      End If
      ' Add more conditions
      If (Only_Bar_Mitsva) Then
         DynamicCondition=IIF(Len(DynamicCondition)>0, DynamicCondition & " AND ", "") & "Age>=13"
      End If
      ...
      ' Open report with all my conditions:
      DoCmd.OpenReport "MyReport", acViewNormal, WhereCondition:=DynamicCondition
      
      hp079H מנותק
      hp079H מנותק
      hp079
      כתב ב נערך לאחרונה על ידי
      #3

      @OdedDvir לא ממש הבנתי כיצד אני כותב את התנאים לתיבות סימון תלת מצבית ולתיבות הטקסט...

      OdedDvirO תגובה 1 תגובה אחרונה
      0
      • hp079H hp079

        @OdedDvir לא ממש הבנתי כיצד אני כותב את התנאים לתיבות סימון תלת מצבית ולתיבות הטקסט...

        OdedDvirO מנותק
        OdedDvirO מנותק
        OdedDvir
        כתב ב נערך לאחרונה על ידי OdedDvir
        #4

        @hp079 לתיבת סימון תלת מצבית שלושה ערכים אפשריים:
        True, False, Null. הערך Null משמעותו ללא סימון.
        אם אין סימון בתיבה זו, מסתמא הלוגיקה היא לא לסנן לפיה, לכן בשורה:

        If (Not IsNull(chkSet_ok)) Then
        

        אתה מוודא שהתיבה מסומנת, כלומר אם היא לא מכילה Null, ואם כן, השורה:

        DynamicCondition="Ok=" & CStr(chkSet_ok)
        

        מוסיפה לתנאי את הבדיקה האם השדה Ok (בדוח) שווה לערך טבלת הסימון.

        לא צירפתי דוגמא לבדיקה של תיבות הטקסט, אבל היא אמורה להתבצע באופן כמעט זהה, דהיינו אם ישנו ערך כלשהו בתיבת הטקסט, אז להוסיף תנאי סינון לשדה הרצוי לפי ערך זה.
        הנה דוגמא שמסננת את הדוח כך שיוצגו רק רשומות בהן ערך השדה FirstName מכיל את מחרוזת החיפוש שהוקלדה בתיבת הטקסט txtSearchFirstName:

        If (Len(txtSearchFirstName) > 0) Then
           DynamicCondition=IIF(Len(DynamicCondition)>0, " AND ", "") & "FirstName LIKE %" & txtSearchFirstName & "%"
        End If
        
        hp079H תגובה 1 תגובה אחרונה
        6
        • OdedDvirO OdedDvir

          @hp079 לתיבת סימון תלת מצבית שלושה ערכים אפשריים:
          True, False, Null. הערך Null משמעותו ללא סימון.
          אם אין סימון בתיבה זו, מסתמא הלוגיקה היא לא לסנן לפיה, לכן בשורה:

          If (Not IsNull(chkSet_ok)) Then
          

          אתה מוודא שהתיבה מסומנת, כלומר אם היא לא מכילה Null, ואם כן, השורה:

          DynamicCondition="Ok=" & CStr(chkSet_ok)
          

          מוסיפה לתנאי את הבדיקה האם השדה Ok (בדוח) שווה לערך טבלת הסימון.

          לא צירפתי דוגמא לבדיקה של תיבות הטקסט, אבל היא אמורה להתבצע באופן כמעט זהה, דהיינו אם ישנו ערך כלשהו בתיבת הטקסט, אז להוסיף תנאי סינון לשדה הרצוי לפי ערך זה.
          הנה דוגמא שמסננת את הדוח כך שיוצגו רק רשומות בהן ערך השדה FirstName מכיל את מחרוזת החיפוש שהוקלדה בתיבת הטקסט txtSearchFirstName:

          If (Len(txtSearchFirstName) > 0) Then
             DynamicCondition=IIF(Len(DynamicCondition)>0, " AND ", "") & "FirstName LIKE %" & txtSearchFirstName & "%"
          End If
          
          hp079H מנותק
          hp079H מנותק
          hp079
          כתב ב נערך לאחרונה על ידי hp079
          #5

          @OdedDvir עשיתי כך

          Dim DynamicCondition As String
          DynamicCondition = ""
          ' Add first condition
          If (Not IsNull(Set_ok)) Then
             DynamicCondition = "ok=" & CStr(Set_ok)
          End If
          
          If (Not IsNull(done)) Then
             DynamicCondition = "done=" & CStr(done)
          End If
          
          If (Len(agent) > 0) Then
             DynamicCondition = IIf(Len(DynamicCondition) > 0, " AND ", "") & "agent LIKE %" & "agent" & "%"
          End If
          
          ' Open report with all my conditions:
          DoCmd.OpenReport "logForOne", acViewPreview, WhereCondition:=DynamicCondition
          

          אבל התנאי של תיבת הטקסט "agent" על השדה agent נתקע, כשעשיתי את התיבות סימון לבד, זה עבד יופי.

          OdedDvirO תגובה 1 תגובה אחרונה
          0
          • hp079H hp079

            @OdedDvir עשיתי כך

            Dim DynamicCondition As String
            DynamicCondition = ""
            ' Add first condition
            If (Not IsNull(Set_ok)) Then
               DynamicCondition = "ok=" & CStr(Set_ok)
            End If
            
            If (Not IsNull(done)) Then
               DynamicCondition = "done=" & CStr(done)
            End If
            
            If (Len(agent) > 0) Then
               DynamicCondition = IIf(Len(DynamicCondition) > 0, " AND ", "") & "agent LIKE %" & "agent" & "%"
            End If
            
            ' Open report with all my conditions:
            DoCmd.OpenReport "logForOne", acViewPreview, WhereCondition:=DynamicCondition
            

            אבל התנאי של תיבת הטקסט "agent" על השדה agent נתקע, כשעשיתי את התיבות סימון לבד, זה עבד יופי.

            OdedDvirO מנותק
            OdedDvirO מנותק
            OdedDvir
            כתב ב נערך לאחרונה על ידי OdedDvir
            #6

            @hp079 כתב ביצירת מסנן לדו"ח באקסס עם תנאים:

            DynamicCondition = IIf(Len(DynamicCondition) > 0, " AND ", "") & "agent LIKE %" & "agent" & "%"

            יש לך גרשיים מיותרות מסביב למשתנה agent. אתה צריך לרשום כך:

            DynamicCondition = IIf(Len(DynamicCondition) > 0, " AND ", "") & "agent LIKE %" & agent & "%"
            

            כשה-agent האחרון בשורה מתייחס לשם של פקד תיבת הטקסט בטופס המכיל את שם הסוכן לחיפוש.

            hp079H תגובה 1 תגובה אחרונה
            2
            • OdedDvirO OdedDvir

              @hp079 כתב ביצירת מסנן לדו"ח באקסס עם תנאים:

              DynamicCondition = IIf(Len(DynamicCondition) > 0, " AND ", "") & "agent LIKE %" & "agent" & "%"

              יש לך גרשיים מיותרות מסביב למשתנה agent. אתה צריך לרשום כך:

              DynamicCondition = IIf(Len(DynamicCondition) > 0, " AND ", "") & "agent LIKE %" & agent & "%"
              

              כשה-agent האחרון בשורה מתייחס לשם של פקד תיבת הטקסט בטופס המכיל את שם הסוכן לחיפוש.

              hp079H מנותק
              hp079H מנותק
              hp079
              כתב ב נערך לאחרונה על ידי
              #7

              @OdedDvir בשימוש בקוד הזה, מה יקרה כשארצה לסנן לפי התוכן של שלושת השדות, כעת זה לא מסנן רק לפי אחד מהם?

              אני כותב, כי לפי מה שראיתי הוא תמיד מתעלם מהפרמטר שבערך של הif הראשון...

              OdedDvirO תגובה 1 תגובה אחרונה
              0
              • hp079H hp079

                @OdedDvir בשימוש בקוד הזה, מה יקרה כשארצה לסנן לפי התוכן של שלושת השדות, כעת זה לא מסנן רק לפי אחד מהם?

                אני כותב, כי לפי מה שראיתי הוא תמיד מתעלם מהפרמטר שבערך של הif הראשון...

                OdedDvirO מנותק
                OdedDvirO מנותק
                OdedDvir
                כתב ב נערך לאחרונה על ידי OdedDvir
                #8

                @hp079 מתוך ההערה שלך, הערת את תשומת לבי שהייתה לי טעות בקוד המקורי שלי: בכל תנאי חדש אני דרסתי את התוכן הקודם של DynamicCondition במקום לשרשר אליו תנאי נוסף...

                כתשובת המשקל, עכשיו אני אחיל קצת עקרונות תכנות, ואכתוב את הכל מחדש כך:

                Dim DynamicCondition As String
                
                Private Sub AddCondition(ByRef currentCondition As String, conditionToAdd As String)
                Begin
                   If (Len(currentCondition)=0) Then
                      currentCondition = conditionToAdd
                   Else
                      currentCondition = currentCondition & " AND " & conditionToAdd
                   End If
                End
                
                DynamicCondition=""
                ' Add first condition
                If (Not IsNull(chkSet_ok)) Then AddCondition DynamicCondition, "Ok=" & CStr(chkSet_ok)
                ' Add second condition:
                If (Not IsNull(chkDone)) Then AddCondition DynamicCondition, "Done=" & CStr(chkDone)
                ' Add more conditions
                If (Only_Bar_Mitsva) Then AddCondition DynamicCondition, "Age>=13"
                ...
                ' Open report with all my conditions:
                DoCmd.OpenReport "MyReport", acViewNormal, WhereCondition:=DynamicCondition
                
                dovidD hp079H 2 תגובות תגובה אחרונה
                3
                • OdedDvirO OdedDvir

                  @hp079 מתוך ההערה שלך, הערת את תשומת לבי שהייתה לי טעות בקוד המקורי שלי: בכל תנאי חדש אני דרסתי את התוכן הקודם של DynamicCondition במקום לשרשר אליו תנאי נוסף...

                  כתשובת המשקל, עכשיו אני אחיל קצת עקרונות תכנות, ואכתוב את הכל מחדש כך:

                  Dim DynamicCondition As String
                  
                  Private Sub AddCondition(ByRef currentCondition As String, conditionToAdd As String)
                  Begin
                     If (Len(currentCondition)=0) Then
                        currentCondition = conditionToAdd
                     Else
                        currentCondition = currentCondition & " AND " & conditionToAdd
                     End If
                  End
                  
                  DynamicCondition=""
                  ' Add first condition
                  If (Not IsNull(chkSet_ok)) Then AddCondition DynamicCondition, "Ok=" & CStr(chkSet_ok)
                  ' Add second condition:
                  If (Not IsNull(chkDone)) Then AddCondition DynamicCondition, "Done=" & CStr(chkDone)
                  ' Add more conditions
                  If (Only_Bar_Mitsva) Then AddCondition DynamicCondition, "Age>=13"
                  ...
                  ' Open report with all my conditions:
                  DoCmd.OpenReport "MyReport", acViewNormal, WhereCondition:=DynamicCondition
                  
                  dovidD מנותק
                  dovidD מנותק
                  dovid
                  ניהול
                  כתב ב נערך לאחרונה על ידי dovid
                  #9

                  @OdedDvir גירסה שלי:

                  Dim DynamicCondition As New Collection
                  
                  If Not IsNull(chkSet_ok) Then DynamicCondition.Add "Ok=" & CStr(chkSet_ok)
                  If Not IsNull(chkDone) Then DynamicCondition.Add "Done=" & CStr(chkDone)
                  If Only_Bar_Mitsva Then DynamicCondition.Add "Age>=13"
                  
                  DoCmd.OpenReport "MyReport", acViewNormal, WhereCondition:=JoinCollection(DynamicCondition, "AND")
                  

                  JoinCollection:

                  Function JoinCollection(col As Collection, operator As String)
                      Dim result As String
                      For i = 1 To col.Count - 1
                         result = result & col(i) & operator
                      Next
                      result = result & col(i)
                      JoinCollection = result
                  End Function
                  
                  • מנטור אישי בתכנות והמסתעף – להתקדם לשלב הבא!
                  • בכל נושא אפשר ליצור קשר dovid@tchumim.com
                  OdedDvirO תגובה 1 תגובה אחרונה
                  3
                  • OdedDvirO OdedDvir

                    @hp079 מתוך ההערה שלך, הערת את תשומת לבי שהייתה לי טעות בקוד המקורי שלי: בכל תנאי חדש אני דרסתי את התוכן הקודם של DynamicCondition במקום לשרשר אליו תנאי נוסף...

                    כתשובת המשקל, עכשיו אני אחיל קצת עקרונות תכנות, ואכתוב את הכל מחדש כך:

                    Dim DynamicCondition As String
                    
                    Private Sub AddCondition(ByRef currentCondition As String, conditionToAdd As String)
                    Begin
                       If (Len(currentCondition)=0) Then
                          currentCondition = conditionToAdd
                       Else
                          currentCondition = currentCondition & " AND " & conditionToAdd
                       End If
                    End
                    
                    DynamicCondition=""
                    ' Add first condition
                    If (Not IsNull(chkSet_ok)) Then AddCondition DynamicCondition, "Ok=" & CStr(chkSet_ok)
                    ' Add second condition:
                    If (Not IsNull(chkDone)) Then AddCondition DynamicCondition, "Done=" & CStr(chkDone)
                    ' Add more conditions
                    If (Only_Bar_Mitsva) Then AddCondition DynamicCondition, "Age>=13"
                    ...
                    ' Open report with all my conditions:
                    DoCmd.OpenReport "MyReport", acViewNormal, WhereCondition:=DynamicCondition
                    
                    hp079H מנותק
                    hp079H מנותק
                    hp079
                    כתב ב נערך לאחרונה על ידי
                    #10

                    @OdedDvir @dovid תודה אבל זה לא עובד, הוא מקפיץ שגיאה כל פעם משהו אחר.

                    עשיתי קובץ דוגמה עם מה שהבנתי ואיפה שהסתבכתי
                    דוגמה.accdb

                    א תגובה 1 תגובה אחרונה
                    0
                    • dovidD dovid

                      @OdedDvir גירסה שלי:

                      Dim DynamicCondition As New Collection
                      
                      If Not IsNull(chkSet_ok) Then DynamicCondition.Add "Ok=" & CStr(chkSet_ok)
                      If Not IsNull(chkDone) Then DynamicCondition.Add "Done=" & CStr(chkDone)
                      If Only_Bar_Mitsva Then DynamicCondition.Add "Age>=13"
                      
                      DoCmd.OpenReport "MyReport", acViewNormal, WhereCondition:=JoinCollection(DynamicCondition, "AND")
                      

                      JoinCollection:

                      Function JoinCollection(col As Collection, operator As String)
                          Dim result As String
                          For i = 1 To col.Count - 1
                             result = result & col(i) & operator
                          Next
                          result = result & col(i)
                          JoinCollection = result
                      End Function
                      
                      OdedDvirO מנותק
                      OdedDvirO מנותק
                      OdedDvir
                      כתב ב נערך לאחרונה על ידי
                      #11

                      @hp079 אם תצרף את נוסח השגיאה אוכל להתייחס.
                      הגרסא של @dovid יותר תיקנית, רק שבטעות נעלם ממנו ריפוד הרווחים מסביב לאופרטור, וצ"ל כך:

                           result = result & col(i) & " " & operator & " "
                      

                      אפשר גם לבודד כל תנאי על ידי סוגריים, כדי למנוע בעיות קדימות במקרים מסויימים:

                            result = result & "(" col(i) & ") " & operator & " "
                         Next
                         result = result & "(" col(i) & ")"
                      
                      תגובה 1 תגובה אחרונה
                      2
                      • hp079H hp079

                        @OdedDvir @dovid תודה אבל זה לא עובד, הוא מקפיץ שגיאה כל פעם משהו אחר.

                        עשיתי קובץ דוגמה עם מה שהבנתי ואיפה שהסתבכתי
                        דוגמה.accdb

                        א מנותק
                        א מנותק
                        ארי
                        כתב ב נערך לאחרונה על ידי ארי
                        #12

                        @hp079 לפי הדוגמא שלך זה הקוד שאתה צריך

                        Private Sub פקודה16_Click()
                        Dim DynamicCondition As New Collection
                        
                        If Not IsNull(ts_active) Then DynamicCondition.Add "פעיל=" & ts_active
                        If Not IsNull(ts_conected) Then DynamicCondition.Add "מחובר=" & ts_active
                        If Len(txt_sug) > 0 Then DynamicCondition.Add "סוג='" & CStr(txt_sug) & "'"
                        If Len(txt_name) > 0 Then DynamicCondition.Add "שם='" & CStr(txt_name) & "'"
                        
                        DoCmd.OpenReport "דוח1", acViewPreview, WhereCondition:=JoinCollection(DynamicCondition, " AND ")
                        End Sub
                        
                        Function JoinCollection(col As Collection, operator As String)
                            Dim result As String
                            For i = 1 To col.Count
                                If i <> 1 Then result = result & operator & " "
                                result = result & "(" & col(i) & ") "
                            Next
                            JoinCollection = result
                        End Function
                        

                        הקרדיט כמובן ל @dovid ו @OdedDvir

                        תגובה 1 תגובה אחרונה
                        3
                        תגובה
                        • תגובה כנושא
                        התחברו כדי לפרסם תגובה
                        • מהישן לחדש
                        • מהחדש לישן
                        • הכי הרבה הצבעות


                        בא תתחבר לדף היומי!
                        • התחברות

                        • אין לך חשבון עדיין? הרשמה

                        • התחברו או הירשמו כדי לחפש.
                        • פוסט ראשון
                          פוסט אחרון
                        0
                        • דף הבית
                        • קטגוריות
                        • פוסטים אחרונים
                        • משתמשים
                        • חיפוש
                        • חוקי הפורום