Solved

search in 2 MSHflexgrid

Posted on 2011-03-14
13
278 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

26 Experts available now in Live!

Get 1:1 Help Now