Check Access table(s) from Excel using Automation - Updated Code


January 31, 2008 – 3:01 pm by JP

Here is the revised and completed code that I posted yesterday. The DAO declarations are moved outside the Sub procedure and the 'Set' statements are outside the function loop. Also, the table & column headers are now passed as arguments to the function, making the code more compact. The speed increase over yesterday's code is significant.

VBA:
  1. Option Explicit
  2. Dim DAODB As DAO.Database
  3. Dim DAORS As DAO.Recordset
  4. Dim objDBEngine As DAO.DBEngine
  5. Dim objWSP As DAO.Workspace
  6.  
  7. Sub DDFileReconcile
  8. '
  9. ' Macro to search Access db "dB" for values in an Excel worksheet
  10. ' Values/text should be in column B, starting in cell B2, result of search is placed in H2
  11. '
  12. Application.ScreenUpdating = False
  13. Application.Calculation = xlCalculationManual
  14.  
  15. If Dir("C:\dB.ldb") = "" Then
  16. ' If a .mdb file is open, a matching .ldb file with the same
  17. ' name is opened in the same directory
  18.   Dim CheckRng As Excel.Range
  19.   Dim cell As Excel.Range
  20.  
  21.   ActiveSheet.UsedRange
  22.  
  23.   Set CheckRng = Range("B2", Range("B65536").End(xlUp))
  24.   Set objDBEngine = New DAO.DBEngine
  25.   Set objWSP = objDBEngine.Workspaces(0)
  26.   Set DAODB = objWSP.OpenDatabase("C:\dB.ldb")
  27.  
  28.   For Each cell In CheckRng
  29.     If MatchAccessTables(cell.Value, "table 1", "Indexed Column Header 1") Then
  30.       cell.Offset(0, 6).Value = "Found"
  31.     ElseIf MatchAccessTables(cell.Value, "table 2", "Indexed Column Header 2") Then
  32.       cell.Offset(0, 6).Value = "Found"
  33.     ElseIf MatchAccessTables(cell.Value, "table 3", "Indexed Column Header 3") Then
  34.       cell.Offset(0, 6).Value = "Found"
  35.     Else
  36.       cell.Offset(0, 6).Value = "Not Found"
  37.     End If
  38.   Next cell
  39. Else
  40.   MsgBox ("Database file appears to be locked. Please try again later."), vbCritical
  41.   GoTo ExitProc
  42. End If
  43.  
  44. ExitProc:
  45. Set objDBEngine = Nothing
  46. Set objWSP = Nothing
  47. Set DAODB = Nothing
  48. Set DAORS = Nothing
  49. Set CheckRng = Nothing
  50. Application.ScreenUpdating = True
  51. Application.Calculation = xlCalculationAutomatic
  52. End Sub

VBA:
  1. Function MatchAccessTables(cell As String, TableName As String, ColToSearch As String) As Boolean
  2.  
  3. MatchAccessTables = False
  4.  
  5. Set DAORS = DAODB.OpenRecordset(TableName, dbOpenTable)
  6. DAORS.Index = ColToSearch
  7. DAORS.Seek "=", cell
  8.  
  9. If DAORS.NoMatch = False Then
  10.   MatchAccessTables = True
  11. End If
  12.  
  13. End Function

Enjoy,
JP


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 24 views since January 31, 2008 – 3:01 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:


« Check Access table(s) from Excel using Automation || VBA Macro to Remove Stationery from Email Message »