Results 1 to 10 of 10

Thread: Dynamic Query list in Access

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58

    Dynamic Query list in Access

    Hi All,

    I am trying to find a method by which I can create an Access form that will list all of the queries that I have created. I then need the user to be able to select the query and run it.

    Does anyone have any ideas, coz I haven't got a clue with this one!

    Cheers

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Try enumerating the QueryDefs colection excluding the system
    and hidden queries, if any. Populate in a listbox or combo, or ???
    On the click event of whatever object you use, DoCmd.OpenQuery or such.

    HTH
    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

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    That sounds perfect! I am having problems with getting any queryDef stuff off the ground. When I run the code, VB doesn't know what a queryDef is. Does anyone know what I need to install or reference to get these working?

    I have looked on MSDN but I cannot find anything that points me in the right direction.

    Cheers

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    Found it!

    Microsoft DAO 3.0 Object Library

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    OK, I have got my head around query defs, but I haven't found anything to do with a queryDef collection?

    Can anyone point me in the direction of how to access the query def collection?

    Cheers

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    OK, I can list the queries and put them in a combo box:
    VB Code:
    1. Public Function listQuery() As Boolean
    2. Dim Arlen As Integer
    3. Dim names() As String
    4.  
    5. Set Db = CurrentDb
    6.  
    7. If Db.QueryDefs.Count <> 0 Then
    8.     Arlen = Db.QueryDefs.Count
    9.     ReDim names(0 To Arlen - 1)
    10.     i = 0
    11.     For i = 0 To (Arlen) - 1
    12.         names(i) = Db.QueryDefs(i).name
    13.     Next i
    14. End If
    15.  
    16. For i = 0 To (Arlen) - 1
    17.     If InStr(names(i), "Output") Then
    18.         cboQueryList.AddItem (names(i))
    19.     End If
    20. Next i
    21.  
    22. End Function

    Really easy question, but I just can't remember the answer.....how do you run a query?

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    If I try to execute the query using the method below, I get an error message saying that you cannot run a select query!

    Any ideas?

    VB Code:
    1. Dim qdf As QueryDef
    2.     Set qdf = CurrentDb.QueryDefs(cboQueryList.Text)
    3.     qdf.Execute
    4.     qdf.Close

  8. #8
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    You know the name of the query, so in a display form (which opens up) set the datasource to the query name.

    This should work for a list box or a full form. Your choice how you implement it.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    Cheers for the idea Vince, but I have implemented a DoCmd...slightly nasty I know, but it needn't be anything too special.

    Thanks for the tip though.

    Does anyone know how to delete a query?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    To drop an Access query using ADO...
    VB Code:
    1. sSQL = "DROP VIEW qryIDCount;"
    2. Cnn.Execute sSQL
    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

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