Results 1 to 4 of 4
  1. #1
    lmh24 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    2

    Run query for current record only

    Hello,

    I'm new to this forum and re-familiarizing myself with Access, which I haven't used in several years. I am designing a survey questionnaire in Access which collects information from a randomly selected adult in a household along with basic information about each member of that household.

    In order to identify the randomly selected adult I have a make-table query that selects the name (hhname) and household member number (hhmemno) into a table (tblBdayName) for the adult who last had a birthday, i.e. minimum number of days from now() to day/month of birth.



    The query is called in the OnOpen property of one of my forms (frmAdult_Migration), and it runs without throwing errors. However, it currently loops through all of the data in the database, and I need it to run ONLY for the record that it's currently in, i.e. I need it to loop through only the records for the household members included in that particular household and pull out the name of the person IN THAT HOUSEHOLD who last had a birthday.

    My primary key is FILE -- a unique file number for each household, the table in which the household member data are stored is tblhhroster_subform, which is the table for the subform frmhhroster_subform. The main or parent table/form are tblhhroster and frmhhroster. The main form has a field for indicating how many people reside in the household, and the subform then collects info about each of those people.

    I have a WHERE clause in my query to read as follows: WHERE "file = frmhhroster!frmhhroster_subform!file".
    As I mentioned, the query runs with this designation, but not for that current record (file) only. I have tried writing the syntax several different ways, and this is the only way it runs without prompting a message requiring me to fill in the current file number.

    I would be very grateful if anyone could tell me how to run this query for ONLY the current record. My SQL code for the query is below if that helps. Thanks!

    SELECT hhname, file, hhmemno INTO BdayNameFROM (SELECT file, hhmemno, hhname, dob, IIf(DatePart("y",Now())-DatePart("y",Str(DatePart("d",dob))+" "+Monthname(Month(dob))+" "+Str(Year(Now())))>=0,(DatePart("y",Now())-DatePart("y",Str(DatePart("d",dob))+" "+Monthname(Month(dob))+Str(Year(Now())))),(DatePa rt("y","31 December "+Str(Year(Now())-1))-DatePart("y",Str(DatePart("d",dob))+" "+Monthname(Month(dob))+" "+Str(Year(Now())-1)))+DatePart("y",Now())) AS numdays FROM hhroster_subform WHERE "file=Forms!hhroster!Household_Roster!file" And DateAdd("yyyy",18,dob)<=Now()) AS A INNER JOIN (SELECT MIN(IIf(DatePart("y",Now())-DatePart("y",Str(DatePart("d",dob))+" "+Monthname(Month(dob))+" "+Str(Year(Now())))>=0,(DatePart("y",Now())-DatePart("y",Str(DatePart("d",dob))+" "+Monthname(Month(dob))+Str(Year(Now())))),(DatePa rt("y","31 December "+Str(Year(Now())-1))-DatePart("y",Str(DatePart("d",dob))+" "+Monthname(Month(dob))+" "+Str(Year(Now())-1)))+DatePart("y",Now()))) AS MinimumDays FROM hhroster_subform WHERE "file=Forms!hhroster!Household_Roster!file" And DateAdd("yyyy",18,dob)<=Now()) AS T ON T.MinimumDays=A.numdays;
    Last edited by June7; 06-01-2012 at 02:46 PM. Reason: code tags made the query harder to read

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    Why is criteria within quotes?
    WHERE "file=Forms!hhroster!Household_Roster!file"

    Instead try:
    WHERE [file]=Forms!hhroster!Household_Roster!file
    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
    lmh24 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    2
    Quote Originally Posted by June7 View Post
    Why is criteria within quotes?
    WHERE "file=Forms!hhroster!Household_Roster!file"

    Instead try:
    WHERE [file]=Forms!hhroster!Household_Roster!file
    Thank you -- I've tried that, but I get an "Enter parameter value" message prompting me to enter a value for file when I have it set up like that. If I enter the current file number it will work, but I need it to work without me having to enter the file number each time.

    The quotes are there because that was the only way I could get it to run without throwing the enter parameter value message. But it loops through the entire data set, not just the data for the current record.

    Any other suggestions?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    The parameter prompt pops up because Access can't find the referenced item.

    Want to provide project for analysis? Follow instructions at bottom of my post. Identify objects involved in the issue.
    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.

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

Similar Threads

  1. Current Record Value in query
    By A S MANN in forum Queries
    Replies: 3
    Last Post: 12-21-2011, 06:21 AM
  2. Replies: 3
    Last Post: 09-18-2011, 03:46 PM
  3. Run Update Query on Current Record in Form
    By c3pse in forum Queries
    Replies: 3
    Last Post: 08-14-2010, 05:40 AM
  4. Query based on current record in form
    By bhsvendbo in forum Queries
    Replies: 1
    Last Post: 06-01-2010, 11:20 AM
  5. How to output only current record in query
    By mslieder in forum Access
    Replies: 0
    Last Post: 01-20-2006, 05:48 PM

Tags for this Thread

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