Solved

search in 2 MSHflexgrid

Posted on 2011-03-14
13
284 Views
Last Modified: 2012-05-11
Hello all,

I have an issue with my code. Not that it give me an error but the result wanted is not there.

I need to ask, for every row of grid 1 to find the value in column 5 in the grid2 in column10.

if it exist, it clear the column 14 from grid one of the same row as the result.

If it does not exist, then it put: MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"


Dim r1 As Long, r2 As Long, i As Long
Dim test1 As String
For r1 = 1 To MSHFlexGrid1.Rows - 1


   test1 = MSHFlexGrid1.TextMatrix(r1, 5)
   
   For r2 = 1 To MSHFlexGrid2.Rows - 1
      If MSHFlexGrid2.TextMatrix(r2, 10) = test1 Then
            MSHFlexGrid1.TextMatrix(r1, 14) = ""
            Else
               MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
               MSHFlexGrid1.Row = r1
               MSHFlexGrid1.Col = 14
               MSHFlexGrid1.CellBackColor = &H80FF&
               Exit For
           
      End If
   Next r2
Next r1

Open in new window


Now, it always put "NEEDS TO BE REMOVED" even if the result exist.

Thanks for your help
0
Comment
Question by:Wilder1626
[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
  • 6
  • 4
  • 3
13 Comments
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 35132636
Wilder,
  I thought your point was to put Removed when the value was found...

if that is the case then your if is backwards...

If MSHFlexGrid2.TextMatrix(r2, 10) <> test1 Then
   MSHFlexGrid1.TextMatrix(r1, 14) = ""
Else
   MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
   MSHFlexGrid1.Row = r1
   MSHFlexGrid1.Col = 14
   MSHFlexGrid1.CellBackColor = &H80FF&
Exit For
           
      End If

Open in new window

0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 35132661
also...bear in mind that vb6 is case sensitive when comparing values...
put a UCASE(TRIM()) around your values when comparing...

Dim r1 As Long, r2 As Long, i As Long
Dim test1 As String
For r1 = 1 To MSHFlexGrid1.Rows - 1
   test1 = UCASE(TRIM(MSHFlexGrid1.TextMatrix(r1, 5)))
   If UCASE(TRIM(MSHFlexGrid2.TextMatrix(r2, 10))) <> test1 Then
      MSHFlexGrid1.TextMatrix(r1, 14) = ""
   Else
      MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
      MSHFlexGrid1.Row = r1
      MSHFlexGrid1.Col = 14
      MSHFlexGrid1.CellBackColor = &H80FF&
      Exit For
   End If
   Next r2
Next r1

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35132727
The problem now is that it never put remove even if the text in cell 5 is not in grid 2.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Brook Braswell
ID: 35132813
AHH..
Reverse it back and leave the ucase(trim( in there...
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 35132847
Brook's last answer should work, unless your question is asked correctly. If so, just replace the "<>" in his code with "=", and it will work as your new question states.

*** UNLESS ***

We are talking numbers here, which is something we all neglected to ask. Sometimes, programmers will put numerical values in a FlexGrid's cells, and if they don't match character-for-character, you will not get a match. In other words, if you want a cell that contains "23.00" to match a cell that reads "23", there is more work to do. I hope I explained that well enough.

Are we dealing with numbers here?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35132883
Hello VBClassicGuy

You touch a point here.

Yes, we are talking about numbers.
0
 
LVL 14

Assisted Solution

by:VBClassicGuy
VBClassicGuy earned 100 total points
ID: 35132944
Then you need to replace this line:

If UCASE(TRIM(MSHFlexGrid2.TextMatrix(r2, 10))) <> test1 Then

with:

If Val(MSHFlexGrid2.TextMatrix(r2, 10)) <> Val(test1) Then
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 35132955
Or, use "=" instead of "<>", since it is unclear what you want the result to be. Use what works.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35132979
I did a test with only 1 line of data

Private Sub Command8_Click()
Dim r1 As Long, r2 As Long, i As Long
Dim test1 As String
For r1 = 1 To MSHFlexGrid1.Rows - 1
For r2 = 1 To MSHFlexGrid2.Rows - 1
   test1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
   Text1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
   Text2 = UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 10)))
   If UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 11))) = test1 Then
      MSHFlexGrid1.TextMatrix(r1, 14) = ""
      
   Else
      MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
      MSHFlexGrid1.Row = r1
      MSHFlexGrid1.Col = 14
      MSHFlexGrid1.CellBackColor = &H80FF&
      Exit For
      Exit For
   End If
   Next r2
Next r1
End Sub

Open in new window


Text1 = 040155031
Text2 = 040155031

Still give me "NEEDS TO BE REMOVED"
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35132991
Oh oh oh

Now i have something with
Dim r1 As Long, r2 As Long, i As Long
Dim test1 As String
For r1 = 1 To MSHFlexGrid1.Rows - 1
For r2 = 1 To MSHFlexGrid2.Rows - 1
   test1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
   Text1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
   Text2 = UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 10)))
   If UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 10))) = test1 Then
      MSHFlexGrid1.TextMatrix(r1, 14) = ""
      
   Else
      MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
      MSHFlexGrid1.Row = r1
      MSHFlexGrid1.Col = 14
      MSHFlexGrid1.CellBackColor = &H80FF&
      Exit For
      Exit For
   End If
   Next r2

Open in new window


That look's good for now.

I will give another try
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35134247
I think i know the problem but i don't know how to fix it.

The problem is that if for example, i have only 1 row in each grid, with the same value, it will work.

If i also have only 1 row per grid but both are different, then it will also put the message REMOVED.

Now the problem is that, if again first row in grid = first row in grid 2 but in grid 2, there is other row with value that don't match the first row in grid 1, then it will add "REMOVE" message.

So it keep's searching even it there was a perfect match.

How can we fix this in the code?

Thanks for your help.
0
 
LVL 14

Accepted Solution

by:
Brook Braswell earned 400 total points
ID: 35137348
Wilder,
   In your code here the problem is that if you Do find a match... then you "want" to keep going but if you do not you want to mark it.
  Your code only falls out of the Loop when NO match is found...
  I propose you change so that you do not mark until you either Find a match or you finish looping through all to see that you do not find a match.
Dim r1 As Long, r2 As Long, i As Long
Dim Text1 As String
Dim Text2 as String
Dim bFound as boolean
For r1 = 1 To MSHFlexGrid1.Rows - 1
   bFound = false 
   For r2 = 1 To MSHFlexGrid2.Rows - 1
      Text1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
      Text2 = UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 10)))
      If Val(Text1) = Val(Text2) Then
         bFound = False
         Exit For
      else
         bFound = True
      End If
   Next r2
   if bFound then 
      MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
      MSHFlexGrid1.Row = r1
      MSHFlexGrid1.Col = 14
      MSHFlexGrid1.CellBackColor = &H80FF&
   Else
      MSHFlexGrid1.TextMatrix(r1, 14) = ""
   End If
Next r1

Open in new window

0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 35139940
Thanks to all of you.

Now it works perfectly well.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

738 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