Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:19:36+00:00 2026-06-17T12:19:36+00:00

I have an MS Access form where the user can select up to four

  • 0

I have an MS Access form where the user can select up to four criteria to filter the records returned by a report. In the VBA, I’m trying to build my filter string dynamically based on which fields, if any, the user chose to filter by. The below code is giving me a “Block If without End If” error. What am I doing wrong here?

If (IsNull(frm!employee) = False) Then
    strFilter = "Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
    If IsNull(frm!sop) = False Then
        strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
    End If
    If IsNull(frm!revision) = False Then
        strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
    End If
    If IsNull(frm!dept) = False Then
        strFilter = strFilter + " AND department = '" & frm!dept & "'"
    End If
Else
If (IsNull(frm!sop) = False) Then
    strFilter = "sop_number = '" & frm!sop & "'"
    If (IsNull(frm!employee) = False) Then
        strFilter = "Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
    End If
    If IsNull(frm!revision) = False Then
        strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
    End If
    If IsNull(frm!dept) = False Then
        strFilter = strFilter + " AND department = '" & frm!dept & "'"
    End If
Else
If (IsNull(frm!revision) = False) Then
    strFilter = "revision_number = '" & frm!revision & "'"
    If (IsNull(frm!employee) = False) Then
        strFilter = " AND Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
    End If
    If IsNull(frm!sop) = False Then
        strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
    End If
    If IsNull(frm!dept) = False Then
        strFilter = strFilter + " AND department = '" & frm!dept & "'"
    End If
Else
If IsNull(frm!dept) = False Then
    strFilter = "department = '" & frm!dept & "'"
    If (IsNull(frm!employee) = False) Then
        strFilter = " AND Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
    End If
    If IsNull(frm!sop) = False Then
        strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
    End If
    If IsNull(frm!revision) = False Then
        strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
    End If
End If

Any advice you could give me to improve this code would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:19:37+00:00Added an answer on June 17, 2026 at 12:19 pm

    That code avoids ” AND ” at the beginning of strFilter. However the logic is challenging to follow and it uses multiple variations on the same basic themes.

    Use a simpler approach. You’re interested in the values contained in 4 form controls:

    • frm!employee
    • frm!sop
    • frm!Revision
    • frm!dept

    Examine each of them in turn and, for any which contain a value, add a segment starting with ” AND ” to strFilter. Afterwards, if strFilter contains any text, you know it starts with ” AND ” so you can simply discard the first 5 characters.

    strFilter = vbNullString ' <- make it explicit
    If Len(Trim(frm!employee) & vbNullString) > 0 Then
        strFilter = strFilter & _
            " AND Trainee_First_Name & ' ' &  Trainee_Last_Name = '" & _
            frm!employee & "'"
    End If
    If Len(Trim(frm!sop) & vbNullString) > 0 Then
        strFilter = strFilter & " AND sop_number = '" & frm!sop & "'"
    End If
    If Len(Trim(frm!Revision) & vbNullString) > 0 Then
        strFilter = strFilter & " AND revision_number = '" & _
            frm!Revision & "'"
    End If
    If Len(Trim(frm!dept) & vbNullString) > 0 Then
        strFilter = strFilter & " AND department = '" & frm!dept & "'"
    End If
    If Len(strFilter) > 0 Then
        ' discard " AND " from beginning of string
        strFilter = Mid(strFilter, 6)
    End If
    MsgBox "strFilter ->" & strFilter & "<-"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a combobox on a web form. The user can select a single
I have a form in access with a button that lets user select a
I have a bound form in microsoft access that allows a user to add
I have this select input in my form that I want to access via
I'm new to VBA and Access. I have to create an inventory loan form
I'm using Access 2003 and have a form that allows the user to pick
I have a search form, where user can look up estates he owns (search
Story I have a select control that represents user access level. I'm looking for
I have a Microsoft Access form that is bound to a linked SQL Server
Let's say I have an embedded Excel Spreadsheet on a Microsoft Access form. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.