Solved

Search field: ability to find 1 part, range, or specific parts

Posted on 2012-03-30
14
267 Views
Last Modified: 2012-03-31
Dear experts!
I am trying to find canned code that allows me to enter a search field, similar to the print dialog in Windows: ability to find one or more parts, according to any of the following syntax options:
1 (finds part 1)
1-4 (finds parts 1-4)
1,4,6,7 (finds parts 1 4 6 and 7)
etc.

Function would validate the string in some fashion, and then be able to set the filter on a subform to only those parts whose key field matched the above criteria.

Any ideas?

THANKS
0
Comment
Question by:terpsichore
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37789855
post a sample db and detail explanation...
0
 
LVL 39

Expert Comment

by:als315
ID: 37790115
You can create function with text string and criteria as parameters, which will return true or false.
Use split to separate criterias from delimited string. If any of criteria will match, return true, else - false.
Add this function as a column to your subform's record source. Add filter to this field (=true). After criteria field will be changed, requery subform. Filter can be switched on and off.
0
 

Author Comment

by:terpsichore
ID: 37790852
I guess j was asking if someone had or knew of a canned procedure since the logic isn't trivial.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37791063
your description is trivial.  If the problem isn't trivial, you need to be more specific.
0
 

Author Comment

by:terpsichore
ID: 37791077
I will do my best.
We have a part table, that has a key field (autonumber) of ID, and a Part_Number that is sequential within each project (1,2,3,4,5 - no other numbering is possible; these numbers are automatically assigned).
I have a subform that displays records for the various parts on this project. The problem is that many projects have many parts and the display is cumbersome.
We would like a control similar to that found in the print dialog, where you can choose to print pages like this: 1 or 1-2 or 1,3,5,7.
Hopefully that helps....
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37791129
there is no available canned codes for what you are want.. here is how you can start this going

dim str as string, j as integer, strDelim, strFilter, nArr

'test for the content of the textbox named "txt0"

if instr(txt0,"-") > 0 then strdelim="-"

if instr(txt0,",") > 0 then strdelim=","

select case strdelim
     case "-"
            strFilter="[part_number] >= " & split(txt0,"-")(0) &" and [part_number] <= " & split(txt0,"-")(1)

     case ","
            nArr=split(txt0,",")
            for j=0 to ubound(nArr)
                  str=str & "," & nArr(j)
             next
            str=mid(str,2)
            strFilter="[part_number] In (" & str & ")"

    case else
           strFilter="[part_number]= " & txt0
end select


now you can use strFilter to filter the subform



me.subformcontrolName.form.filter=strFIlter
me.subformcontrolName.form.filteron=true
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37791179
This will work to create a selection criterion for a filter property or a domain function or a query.
Public Function RecordFilter(ByVal parmFilter As String, ByVal parmKeyFieldname As String) As String
    Dim strCondition As String
    Dim strParsed() As String
    Dim strInVals As String
    Dim strBetweenVals As String
    Dim lngLoop As Long
    strParsed = Split(parmFilter, ",")
    For lngLoop = 0 To UBound(strParsed)
        If InStr(strParsed(lngLoop), "-") = 0 Then
            strInVals = strInVals & "," & strParsed(lngLoop)
        Else
            strBetweenVals = strBetweenVals & " and (" & parmKeyFieldname & " between " & Split(strParsed(lngLoop), "-")(0) & " and " & Split(strParsed(lngLoop), "-")(1) & ")"
        End If
    Next
    RecordFilter = parmKeyFieldname & " In (" & Mid(strInVals, 2) & ") " & strBetweenVals
End Function

Open in new window


This is a sample test in the immediate window.
x="1,3,5,7,13-19, 21, 26-35"
?RecordFilter(x, "id")
id In (1,3,5,7, 21)  and (id between 13 and 19) and (id between  26 and 35)

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 37791203
oops.  the logical operator should be an OR instead of an AND

Public Function RecordFilter(ByVal parmFilter As String, ByVal parmKeyFieldname As String) As String
    Dim strCondition As String
    Dim strParsed() As String
    Dim strInVals As String
    Dim strBetweenVals As String
    Dim lngLoop As Long
    strParsed = Split(parmFilter, ",")
    For lngLoop = 0 To UBound(strParsed)
        If InStr(strParsed(lngLoop), "-") = 0 Then
            strInVals = strInVals & "," & strParsed(lngLoop)
        Else
            strBetweenVals = strBetweenVals & " or (" & parmKeyFieldname & " between " & Split(strParsed(lngLoop), "-")(0) & " and " & Split(strParsed(lngLoop), "-")(1) & ")"
        End If
    Next
    RecordFilter = parmKeyFieldname & " In (" & Mid(strInVals, 2) & ") " & strBetweenVals
End Function

Open in new window


should return the following string
id In (1,3,5,7, 21)  or (id between 13 and 19) or (id between  26 and 35)

Open in new window

0
 

Author Closing Comment

by:terpsichore
ID: 37791284
amazing - perfect solution. thanks so much.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37791348
:-)
0
 

Author Comment

by:terpsichore
ID: 37791414
actually I should have awarded capricorn some points as well - my apologies. How do I notify sysadmin?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37791555
click on  Request Attention  somewhere below the box where you posted your original question
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37791860
@capricorn1

Does your code cover multiple #-# ranges?
0
 

Author Comment

by:terpsichore
ID: 37791863
yes - I tweaked the original code (there were a few modifications needed) and it is working well. thanks.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now