Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: Solved: Excel Code Efficiency with DB Connection

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location

    Solved: Excel Code Efficiency with DB Connection

    I already have a MS Access DB with queries stored within it. As of now with my code within Excel, I'm creating an ado connection, creating a recordset, and using a SQL string to populate the recordset. Is there anyway possible to just run the query already saved in Access to populate a recordset in Excel. I find that my excel code is beginning to look sloppy with all this SQL code thrown in there.

  2. #2
    VBAX Regular Ebrow's Avatar
    Joined
    May 2007
    Posts
    67
    Location

    Give this a shot.

    Hi.

    Try this out.

    [VBA]
    Sub runQueryFromDB()
    'Add the reference to Microsoft Access 11.0 Lib.
    'Add the DAO 3.6 reference
    Dim myAccess As Access.Application
    Dim myDB As Database
    Dim myRS As Recordset
    Set myAccess = CreateObject("Access.Application")
    myAccess.Visible = True 'if you don't run this the database will run in the background hidden
    myAccess.OpenCurrentDatabase ("C:\Test\Test.mdb")
    'open access query normally
    myAccess.DoCmd.OpenQuery ("qryQuery1")
    'create recordset
    Set myDB = myAccess.CurrentDb()
    Set myRecordset = myDB.OpenRecordset("qryQuery1")
    End Sub
    [/VBA]
    Nothing is impossible, just it hasn't been thought of yet.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why? Looks sloppy? Works well? Where are your priorities?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    Quote Originally Posted by xld
    Why? Looks sloppy? Works well? Where are your priorities?
    Im just not sure on the best way of doing it. I already have the queries stored in Access. I just want to know which way was more efficient. Opening an Access object and executing the query or just copying the SQL from access and using it in excel.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you have the stored queries in Access, I would suggest that is the way to go.

    Even better would be to write a nice Query class to access the queries.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    So try Ebrows method? That would make my VBA look a lot less cluttered.

  8. #8
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    [vba]Public Sub runQueryFromDB(query As String, ByRef myRS As Recordset)
    'Add the reference to Microsoft Access 11.0 Lib.
    'Add the DAO 3.6 reference
    Dim myAccess As Access.Application
    Dim myDB As Database


    Set myAccess = CreateObject("Access.Application")
    myAccess.Visible = False 'if you don't run this the database will run in the background hidden
    myAccess.OpenCurrentDatabase ("C:\Documents and Settings\IntTesting\IA Testing.mdb")
    'open access query normally
    myAccess.DoCmd.OpenQuery (query)
    'create recordset
    Set myDB = myAccess.CurrentDb()
    Set myRS = myDB.OpenRecordset(query)

    End Sub[/vba]
    im getting a type mismatch error in the last line. myRS is delcared as a Recordset. Does myDB.OpenRecordset(query) return something else besides a recordset?
    Last edited by taporctv; 07-12-2007 at 09:00 AM.

  9. #9
    VBAX Regular Ebrow's Avatar
    Joined
    May 2007
    Posts
    67
    Location

    Smile Try this out.

    [VBA]
    Function runQueryFromDB(query As String) as recordset 'Add the reference to Microsoft Access 11.0 Lib. 'Add the DAO 3.6 reference Dim myAccess As Access.Application Dim myDB As Database Set myAccess = CreateObject("Access.Application") myAccess.Visible = False 'if you don't run this the database will run in the background hidden myAccess.OpenCurrentDatabase ("C:\Documents and Settings\IntTesting\IA Testing.mdb") 'create recordset Set myDB = myAccess.CurrentDb() Set runQueryFromDB = myDB.OpenRecordset(query) End FunctionSub TestDim myRS as recordsetSet myRS = runQueryFromDB(?qryQuery1?) End sub

    [/VBA]

    Are you trying to do this??

    I don't understand why you where passing the recordset variable in and then set it on the sub. I am guessing you want to be able to set a variable else were in another sub by using the function.
    Nothing is impossible, just it hasn't been thought of yet.

  10. #10
    VBAX Regular Ebrow's Avatar
    Joined
    May 2007
    Posts
    67
    Location
    That went wierd. Sorry here is the code:

    [VBA]
    Function runQueryFromDB(query As String) as recordset

    'Add the reference to Microsoft Access 11.0 Lib.
    'Add the DAO 3.6 reference

    Dim myAccess As Access.Application
    Dim myDB As Database

    Set myAccess = CreateObject("Access.Application")
    myAccess.Visible = False 'if you don't run this the database will run in the background hidden
    myAccess.OpenCurrentDatabase ("C:\Documents and Settings\IntTesting\IA Testing.mdb")

    'create recordset
    Set myDB = myAccess.CurrentDb()
    Set runQueryFromDB = myDB.OpenRecordset(query)

    End Function


    Sub Test

    Dim myRS as recordset
    Set myRS = runQueryFromDB(“qryQuery1”)

    End sub
    [/VBA]
    Nothing is impossible, just it hasn't been thought of yet.

  11. #11
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    Im still getting a type mismatch at this line within the function

    [VBA]Set runQueryFromDB = myDB.OpenRecordset(query) [/VBA]

    I have no idea what I could be doing wrong.

  12. #12
    VBAX Regular Ebrow's Avatar
    Joined
    May 2007
    Posts
    67
    Location
    Have you set the DAO 3.5 reference?

    query should be in quotation marks and the query name, so I am guessing that you are using query as a variable then?

    Try this code instead:-

    [VBA]
    Sub Test

    Dim myRS As DAO.recordset
    Set myRS = runQueryFromDB(Query)

    End Sub
    [/VBA]
    Nothing is impossible, just it hasn't been thought of yet.

  13. #13
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    Thanks. That helped. I also had to add that to the function.

    Function runQueryFromDB(query As String) As dao.recordset

    My only problem is Access still opens regardless of the fact I set visible to false.

  14. #14
    VBAX Regular
    Joined
    Jul 2007
    Posts
    71
    Location
    Sometimes with Access I've seen issues where you have to explicitly reference the object - i.e.:

    Access.Application.Visible = True

  15. #15
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    Access keeps opening up and not closing despite the code I have. Is there anything that i'm missing?

    [vba]Public Sub runQueryFromDB(query As String, myRS As DAO.Recordset)

    'Add the reference to Microsoft Access 11.0 Lib.
    'Add the DAO 3.6 reference
    Dim myAccess As access.Application
    Dim myDB As Database
    Set myAccess = CreateObject("Access.Application")


    'myAccess.Application.Visible = False 'if you don't run this the database will run in the background hidden
    myAccess.OpenCurrentDatabase ("C:\Documents and Settings\IntTesting\IA Testing.mdb")
    'open access query normally
    myAccess.DoCmd.OpenQuery (query)
    'create recordset
    Set myDB = myAccess.CurrentDb()
    Set myRS = myDB.OpenRecordset(query)
    myAccess.Application.Quit
    Set myAccess = Nothing

    End Sub[/vba]

    When I delete the sheet where the tables were loaded, then access will close.
    Last edited by taporctv; 07-16-2007 at 06:00 AM.

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just try

    myAccess.Quit
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  17. #17
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    I tried that but its not working. I dont get it. I have another module that opens access and closes access and all i used there was myaccess.quit. I dont understand why its not working in my current module.

  18. #18
    VBAX Regular
    Joined
    Aug 2004
    Location
    London, England
    Posts
    52
    Location
    Can't you just use your existing method posted at the top of this topic, but pass the name of a query in the db rather than a sql string when you open the recordset? Surely there's no need to actually automate an instance of Access as that would seem to be an unnecessary overhead.

  19. #19
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    Quote Originally Posted by alimcpill
    Can't you just use your existing method posted at the top of this topic, but pass the name of a query in the db rather than a sql string when you open the recordset? Surely there's no need to actually automate an instance of Access as that would seem to be an unnecessary overhead.
    I dont know if that's possible. If it is, do you know how to implement that in code.

  20. #20
    VBAX Regular
    Joined
    Aug 2004
    Location
    London, England
    Posts
    52
    Location
    Well as I say, it sounds like you are pretty close to it already from what you said in the first post, but say you had an open connection to an access DB called 'conn' and new recordset object called 'rs', then to run some sql and put the results in the recordset you'd presumably use something like

    [vba]rs.Open "SELECT * FROM MyTable", conn[/vba]

    However, if you've got a query in your database called 'MyQuery' which returns the results you want, and you don't want to copy the sql across to your vba code (and why would you), you just go

    [vba]rs.Open "MyQuery", conn[/vba]

    and it should work. There's various ways to do this, but this sounds closest to what you describe at the top. If you want you can specify that you are passing a query name rather than a sql string, but there's no need to as ADO will work it out at runtime. It might improve performance a tiny bit, but for a single shot like this it'd be negligible.

Posting Permissions

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