Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 30th, 2006, 12:03 AM
Registered User
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Match records

Hi Experts, below is my code using to compare two table in one database using data control , a click on Check order button will delete those match record and show the unmatch record on msflexgrid1, but my problem is even the two table is similar (not same in sequence) the program still fail to delete the match, properly,pls help thank you

pls allow me to add some additional info on my question, the record on table 1 will only delete the first occurence record in table 2 , eg:

table 1 table 2
1.apple 1.strawberry
2.melon 2.apple
3.grape 3.melon
4.melon 4.apple
5.orange 5.nut
                      6.melon

so no.1 apple (table 1) and no.2(apple) will be deleted, while no.4 apple will remain unless the record on table 1 has another apple , so after delete the match record, grid will show the unmatch ones as follow :

table 1 table 2
3.grape 1.strawberry
5.orange 4.apple
                      5.nut

thank you very much

Below is the code i try :
Private Sub cmdcheckorder_Click()
Dim acc1, buysell1, lot1, price1, liq1, Dat1 As String
Dim acc2, buysell2, lot2, price2, liq2, Dat2 As String
Dim compare1 As String
Dim compare2 As String
MSFlexGrid1.Visible = False
MSFlexGrid2.Visible = False


Do While Not Data1.Recordset.EOF
acc1 = Data1.Recordset.Fields(1)
buysell1 = Data1.Recordset.Fields(2)
lot1 = Data1.Recordset.Fields(3)
price1 = Data1.Recordset.Fields(4)
liq1 = Data1.Recordset.Fields(5)
Dat1 = Trim(acc1) & Trim(buysell1) & Trim(lot1) & Trim(price1) & Trim(liq1)
compare1 = Dat1
     Data2.Recordset.MoveFirst

    Do While Not Data2.Recordset.EOF
       acc2 = Data2.Recordset.Fields(1)
       buysell2 = Data2.Recordset.Fields(2)
       lot2 = Data2.Recordset.Fields(3)
       price2 = Data2.Recordset.Fields(4)
       liq2 = Data2.Recordset.Fields(5)
       Dat2 = Trim(acc2) & Trim(buysell2) & Trim(lot2) & Trim(price2) & Trim(liq2)
       compare2 = Dat2
   If StrComp(Trim(Dat1), Trim(Dat2), vbTextCompare) = 0 Then

       Data1.Recordset.Delete
       Data1.Recordset.MoveNext
       If Data1.Recordset.EOF Then
       Data1.Recordset.MovePrevious
       End If

       Data2.Recordset.Delete
       Data2.Recordset.MoveNext
       If Data2.Recordset.EOF Then
       Data2.Recordset.MovePrevious
       End If
   End If

   If Not Data2.Recordset.EOF Then
   Data2.Recordset.MoveNext
   End If
   Loop

If Not Data1.Recordset.EOF Then
Data1.Recordset.MoveNext
End If

Loop

'====bind the msflexgrid programmingly,do not bind manually
'====coz it will display all match and unmatch at startup
Data1.Refresh
Data2.Refresh

MSFlexGrid1.Visible = True
MSFlexGrid2.Visible = True



End Sub

 
Old May 5th, 2006, 10:36 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Just for information purposes:
Code:
    Dim acc1, buysell1, lot1, price1, liq1, Dat1 As String
    Dim acc2, buysell2, lot2, price2, liq2, Dat2 As String

creates acc1, buysell1, lot1, price1, liq1, acc2, buysell2, lot2, price2, liq2, as [u]Variants</u>, and only Dat1 and Dat2 as strings...

You will find your own code much easier to read (and others will have an easier time reading your code as well) if you indent. For instance
Code:
   If Not Data2.Recordset.EOF Then
   Data2.Recordset.MoveNext
   End If
   Loop

If Not Data1.Recordset.EOF Then
Data1.Recordset.MoveNext
End If

Loop
would be [u]much</u> easier to follow as
Code:
            If Not Data2.Recordset.EOF Then
                Data2.Recordset.MoveNext
            End If
        Loop

        If Not Data1.Recordset.EOF Then
            Data1.Recordset.MoveNext
        End If

    Loop
    In the following section of code, it took me about 6 seconds to find the green line so as to match it up with the blue line. This should have been automatic through indentation ([u]0</u> seconds spent).
Code:
    Do While Not Data2.Recordset.EOF
Code:
       acc2 = Data2.Recordset.Fields(1)
       buysell2 = Data2.Recordset.Fields(2)
       lot2 = Data2.Recordset.Fields(3)
       price2 = Data2.Recordset.Fields(4)
       liq2 = Data2.Recordset.Fields(5)
       Dat2 = Trim(acc2) & Trim(buysell2) & Trim(lot2) & Trim(price2) & Trim(liq2)
       compare2 = Dat2
   If StrComp(Trim(Dat1), Trim(Dat2), vbTextCompare) = 0 Then

       Data1.Recordset.Delete
       Data1.Recordset.MoveNext
       If Data1.Recordset.EOF Then
       Data1.Recordset.MovePrevious
       End If

       Data2.Recordset.Delete
       Data2.Recordset.MoveNext
       If Data2.Recordset.EOF Then
       Data2.Recordset.MovePrevious
       End If
   End If

   If Not Data2.Recordset.EOF Then
   Data2.Recordset.MoveNext
   End If
   Loop
   Granted, this would have been easier in the IDE through context coloring, but indentation would still have eliminated the time spent scanning the lines of code to find the "If."





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Match Mitali XSLT 7 July 24th, 2008 03:39 AM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
problem in updating records & finding records naveed77 VB Databases Basics 1 January 16th, 2007 12:12 PM
problem in updating records & finding records naveed77 VB How-To 1 January 16th, 2007 12:10 PM
Help!! i need to get a closest match tseng Access VBA 3 February 4th, 2005 07:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.