Results 1 to 9 of 9
  1. #1
    Yesideez is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Location
    Devon, UK
    Posts
    17

    Listview pulling data from a second table

    I've got a form "properties" and at the bottom I've got a listview gadget that I'd like to pull data from a "worklogs" table where the property ID matches a field in the worklogs table.



    I've written the query (below) but I can't seem to find out how to make it so as I'm browsing through the properties the listview automatically shows any work logs if any are present.

    If this is a simple thing a pointer in the right direction would be appreciated otherwise a link to a good tutorial would be appreciated.

    Code:
    SELECT [worklog].[id],[worklog].[address],[worklog].[postcode],[worklog].[dateassigned],[worklog].[workcompleted],[worklog].[paymentreceived],[worklog].[added] FROM worklog WHERE [properties].[id]=[worklog].[_propertyid] ORDER BY [address];

  2. #2
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    You lack a join in your SQL to the properties table. I have attempted to rewrite your SQL:

    SELECT id, address, postcode, dateassigned, workcompleted, paymentreceived, added FROM worklog INNER JOIN properties ON worklog.[_propertyid] = properties.id ORDER BY address;

    I know that you are told to put names inside brackets and to explicitly address every column by prefixing the table name but in your case omitting these items makes the SQL more readable. Prefixing a name with an underscore is allowed but not a good idea; I have left the brackets around it as I'm not sure what the SQL parser would do with it otherwise.

    What exactly do you mean by a 'listview gadget?'

    PS The SQL tags won't work for me so the sample SQL remains in plain text.

  3. #3
    Yesideez is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Location
    Devon, UK
    Posts
    17
    Thanks for that - I've been using MySQL for around 10 years and didn't think I'd be needing to join with another table, still trying to get my head around how Access queries run in this environment.

    A listview gadget is one of these: http://www.c-sharpcorner.com/UploadF...stViewImg2.gif

    Why is underscore not a good idea?

    I use those so that foreign keys stand out, have done since I started using MySQL, never had a problem with them.

    The query I posted was a direct copy & paste job from Access.

  4. #4
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Hi,

    Most important: does my SQL work?

    Alright ...

    MySQL is a RDBMS (Relational Database Management System). Access is also a RDBMS plus a lot more. (Actually JET is the RDBMS that Access uses.) SQL (Structured Query Language) is an industry wide standard database query language and is not exclusive to Access. Access follows the industry standard fairly closely and deviations from standard are well documented. Joining tables is not peculiar to Access.

    I knew there is a standard where foreign keys (or is it primary keys?) are prefixed by an underscore - so it's MySQL, is it? The only instance I know of (to my cost!) where underscores cause a problem in Access is in the naming of user events. This I discovered in v2003 and I have seen no documentation to say that the 'bug' has been corrected. Otherwise my objection to underscores is more cosmetic. Access will often introduce underscores in the names it constructs. For this reason I avoid underscores - all special characters in fact - and stick to capitalisation for breaking-up my names. I find the resulant code more readable and easier to interpret.

    Er ... the PS was a comment for the moderators. Do they ever read these posts? I placed SQL tags around my text and the result was an empty exhibit!

  5. #5
    Yesideez is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Location
    Devon, UK
    Posts
    17
    thanks for explaining that, I'll consider replacing the underscore with something else like FK or fk.

    The query did and didn't work. It does pull out the records but then it also pulled out a lot more as well.

    If I add a WHERE clause in:
    Code:
    worklog.[_propertyid]=properties.id
    That gives me exactly the same.

    I tested with a record in the properties table I know has had some work done on it:
    Code:
    worklog.[_propertyid]=6397
    That showed me the records I need - just need to figure out how to use the id from the property being viewed at the time.

  6. #6
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Yup, a WHERE clause is what you need. Where is the value of the 'id being viewed?' I assume it's in some control or other. Put the explicit reference to this control in the SQL and all should be well.

  7. #7
    Yesideez is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Location
    Devon, UK
    Posts
    17
    How would I do that?

    The "id" field is the primary key of the properties table (and all others)

  8. #8
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    OK.

    SELECT id, address, postcode, dateassigned, workcompleted, paymentreceived, added FROM worklog INNER JOIN properties ON worklog.[_propertyid] = properties.id WHERE worklog.[_propertyid] = xxxxx ORDER BY address;

    The question is what to substitute for xxxxx.

    Hopefully somewhere on your form, visible or hidden, enabled or disabled is the 'id' value. Suppose that it is in a control named, txtId, and that it is numeric, then substitute xxxxx with Me.txtId. (I won't give the text/string value syntax which is slightly different.) If 'id' is not on the form then refer to the underlying recordset, [_propertyid] or id is probably a column in the recordset.

    Some considerations:
    • Me is shorthand for the object in which the code is running - in this case the form. It avoids all that tedious Forms!.. nonsense.
    • Me.txtId should properly be written as Me.txtId.Value but as Value is the default property then it is not necessary to be explicit.

  9. #9
    khalid's Avatar
    khalid is offline MS-Access Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2010
    Location
    Kuwait
    Posts
    244
    What is the [properties].[id]? Is it numeric or its text?
    Never face problem with _ underscore in my table/field names, also [ ] is good idea around the tables/fields names if there is spaces between the field/table names.
    Calling [properties].[id] (table.fieldname) is necessary if the same field name exists in two tables. Otherwise ambiguous error would accord.
    Naming the reserved word (properties, field, name etc) using as a field name would be painful and creates problem in VBA.

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

Similar Threads

  1. Relationships and pulling data.
    By Subhunter in forum Queries
    Replies: 2
    Last Post: 02-08-2011, 01:18 PM
  2. Pulling only certain data from tables.
    By stevman22889 in forum Access
    Replies: 2
    Last Post: 07-15-2010, 06:23 PM
  3. Transfer data from ListView to Table
    By Zyckie in forum Access
    Replies: 1
    Last Post: 12-15-2009, 11:23 AM
  4. Replies: 0
    Last Post: 07-27-2009, 07:51 AM
  5. Pulling most recent data from child table
    By davidv43 in forum Access
    Replies: 0
    Last Post: 02-07-2009, 11:36 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