Solved

Help with partial string search

Posted on 2013-01-08
7
209 Views
Last Modified: 2013-01-08
I am using the code below to select rows in a Grid where values of SDeactivation is found agains SDéactivation_ID which is in Coumn(2). SDeactivation_ID contains single values.

When SDeactivation is a single value all is working of but when SDeactivation includes multiple values (i.e. 1,2,3) it does not work. How do i modify the code to select rows when using multiple values for SDeactivation?

MsgBox("keySDeactivation)" & keySDeactivation)
        '  keySDeactivation = "1,2" does not works
        '  keySDeactivation = "1" works
        If keySDeactivation <> "" Then
            Dim _tableA As DataView = dtMasterA.DefaultView
                  C1SDeactivation.DataSource = _tableA
                    For i As Integer = 0 To C1SDeactivation.RowCount - 1
                If C1SDeactivation.Columns(2).CellValue(i).ToString().Contains(keySDeactivation) = True Then
                    C1SDeactivation.SelectedRows.Add(i)
                End If
            Next
        End If


Thanks,

Victor
0
Comment
Question by:vcharles
7 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 38754030
So your cell value contains a single value such as 1 or 2...

in other words, you can have
               C1SDeactivation.Columns(2).CellValue(i) =1
or           C1SDeactivation.Columns(2).CellValue(i) = 2
etc...

And you are comparing that to keySDeactivation where you can have
             keySDeactivation = 1
             keySDeactivation = 2
             keySDeactivation = "1,2"
etc..

Am I understanding that correctly?

<< If C1SDeactivation.Columns(2).CellValue(i).ToString().Contains(keySDeactivation) = True Then>>

If so, I think your Contains statement is backwards.

Try rewriting it like this:

                If keySDeactivation.Contains(C1SDeactivation.Columns(2).CellValue(i).ToString()) = True Then

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38754221
To make sure that 1 does not match with 11 using Contains function, I would add commas at start and end of the keySDeactivation and do following comparison

Outside loop:
keySDeactivation = "," & keySDeactivation & ","


Inside Loop:
If keySDeactivation.Contains("," & C1SDeactivation.Columns(2).CellValue(i).ToString() & ",") = True Then
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38754249
You could use regular expressions:

Imports System.Text.RegularExpressions

...

If Regex.IsMatch(C1SDeactivation.Columns(2).CellValue(i).ToString(), "\b" + keySDeactivation + "\b") Then
    C1SDeactivation.SelectedRows.Add(i)
End If

...

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Closing Comment

by:vcharles
ID: 38754529
Thank You for all the comments. This thread was the solution.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38754627
You are sure you wont have numbers greater than 9 in there?
0
 

Author Comment

by:vcharles
ID: 38754641
Yes, that is ok, those are just values of the ID fields I will use to select the rows.

Thanks.

Victor
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38754661
And you will never have an ID greater than 9(such as 10, 11 etc).  The reason I ask is that using the contains method as you are, 1 will match if there is 11 but no 1. So if you are trying to select just 11, both 1 and 11 will get selected.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

760 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

22 Experts available now in Live!

Get 1:1 Help Now