Results 1 to 6 of 6
  1. #1
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280

    Refering to field when creating table relationship using string

    I'm sure this must be simple, but can't find the correct syntax to make this work.

    I have the following code :
    relRelation.Fields!Place.ForeignName = "PlaceFirstlogged", where relRelation is a relation set using CreateRelation



    I want to replace this with :
    relRelation.Fields!strTableField.ForeignName = strForeignTableField where strTableField is a string referring to the main table field and strForeignTableField is a string referring to the Foreign table field.

    This gives an error on the first bit relRelation.Fields!strTableField.ForeignName - it says "Item not found in this collection"

    What is the correct syntax to make this work?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,557
    Perhaps
    Code:
    relRelation.Fields!(strTableField).ForeignName
    or
    Code:
    relRelation.Fields.(strTableField).ForeignName
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,934
    Or perhaps

    relRelation.Fields(strTableField).ForeignName

  4. #4
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Thanks for the reply, but I've tried both those. Both fail at compile time.
    The first gives : Type-declaration character does not match declared data type
    and the second just gives a syntax error.

    You see my problem in finding the right syntax.

  5. #5
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Wow, yes that works.
    Knew it would be simple, problem solved.

    just to be clear, that is relRelation.Fields(strTableField).ForeignName posted by CJ_London

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,557
    Quote Originally Posted by Miles R View Post
    Wow, yes that works.
    Knew it would be simple, problem solved.

    just to be clear, that is relRelation.Fields(strTableField).ForeignName posted by CJ_London
    Well I was close?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 1
    Last Post: 05-02-2019, 09:12 AM
  2. refering to a field on a form
    By StuartR in forum Forms
    Replies: 2
    Last Post: 02-20-2018, 05:57 AM
  3. Replies: 1
    Last Post: 06-21-2012, 09:38 AM
  4. Replies: 3
    Last Post: 06-03-2009, 07:15 AM
  5. Replies: 4
    Last Post: 04-01-2009, 08:49 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