Results 1 to 8 of 8
  1. #1
    nixonshaun is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2020
    Posts
    7

    Merge Two Pieces of VBA code into one sub

    I have a form with a query subform on it and two unbound text boxes.
    The goal is to add command buttons each filtering the date Between user input in unbound text boxes and between a specific disability. would be a command button for each of the disabilities we recognize.
    The query has several specific fields from the main table: Participant ID, Participant Name, Application Date, Disability Primary.
    it allows me to organize my caseload, based on the number of applications in a date range and various different disabilities we used to find someone eligible for service.



    I have a code snippet that will filter Between two dates (From and To) based on user added input in the text boxes on the form.
    I have another code snippet that will filter by specific disability using Like. e.g Like Cognitive*, Like Physical*, Like Psychosocial* etc.

    these both work well individually, but i would like to get them into one snippet of code that would allow me to filter by date, and then by primary disabilities within that date range.

    can anybody offer any advice, i am not a programmer, just picking this up from looking at other people's examples.

    heres the code:
    filter that works on date: (would ultimately like this command button to filter date and then find cognitive disability within that date range)

    Private Sub Cognitive_Click()
    Dim strFilter As String
    strFilter = "([Application Date] Between #" & Me.txtFromDate & "# And #" & Me.txtToDate & "#)"
    Me.DisabilityList.Form.Filter = strFilter
    Me.DisabilityList.Form.FilterOn = True
    End Sub

    filter that works on text using Like:
    Private Sub psychosocial_Click()
    Dim strFilterText As String
    strFilterText = "([Disability Primary] Like 'psycho*')"
    Me.DisabilityList.Form.Filter = strFilterText
    Me.DisabilityList.Form.FilterOn = True
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Are you always going to ennter dates and diability?
    If so simple contactenation is all that is needed.

    Otherwise you would need some IF statements

    Simple concatenation

    Code:
    strFilter = "[Application Date] Between #" & Me.txtFromDate & "# And #" & Me.txtToDate & "# AND [Disability Primary] Like '" & Me.Disability & "*'"
    Debug.Print strFilter
    I hope your dates are in USA format?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    nixonshaun is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2020
    Posts
    7
    Thanks Welshgasman,
    yes there will always be an application date, as we are only allowed 60 days to review records and make an eligibility determination, then 90 days from eligibility date to develop an individualized plan.
    yes date is formatted to USA.
    concatenation seems to work, though i am mostly unfamiliar with it, i think i used it once years ago in Excel to merge first name and surname into one cell.

    I changed your code slightly so i wouldn't need a text box of combo box for Me.Disability.
    looks something like this now: strFilter = "[Application Date] Between #" & Me.txtFromDate & "# And #" & Me.txtToDate & "# AND [Disability Primary] Like 'Cog*'"
    Debug.Print strFilter

    it works well, thanks alot.
    I have one area which is effectively physical and mobility, it includes mobility, both mobility and manipulation, general physical debilitation. is there a way i can get all three of three of these in the same line, i tried to add AND [Disability Primary] Like 'Mobility*' And [Disability Primary] Like 'Both*'"
    but it didnt seem to work, my formatting for this line must be off.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    Just check your logic- how can something be equal to two different things at the same time

    [Disability Primary] Like 'Mobility*' And [Disability Primary] Like 'Both*'"

    you need an or instead of an and. You are then also into Boolean logic - ands and ors need to be separated in some way

    a and b or c

    is that
    a and b OR c

    or

    a AND b or c?

    as with maths you need to use brackets

    either

    (a and b) OR c

    or

    a AND (b or c)


  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    What is the point of hard coding disability?, I would have thought you wanted whatever was in the disability control? regardless of the control type, otherwise you will be changing the code every 10 seconds.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    nixonshaun is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2020
    Posts
    7
    Thank you CJ that provides a more clear understanding of how to proceed.
    I tried it using Or and it works flawlessly.

    its been a challenge, trying to replicate Query Criteria from query design feature into Basic. But it's good learning something new, thanks again for your help.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    For multiple disabilities I would perhaps use IN ( ) and build the string?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    it’s been a challenge, trying to replicate Query Criteria from query design feature into Basic.
    switch to sql view to see the where clause

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 07-11-2019, 02:00 PM
  2. Date\Time\Scheduler Pieces 3
    By Cybercow in forum Code Repository
    Replies: 1
    Last Post: 11-22-2012, 05:20 PM
  3. Trying to get mail merge code to work
    By itm in forum Programming
    Replies: 0
    Last Post: 09-13-2012, 01:01 PM
  4. VBA Code for Mail Merge via Dynamic Data Exchange
    By Ganymede in forum Programming
    Replies: 2
    Last Post: 12-28-2011, 05:20 PM
  5. Merge Code problems
    By kfergus in forum Access
    Replies: 1
    Last Post: 06-01-2006, 03:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums