Results 1 to 6 of 6
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    246

    Update a field in subform1 based on criteria of SubForm2


    Hi
    SubForm1 and SubForm2 (based on queries) are two Sub Forms on a Main Form. Each record in SubForm1 shows related records in Subform2.
    A Yes/No field is on SubForm1 with default value= Yes. I want to write criteria for the Yes/No field BeforUpdate Event so that it will not to be updated (chnaged from Yes to No) if records exists in subForm2.
    How can this be done?

    KHalil

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,012
    Use a Dcount() ?
    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
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    246
    Hi,

    I used this line code:
    Code:
    If DCount("*", "queryNameOfSubForm2", "[ContainerNameSubForm2].[pkNumberId]=fkNumberId") > 0
    And had a Type mismatch error.
    Khalil

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,012
    You need to concatenat the control value.
    You also need to adress the control correctly depending on where the code is placed.
    http://access.mvps.org/access/forms/frm0031.htm

    Code:
    [pkNumberId]= " & fkNumberId
    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?


    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format


    Numbers do not need anything


    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.


    Added benefit is, if you still cannot see the error, you can copy and paste back here for someone else to spot it.
    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

  5. #5
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    246
    Hi,
    I used the following:
    Code:
    If DCount("*", "queryNameOfSubForm2", "[ContainerNameSubForm2].[pkNumberId]= " & fkNumberId) > 0
    I got the error message: Variable not defined.

    I added:
    Code:
    Dim fkNumberId As Integer
    the error is: Typemismatch
    Any ideas

    Khalil

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,012
    Yes, pknumber is not a numeric
    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. Linking Subform1 to Subform2
    By Perfac in forum Forms
    Replies: 4
    Last Post: 03-08-2023, 12:09 PM
  2. Replies: 7
    Last Post: 08-17-2020, 01:59 PM
  3. Replies: 2
    Last Post: 11-21-2016, 07:44 PM
  4. Replies: 3
    Last Post: 10-18-2016, 08:31 PM
  5. Replies: 1
    Last Post: 02-27-2014, 01:57 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