Results 1 to 5 of 5
  1. #1
    kaelcarp is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    3

    Create a variable from a query created in VBA

    I would like to create a dynamic query using VBA and use the query's result as a variable. I have done this before, without using dlookup (I think it's too complex for dlookup), but I cannot seem to remember how.

    What I'd essentially like to be able to do is create a query:



    docmd.runsql("SELECT x from y where z;") [note: actual query will be a lot more complex than that and include subqueries]

    ...and use the result of that as a variable. I don't want to create permanent queries because I'd like to avoid the clutter that would come from that.

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You would open a recordset on the SQL and get the value from that. You should know that a saved query gets compiled and will run faster than one created in code. I generally only create SQL in code when the actual structure of it will change based on user input. IMHO, you're trading "clutter" for maintainability. It would be easier to create and maintain a saved query.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    kaelcarp is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    3
    I probably will need to be able to have the structure of the query change based on user input. We're still in the planning phase on this and I'm trying to think ahead by asking here, which is why I didn't post in more detail. There will definitely be dynamic elements to the query.

    What do you mean by opening a recordset on the SQL? Sorry, I'm not particularly advanced with this. I'm not sure exactly how to do that.

  4. #4
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    A Recordset is basically an Array with the Table (or Query) data pre-entered in it.

    You'd use a Recordset like this:
    Code:
      ' Create an empty Recordset that you can use
      Dim rstTableName as Recordset
    
      ' Grab the results of our Query into the Recordset variable
      Set rstTableName = CurrentDb().OpenRecordset("SELECT x FROM y WHERE [x]='z', dbOpenDynaset)
    
      ' Set the Form Element Me!Text0 to the contents of Field x in Table y
      Me!Text0 = rstTableName("x") ' Me!Text0 now equals "z"!
    If your Table/Query results (the Recordset) have more than just a single Record in them, you can move back and forth through it by using the following builtin Methods:
    rstTableName.MoveFirst = Go to the first Record in the Recordset
    .MoveLast = Go to the last Record
    .MoveNext = Go to the next Record
    .MovePrevious = Go to the previous Record
    .RecordCount = The number of Records in the Recordset (to be sure this number is accurate, you will want to do a MoveLast before checking this. That will force Access to actually load the whole Recordset)
    .EOF = Are you at the end of the Recordset (no more Records!)
    .BOF = Are you at the beginning of the Recordset (BEFORE the first record)
    Last edited by Rawb; 06-10-2010 at 11:17 AM. Reason: Added stuffz!

  5. #5
    kaelcarp is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    3
    Thank you. That was immensely helpful. Much appreciated.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using a field as a variable in a query
    By Shadow2K9 in forum Queries
    Replies: 1
    Last Post: 06-10-2010, 07:38 PM
  2. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM
  3. Replies: 7
    Last Post: 02-25-2010, 12:32 PM
  4. New Field Name on Newly created query
    By inan25 in forum Queries
    Replies: 3
    Last Post: 05-13-2009, 09:05 PM
  5. Passing a value from a variable to an update query
    By MUKUDU99 in forum Programming
    Replies: 0
    Last Post: 08-24-2008, 11:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums