Solved

Help with partial string search

Posted on 2013-01-08
7
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 75

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

628 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