Check Access table(s) from Excel using Automation


January 30, 2008 – 8:43 pm by JP

Finally, with some help from the good folks over in the microsoft.public.access.modulesdaovba newsgroup, I was able to complete my code to search an Access database for some information stored in an Excel worksheet.

This code will cycle through a list of numbers in column B, starting in cell B2, then check three tables in an Access file for a matching record. If it finds a match in the first table, it exits, otherwise it checks the second, then the third table (no need to keep going if we find a match right away). It prints "Found" or "Not Found", as appropriate, in the corresponding cell in column H. This is to accomodate my specific needs for this project at my office, but you could easily adjust this code to search for text or numbers in the Access db of your choice and put the response into the next column (instead of 6 columns away).

I got a pretty rough education in Access VBA coding; it took only a few minutes to write the code, but hours to search for the proper syntax to access the correct objects.

First you need to open each table and index the column you want to search. This is the "Column Header #" that contains the values you want to search through. Click on the table and go to 'Design View'. Select the field you want to index, and in the box at the bottom, click in the dropdown next to 'Index' and choose 'Yes (duplicates OK)'. Save and close and now you can use the Index property of the Recordset Object to specify that column to search for your values.

VBA:
  1. Sub FileCheck()
  2. '
  3. ' Macro to search a column in an Access db table for text/values in column B, starting in cell B2
  4. '
  5. Application.ScreenUpdating = False
  6. Application.Calculation = xlCalculationManual
  7.  
  8. If Dir("C:\Folder\database.ldb") = "" Then
  9.  ' If a .mdb file is open, a matching .ldb file with the same name is opened in the same directory
  10.  Dim CheckRng As Excel.Range
  11.  Dim cell As Excel.RangeActiveSheet.UsedRange
  12.  
  13.  Set CheckRng = Range("B2", Range("B65536").End(xlUp))
  14.  
  15.  For Each cell In CheckRng
  16.  
  17.    If MatchAccessTables(cell.Value) Then
  18.      cell.Offset(0, 6).Value = "Found"
  19.    Else
  20.      cell.Offset(0, 6).Value = "Not Found"
  21.    End If
  22.  
  23.  Next cell
  24.  
  25. Else
  26. MsgBox ("Database file appears to be locked. Please try again later."), vbCritical
  27. GoTo ExitProc
  28. End If
  29.  
  30. ExitProc:
  31. Set objDBEngine = Nothing
  32. Set objWSP = Nothing
  33. Set DAODB = Nothing
  34. Set DAORS = Nothing
  35. Set DAOTBL = Nothing
  36. Set CheckRng = Nothing
  37. Application.ScreenUpdating = True
  38. Application.Calculation = xlCalculationAutomatic
  39. End Sub

VBA:
  1. Function MatchAccessTables(cell As String) As Boolean
  2.  
  3. MatchAccessTables = False
  4.  
  5. Dim DAODB As DAO.Database
  6. Dim DAORS As DAO.Recordset
  7. Dim DAOTBL As DAO.TableDef
  8. Dim objDBEngine As DAO.DBEngine
  9. Dim objWSP As DAO.Workspace
  10.  
  11. Set objDBEngine = New DAO.DBEngine
  12. Set objWSP = objDBEngine.Workspaces(0)
  13. Set DAODB = objWSP.OpenDatabase("C:\Folder\database.mdb ")
  14.  
  15. Set DAORS = DAODB.OpenRecordset("Table 1", dbOpenTable)
  16. Set DAOTBL = DAODB.TableDefs("Table 1")
  17. Set DAORS = DAOTBL.OpenRecordset(dbOpenTable)
  18. DAORS.Index = "Column Header 1"
  19. DAORS.Seek "=", cell
  20.  
  21. If DAORS.NoMatch = False Then
  22.   MatchAccessTables = True
  23.   Exit Function
  24. Else
  25.   Set DAORS = DAODB.OpenRecordset("Table 2", dbOpenTable)
  26.   Set DAOTBL = DAODB.TableDefs("Table 2")
  27.   Set DAORS = DAOTBL.OpenRecordset(dbOpenTable)
  28.   DAORS.Index = "Column Header 2"
  29.   DAORS.Seek "=", cell
  30. End If
  31.  
  32. If DAORS.NoMatch = False Then
  33.   MatchAccessTables = True
  34.   Exit Function
  35. Else
  36.   Set DAORS = DAODB.OpenRecordset("Table 3", dbOpenTable)
  37.   Set DAOTBL = DAODB.TableDefs("Table 3")
  38.   Set DAORS = DAOTBL.OpenRecordset(dbOpenTable)
  39.   DAORS.Index = "Column Header 3"
  40.   DAORS.Seek "=", cell
  41. End If
  42.  
  43. If DAORS.NoMatch = False Then
  44.   MatchAccessTables = True
  45. End If
  46.  
  47. End Function

Make sure you set a reference to 'Microsoft DAO 3.6 Object Library' before using this code.

Enjoy,
JP

Acknowledgments: microsoft.public.access.modulesdaovba


Share and Enjoy:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Access, Excel, VBA
Tags: , , , ,

This post has 44 views since January 30, 2008 – 8:43 pm.

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Counting Unique Occurrences in an Excel Spreadsheet || Check Access table(s) from Excel using Automation - Updated Code »