+ Reply to Thread
Results 1 to 1 of 1

UDF to return value from Access Database

Hybrid View

  1. #1
    Registered User
    Join Date
    09-28-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    1

    UDF to return value from Access Database

    Hey,

    I created a UDF in VBA that calls a value from an Access Database. It works, but it is absurdly SLOW. It takes like 20 secs to get 1 cell of data, and if you try to call >5 cells it will lock up excel. Does anyone have any ideas on how to make this faster and thus actually usable? Code below:

    Function GetNumber(ID As String, Month As Date)
    
        Dim db As ADODB.Connection
        Dim rst As ADODB.Recordset
        Dim strSQL As String
    
    
    strSQL = "SELECT Number FROM Table1 " & _
            "WHERE [PersonID] = ('" & ID & "') AND Date = (#" & Month & "#);"
    
       
    Set db = New ADODB.Connection
    file = "C:\Database.accdb"
      
    With db
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Open file
    End With
     
     
    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseClient
    rst.Open Source:=strSQL, ActiveConnection:=db, CursorType:=adOpenForwardOnly, _
        LockType:=adLockReadOnly
    
    GetNumber = rst.Fields("Number").Value
    
    rst.Close
    db.Close
    Set rst = Nothing
    Set db = Nothing
    
    End Function
    Thanks!


    Moderator Edit:

    Welcome to the forum, sike64.

    Please notice that [CODE] tags have been added to your post. The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at the top of the page and read Rule #3.
    Thanks.
    Last edited by Cutter; 10-17-2012 at 07:19 PM. Reason: Added code tags

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1