Results 1 to 10 of 10
  1. #1
    ericxmiller is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5

    Comparing 2 fields on 2 different tables

    I am looking to do a fuel comparison query for a database that I put together. I have all of the purchase data in one table and I have all of the vehicle data in another table with 3 common fields joining the tables. I want to compare the amount of fuel purchased from Table 1 to the vehicle fuel capacity (not a shared field) in Table 2. I can't seem to get the query to work. I am pretty new to writing Access queries so I am sure this is an easy query to write. Does anyone know how I can get this to work?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,618
    Show the attempted query SQL statement. Provide samples of source data and desired output. If you want to provide db, follow instructions at bottom of my post.
    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
    ericxmiller is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5
    Here is the attempted SQL statement:

    SELECT [VAR Database].VehicleModelYear, [VAR Database].VehicleMake, [VAR Database].VehicleModel, [VAR Database].LicenseNumber, [VAR Database].[Driver Name], [VAR Database].DateofPurchase, [VAR Database].TimeofPurchase, [VAR Database].VendorName, [VAR Database].VendorState, [VAR Database].ProdCode, [VAR Database].PurchaseQuantity, [VAR Database].PurchaseAmount, [Fuel Tank Capacity].[Fuel Tank Capacity]
    FROM [Fuel Tank Capacity] INNER JOIN [VAR Database] ON ([Fuel Tank Capacity].Model = [VAR Database].VehicleModel) AND ([Fuel Tank Capacity].Make = [VAR Database].VehicleMake) AND ([Fuel Tank Capacity].[Model Year] = [VAR Database].VehicleModelYear);
    WHERE ((([VAR Database].PurchaseQuantity)>[Fuel Tank Capacity].Fuel Tank Capacity));

    I am looking to take the fuel tank capacity of our fleet vehicles and see if employees are purchasing more gas than the capacity.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,618
    Only thing I see wrong is lack of [] around Fuel Tank Capacity in the WHERE clause.
    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
    ericxmiller is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5
    It keeps telling me that characters are found after end of SQL statement. Not sure what characters they are talking about since what I posted is everything that is in the SQL view. Any ideas of what my WHERE clause should look like to eliminate this error?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,618
    Just the [] around field name.

    Don't use spaces and special characters/punctuation (underscore is exception) and the [] will not be mandatory.
    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.

  7. #7
    ericxmiller is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5
    Now I am getting a data type mismatch in criteria expression. Here is what I have: WHERE ((([VAR Database].PurchaseQuantity)>[Fuel Tank Capacity].[Fuel Tank Capacity]));

    Should work shouldn't it with the previous select and from clauses as in my previous post? At least I am getting a new error now.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,618
    Verify that related fields used in the JOIN and WHERE are the same data type in tables.
    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.

  9. #9
    ericxmiller is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5
    Ok it works now, thank you so much! The only problem now is that in the text data type it does not correctly compare the numbers. How do I convert the data type to number for with 2 decimal places without converting it to a full value number without the decimal places? I made a copy of the database but I keep losing the data I need for each decimal place when I convert. Is it possible to convert it without losing those values?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,618
    You made a copy of database or did you mean table?

    Why are you comparing a text type with a number type? Which fields are involved? Why aren't they the same datatype in the tables? Select Double as the FieldSize in the field properties section at the bottom of the table designer.
    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. Comparing two tables
    By jcarstens in forum Queries
    Replies: 3
    Last Post: 05-07-2012, 10:06 PM
  2. Comparing two tables.
    By elmister in forum Access
    Replies: 11
    Last Post: 08-24-2011, 11:59 AM
  3. Comparing two tables.
    By elmister in forum Queries
    Replies: 1
    Last Post: 08-22-2011, 05:32 PM
  4. Comparing tables
    By YoungWolf in forum Database Design
    Replies: 7
    Last Post: 01-10-2011, 11:32 AM
  5. Comparing between fields
    By Dashin in forum Queries
    Replies: 0
    Last Post: 02-13-2009, 08:38 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