Results 1 to 5 of 5
  1. #1
    jscriptor09 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    60

    need better VBA logic than I currently have

    Hi folks

    I am looking for better way to do the following. Teh code below is how I do it now and it may be very inefficient.

    1. I dump a list of image files (jpe) that I have on my server to a file.
    2. I have a list of recent images files
    Below, I create a loop to look ate each image in (1) and if not found in (2) then the image name is written to a table.

    Item (1) list file can have up to 100,000

    Therefore, the loop below can be time consuming (actually, it is).

    So, the objective is compare each jpg file in item (1) list to item (2) list and if not found, write the item to a 3rd table.

    I would appreciate abetter solution than the one I show below.

    Thanks
    Mike





    Public Function findOldImages()
    Dim rs As DAO.Recordset
    Dim rs1 As DAO.Recordset
    Dim rs2 As DAO.Recordset
    Dim DB As Database
    Dim strSQL1 As String
    Dim strSQL2 As String
    Dim strSQL3 As String
    Dim icounter As Integer
    Dim i, j As Integer
    Set DB = CurrentDb


    '
    strSQL1 = "CREATE Table ImagesToDelete(theImage String, theLocation String)"
    DoCmd.RunSQL strSQL1
    '
    'DirImagesFileList
    strSQL2 = "SELECT * FROM DirImagesFileList"
    Set rs2 = DB.OpenRecordset(strSQL2)
    rs2.MoveFirst
    DoCmd.SetWarnings False
    Do While Not rs2.EOF
    theImage = Mid(rs2!Field2, 1, 7)
    If (Len(Trim(theImage )) > 5) Then
    Set rs3 = DB.OpenRecordset("SELECT anImage FROM imagetable WHERE someImage =" & theIamge)
    If rs3.RecordCount <= 0 Then
    strSQL1 = "INSERT INTO ImagesToDelete Values('" & rs2!Field2 & "','" & rs2!Field1 & "')"
    DoCmd.RunSQL strSQL1
    End If
    End If
    rs2.MoveNext
    Loop
    DoCmd.SetWarnings True
    End Function

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by jscriptor09 View Post
    So, the objective is compare each jpg file in item (1) list to item (2) list and if not found, write the item to a 3rd table.
    What you're basically doing here is using recordset loops to do a task that something a single query can do by itself. So yeah, it's incredibly inefficient.

    I didn't read all your code, but obviously you're extracting portions of the image names, so you'll need to use the same string extraction functions in the query statements too.

    Basically the way you do this properly, is use an outer join in an append query to target the image names that are in table 1 but not table 2. Join on the equal field value and execute the query. That's the whole task, literally.

    so if you want image names from table1 that's not in table2 and you want to append those names to table3, you write this:

    Code:
    INSERT INTO table3 (imagename)
    SELECT table1.imagename
    FROM table1 LEFT JOIN table2 on table1.imagename = table2.imagename 
    WHERE table2.imagename IS NULL
    and the code you would write would probably be this:

    Code:
    Public Function findOldImages()
      currentdb.execute(sql statement from above)
    End Function
    So yeah, a one-line function is probably what you're looking for!

  3. #3
    jscriptor09 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    60
    Hi Adam
    Thaks for showing the Join approach. I tried it and it seems to take a long time as well, maybe as long as the looping. I have 8000 records in Table 1, comparing to ~100,000 records in table 2, if a record is not found put in Table 3.

    Thanks

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Can you show the table structures for
    DirImagesFileList and imagetable?
    Do you have any indexes on these tables?

  5. #5
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by orange View Post
    Can you show the table structures for
    DirImagesFileList and imagetable?
    Do you have any indexes on these tables?
    indexes do help with speed, but not much.

    Queries are always better to use than recordsets for 2 reasons:

    1) less coding.
    2) less risk of human error.

    good luck to ya.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Can't get the logic right.
    By Nouri31 in forum Queries
    Replies: 3
    Last Post: 03-31-2011, 08:25 AM
  2. Relationship Logic
    By Huddle in forum Access
    Replies: 2
    Last Post: 01-25-2011, 04:27 PM
  3. Help a noob (calculating fields and logic)
    By unnamedplayer in forum Reports
    Replies: 9
    Last Post: 07-16-2010, 10:28 AM
  4. Need help with code logic/consolidation
    By bg18461 in forum Programming
    Replies: 1
    Last Post: 03-31-2010, 04:19 PM
  5. Split database logic
    By Overdive in forum Database Design
    Replies: 3
    Last Post: 02-05-2010, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums