Results 1 to 8 of 8

Thread: SQL Query and Access

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    50

    SQL Query and Access

    Hi Guys,

    I want to use VBA to execute a query and return the results just like a normal Access query (ie: in a datasheet type view). How can I do this?

    Shane

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: SQL Query and Access

    Same way as you would do it in VB.

    Create a connection to the database using ADODB and execute the query.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Re: SQL Query and Access

    Search in google about ado. There are loads of code samples abd tutorials.

    or

    This should be helpful:
    - http://msdn.microsoft.com/library/de...tlibraries.asp

    hope this help,

    regards,
    sweet_dreams
    using VB 2010 .NET Framework 4.0; MS Office 2010; SQL Server 2008 R2 Express Edition | Remember to mark resolved threads and rate useful posts.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: SQL Query and Access

    DAO (fastest for Access only operations):
    DoCmd.RunSql "SELECT * FROM Foo"

    or if it's a saved query in Access

    DoCmd.OpenQuery "qryFoo"

    ADO is different, uses .Execute, etc.
    Tengo mas preguntas que contestas

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    50

    Re: SQL Query and Access

    DoCmd.RunSQL "SELECT * FROM mwo2005 WHERE Method='LIQUID'"

    Gives error:

    A RunSQL action requires an argument consisting of an SQL statement.

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    50

    Re: SQL Query and Access

    I have discovered that DoCmd.RunSQL cannot be select statements but havent found a way to extract information and display it in the same way as a saved query. Also, how do I reference a field value on a form?

    Ie: frmMain.txtType.Text? for SELECT * FROM mwo2005 WHERE Method='" & ... & "'"

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

    Re: SQL Query and Access

    You can access control values like so.
    VB Code:
    1. SELECT * FROM mwo2005 WHERE Method='" & Forms![Form1].txtType.Value & "'"
    Only these types of queries can be run with RunSQL...
    Code:
    Query type     SQL statement 
    Action  
    ---------------------------
    Append         INSERT INTO 
    Delete         DELETE 
    Make-table     SELECT...INTO 
    Update         UPDATE 
    
    Data-definition (SQL-specific)  
    Create a table    CREATE TABLE 
    Alter a table     ALTER TABLE 
    Delete a table    DROP TABLE 
    Create an index   CREATE INDEX 
    Delete an index   DROP INDEX
    To run a basic SELECT SQL statement you will need to use the .OpenQuery function of a saved query.
    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

  8. #8
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: SQL Query and Access

    Duh! I actually knew that and just wasn't thinking. Sorry for the misinfo, shane.
    Tengo mas preguntas que contestas

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