Results 1 to 12 of 12

Thread: [RESOLVED] Code Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Resolved [RESOLVED] Code Problem

    Ok I'm having some problems with this code not doing what I'd like for it to do. I'd like for it to loop through all the records in the MAIN table with the outer loop and then loop and compare the MAIN table records to the ALTER table records adding records that are not in the ALTER table. In other words, if the record is in the ALTER table AND the MAIN table it should not appear in the listbox. This code only loops around 10 times. The MAIN table has over 45,000 records in it. Any suggestions anyone? Thanks.
    VB Code:
    1. Set connection = New ADODB.connection
    2.     connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    3.         "Data Source= C:\Documents and Settings\P6B0438\My Documents\Programs\Visual Basic\DB\cycleCount.mdb"
    4.     connection.Open
    5.    
    6.     Set newConnection = New ADODB.connection
    7.     newConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    8.         "Data Source= C:\Documents and Settings\P6B0438\My Documents\Programs\Visual Basic\DB\cycleCount.mdb"
    9.     newConnection.Open
    10.    
    11.     Set recordSet = New ADODB.recordSet
    12.     Set newRecordSet = New ADODB.recordSet
    13.      
    14.     Dim inAlterTable As Boolean
    15.     Dim selectStatement As String
    16.     Dim newSelectStatement As String
    17.    
    18.     inAlterTable = False
    19.     selectStatement = "SELECT DISTINCT PICKSL,DFP,PROD,DESC FROM MAIN"
    20.     'newSelectStatement = "SELECT DISTINCT PICKSL,DFP,DESC FROM [ALTER]"
    21.    
    22.     recordSet.Open selectStatement, connection, adOpenKeyset, adLockPessimistic, adCmdText
    23.     'newRecordSet.Open newSelectStatement, newConnection, adOpenKeyset, adLockPessimistic, adCmdText
    24.     newRecordSet.Open "[ALTER]", newConnection, adOpenKeyset, adLockPessimistic, adCmdTable
    25.    
    26.     recordSet.MoveFirst
    27.     newRecordSet.MoveFirst
    28.        
    29.     Do Until recordSet.EOF
    30.         Do Until newRecordSet.EOF
    31.             If (recordSet.Fields("PICKSL") = newRecordSet.Fields("PICKSL") And _
    32.                 recordSet.Fields("DFP") = newRecordSet.Fields("DFP") And _
    33.                 recordSet.Fields("PROD") = newRecordSet.Fields("PROD") And _
    34.                 recordSet.Fields("DESC") = newRecordSet.Fields("DESC")) Then
    35.                 inAlterTable = True
    36.             End If
    37.             DoEvents
    38.             newRecordSet.MoveNext
    39.         Loop
    40.         If (inAlterTable = False) Then
    41.             lstFirstScreen.AddItem (recordSet.Fields("PICKSL") & vbTab _
    42.                 & recordSet.Fields("DFP") & vbTab _
    43.                 & recordSet.Fields("PROD") & vbTab _
    44.                 & recordSet.Fields("Desc"))
    45.         End If
    46.         DoEvents
    47.         recordSet.MoveNext
    48.     Loop
    49.    
    50.     recordSet.Close
    51.     connection.Close
    52.     Set recordSet = Nothing
    53.     Set connection = Nothing

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Code Problem

    Are you able to use an outter join?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Code Problem

    Probably, but not really sure how to do that

  4. #4
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: Code Problem

    Quote Originally Posted by manofsteel00
    ...
    This code only loops around 10 times. The MAIN table has over 45,000 records in it. Any suggestions anyone? Thanks.

    VB Code:
    1. ...
    2.  
    3.     recordSet.MoveFirst
    4.     newRecordSet.MoveFirst
    5.        
    6.     Do Until recordSet.EOF
    7.         Do Until newRecordSet.EOF
    8.             If (recordSet.Fields("PICKSL") = newRecordSet.Fields("PICKSL") And _
    9.                 recordSet.Fields("DFP") = newRecordSet.Fields("DFP") And _
    10.                 recordSet.Fields("PROD") = newRecordSet.Fields("PROD") And _
    11.                 recordSet.Fields("DESC") = newRecordSet.Fields("DESC")) Then
    12.                 inAlterTable = True
    13.             End If
    14.             DoEvents
    15.             newRecordSet.MoveNext
    16.         Loop
    17.         If (inAlterTable = False) Then
    18.             lstFirstScreen.AddItem (recordSet.Fields("PICKSL") & vbTab _
    19.                 & recordSet.Fields("DFP") & vbTab _
    20.                 & recordSet.Fields("PROD") & vbTab _
    21.                 & recordSet.Fields("Desc"))
    22.         End If
    23.         DoEvents
    24.         recordSet.MoveNext
    25.     Loop
    26.    
    27.     recordSet.Close
    28.     connection.Close
    29.     Set recordSet = Nothing
    30.     Set connection = Nothing
    Hello manofsteel00,
    I think the problem is that you should place this line :
    VB Code:
    1. newRecordSet.MoveFirst
    between the two loops:
    VB Code:
    1. ...
    2.     Do Until recordSet.EOF
    3.         [B][COLOR=Blue]newRecordSet.MoveFirst[/COLOR][/B]
    4.         Do Until newRecordSet.EOF
    5. ...
    That way every new iteration of recordSet will be compared to newRecordSet from the begining.
    Best Regards,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Code Problem

    I put the newRecordSet.moveFirst between the loops and it appears to loop through the entire contents; however, it never adds anything to the listbox.

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  6. #6
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Code Problem

    It would be something like this (mind you, I don't know which fields are relational)

    select DISTINCT MAIN.PICKSL,MAIN.DFP,.MAIN.PROD,MAIN.[DESC], [ALTER].PICKSL
    from MAIN, [ALTER]
    where MAIN.PICKSL *= [ALTER].PICKSL

    It probably would be a little less demanding. You can use a single recordset and connection. Tweak it a little bit and see if you can get it to work right for you.

    (In theory) The [ALTER].PICKSL field will only have a value if it has a matching record in MAIN; otherwise it will be null.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Code Problem

    I see what you're saying, but what I'm trying to do is output the only the contents of the MAIN table that are not in the ALTER table. Because if they are in the ALTER table then it means someone has already chosen them.

  8. #8
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Code Problem

    My response wasn't a silver bullet solution

    Instead of creating two recordsets are trying to compare them, you can loop through one and check just the last field to see if it's populated. If it is, it's in both the ALTER and the MAIN table. Using just an if/then, you can add it to the listbox accordingly.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Code Problem

    In this case using a subquery should work, eg:
    VB Code:
    1. selectStatement = "SELECT DISTINCT PICKSL,DFP,PROD,DESC FROM MAIN " _
    2.               & "WHERE PICKSL Not In(SELECT PICKSL FROM [ALTER])"
    (based on same join field assumption as sevenhalo's example)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Code Problem

    Could I do this?
    VB Code:
    1. selectStatement = "SELECT DISTINCT PICKSL,DFP,PROD,DESC FROM MAIN " _
    2.               & "WHERE PICKSL Not In(SELECT PICKSL,DFP,PROD,DESC FROM [ALTER])"
    Since multiple items can have the same pick slot, but they cannot have the same pick slot, dfp, product number, and description.

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Code Problem

    Nope, 'In' only works for one field...

    I think you should be able to get away with a 'Null' Left join, something like this (note the brackets may be wrong):

    VB Code:
    1. selectStatement = "SELECT DISTINCT M.PICKSL, M.DFP, M.PROD, M.[DESC] " _
    2.                 & "FROM Main M LEFT JOIN [Alter] A ON (" _
    3.                 & "  M.PICKSL = A.PICKSL AND M.DFP = A.DFP AND M.PROD = A.PROD AND M.[DESC] = A.[DESC] " _
    4.                 & "WHERE A.PICKSL Is Null"
    This will perform a left join (so all rows of Main are returned, even if no matching Alter data is present), but only allow rows where there is a Null value for PICKSL in Alter (which is how no match is shown for Left Joins).

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Code Problem

    Gotcha Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width