Results 1 to 11 of 11

Thread: [RESOLVED] VBA and Executing SQL Statements

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved [RESOLVED] VBA and Executing SQL Statements

    Quick question... what do you need for connection info to execute an sql statement from within Access VBA? Can I do it just like using ADO in VB?

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Executing SQL Statements

    Yes, you can use ADO in Access VBA, pretty much exactly as in VB.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: VBA and Executing SQL Statements

    what would I use for the connection? Is there a variable to automatically connect to the current DB?

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Executing SQL Statements

    First off. you will need to add a reference to the Microsoft ActiveX Data Objects library.

    To create recordsets from the current DB you would use the following syntax

    VB Code:
    1. Dim rsMyRecordSet As ADODB.Recordset
    2. Dim sSQL As String
    3.  
    4.     Set rsMyRecordSet = New ADODB.Recordset
    5.     sSQL = "SELECT field FROM table....etc"
    6.     rsMyRecordSet.Open sSQL, CurrentProject.Connection

    the "CurrentProject.Connection" in the above connects to the db in which your code resides.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA and Executing SQL Statements

    You can use this to execute a sql string.
    VB Code:
    1. Public Function ExecuteMe(ByVal sSQL As String)
    2.     Application.CurrentDb.Connection.Execute sSQL
    3. End Function
    With a few changes you can make it a Command Object too.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA and Executing SQL Statements

    And this one to open a recordset
    VB Code:
    1. Public Function OpenMe(ByVal sSQL As String) As Recordset
    2.     Dim rs As Recordset
    3.     Set rs = Application.CurrentDb.OpenRecordset(sSQL, dbOpenDynamic, , dbOptimistic)
    4.     OpenMe = rs
    5. End Function
    Sorry for the delay, had a phone call.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: VBA and Executing SQL Statements

    Ok I have tried all 3 of you guys' suggestions. However I got errors on all of them. The last one I tried was

    VB Code:
    1. Dim rs As Recordset
    2. Set rs = Application.CurrentDb.OpenRecordset(sSQL, dbOpenDynamic, , dbOptimistic)
    3. OpenMe = rs

    I would like to open it as a recordset but I got an error on the Set statement:

    Invalid Arguement

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Executing SQL Statements

    1/ Have you added a reference to the Microsoft ActiveX Data Objects library?

    2/ Change
    Dim rs as Recordset
    to
    Dim rs as ADODB.Recordset
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: VBA and Executing SQL Statements

    Yes I have referenced Microsoft ActiveX Data Objects 2.1 Library and this is the exact code I am using:

    Dim rs As ADODB.Recordset
    Dim stSQL As String
    stSQL = "SELECT * FROM PIU_Percentages"

    Set rs = Application.CurrentDb.OpenRecordset(stSQL, dbOpenDynamic, , dbOptimistic)
    OpenMe = rs

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: VBA and Executing SQL Statements

    Got this to work:

    Dim rs As ADODB.Recordset
    Dim stSQL As String
    stSQL = "SELECT * FROM PIU_Percentages"


    Set rs = New ADODB.Recordset

    rs.Open stSQL, CurrentProject.Connection


    Dunno why it didn't work the first time I tried it but thanks for your help guys!

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] VBA and Executing SQL Statements

    Try the following...

    VB Code:
    1. Sub test()
    2. Dim rs As ADODB.Recordset
    3. Dim sSQL As String
    4.  
    5.     Set rs = New ADODB.Recordset
    6.    
    7.     sSQL = "SELECT * FROM PIU_Percentages"""
    8.    
    9.     rs.Open Source:=sSQL, ActiveConnection:=CurrentProject.Connection, _
    10.             CursorType:=adOpenDynamic, LockType:=adLockOptimistic
    11.    
    12.     rs.MoveFirst
    13.     MsgBox rs.Fields(0).Value
    14.    
    15.     rs.Close
    16.     Set rs = Nothing
    17. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width