Results 1 to 6 of 6
  1. #1
    jthoni is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Oct 2010
    Posts
    6

    Query that shows a 1 or a 0 based on existence of record in another table

    I have a db that is tracking a student fund raiser. I have a table of students, and a table to pledges collected. I want to create a query that has all the data from the students table, and then I want a 1/0 or a true/false based on whether or not there is a record in the pledgesCollected table. I am thinking something like:



    SELECT tblStudents.FullName, IIf(EXISTS(SELECT StudentProceeds.Student FROM StudentProceeds WHERE StudentProceeds.Student = tblStudents.FullName),1,0) AS PledgesCollected
    FROM tblStudents;

    I get 'Data type mismatch in criteria expression.'

    Any ideas of what I am doing wrong, or a better way to do this?

    Thanks!
    ~john

  2. #2
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    The query itself looks fine to me..

    The 'Data type mismatch in criteria expression.' might be due to:
    tblStudents.FullName AND StudentProceeds.Student are not the same type?

    Also if you have some null values, that *might* also be a problem.

  3. #3
    jthoni is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Oct 2010
    Posts
    6
    Well,
    tblStudents.FullName is calculated with: [LastName] & ", " & [FirstName]

    StudentProceeds.Student is a lookup type which looks up tblStudents.FullName

    So one is a calculated field, and the other a lookup. If this is the problem, is there anyway to do the check on a .ToString() type so that they both resolve to the same data type?

    Thanks!

  4. #4
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    I think an outer join is more efficient than exists:

    SELECT tblStudents.FullName, IIf(StudentProceeds.Student is null, 0,1) AS PledgesCollected
    FROM tblStudents left join StudentProceeds on StudentProceeds.Student = tblStudents.FullName

  5. #5
    slave138's Avatar
    slave138 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    WI
    Posts
    233
    I couldn't find a reference for the Exists method in Access 2010, but in Word it requires a String-type parameter. If the same is true then the code should probably be something like:

    IIf(EXISTS("SELECT StudentProceeds.Student FROM StudentProceeds WHERE StudentProceeds.Student = tblStudents.FullName"),1,0)

  6. #6
    jthoni is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Oct 2010
    Posts
    6
    Thanks all.

    I figured out what was going on. I was thinking that the field was text, but the lookup is actually the ID (it just renders text in the query). I did needed to join on the ID, not the text full name:

    SELECT tblStudents.FullName, IIf(StudentProceeds.Student is null, 0,1) AS PledgesCollected
    FROM tblStudents left join StudentProceeds ON tblStudents.[StudentID] = StudentProceeds.[Student];

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

Similar Threads

  1. Replies: 2
    Last Post: 10-13-2010, 07:09 AM
  2. Query based on current record in form
    By bhsvendbo in forum Queries
    Replies: 1
    Last Post: 06-01-2010, 11:20 AM
  3. Replies: 3
    Last Post: 12-23-2009, 08:50 AM
  4. Report based on query shows no data
    By hbograd in forum Reports
    Replies: 2
    Last Post: 12-18-2009, 12:28 PM
  5. Replies: 2
    Last Post: 10-27-2009, 07:09 AM

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