Results 1 to 5 of 5
  1. #1
    ConfusedMike is offline Novice
    Windows XP Access 2002
    Join Date
    Sep 2011
    Posts
    10

    Angry Use variable in a Union Query

    Hi,



    I am using a Union Query to join the contents of Table1 with a variable number of blank rows extracted from Table2 to create a table which will then be used in a fixed length report. I have used the following code which works by finding the number of rows in Table1 and then subtracting that value from 10 (the total number of rows I require):

    SELECT Table1.AID, Table1.A, Table1.A1, Table1.A2 FROM Table1
    UNION SELECT Table2.Row, Table2.B, Table2.B1, Table2.B2 FROM Table2 WHERE ((Table2.ARow)<=(10-(Select Count(*) As RowsInTable1 From Table1)));


    To make it easier to read in the future I would like to use variables such as 'TotalNumberOfRows' instead of '10' and 'RowsInTable1' instead of 'Select Count(*) As RowsInTable1 From Table1'

    All attempts to define the varialble within the query result in a syntax error and I am not sure how to define them outside the query and then reference them in the where statement.

    Can anyone help me with this
    Last edited by ConfusedMike; 09-11-2011 at 11:45 PM. Reason: spelling mistake

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    An Access query can call a function or refer to a control on a form or report or an input parameter prompt can get a value.

    A query in VBA code can concatenate a variable.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ConfusedMike is offline Novice
    Windows XP Access 2002
    Join Date
    Sep 2011
    Posts
    10
    Thanks for you help so far but I'm not there yet.

    I created a function as follows:

    Private Function Rows() as Integer
    Rows = DCount("*","Table1")
    End Function

    I know that the function works as I can output the answer to a msgbox but when I substite (10-Rows()) into the where statement instead of (10-(Select Count...... I get the following error 'Undefined function 'Rows' in expression'

    What am I doing wrong?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Has to be a Public function in a general module.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ConfusedMike is offline Novice
    Windows XP Access 2002
    Join Date
    Sep 2011
    Posts
    10
    Hi June7,

    Thanks, it works now. Its so simple when you know how!!!!!!!

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

Similar Threads

  1. Help with a Union Query
    By Bear in forum Queries
    Replies: 12
    Last Post: 08-14-2011, 05:12 PM
  2. Union Query
    By jlclark4 in forum Queries
    Replies: 3
    Last Post: 02-25-2011, 08:21 PM
  3. Union Query Help
    By jo15765 in forum Queries
    Replies: 7
    Last Post: 01-06-2011, 05:46 PM
  4. Please help me out with union query
    By radicrains in forum Access
    Replies: 1
    Last Post: 10-29-2010, 01:48 AM
  5. Need help with a Union Query
    By jdowdy in forum Access
    Replies: 1
    Last Post: 10-13-2009, 05:24 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