Results 1 to 6 of 6
  1. #1
    Favri is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2025
    Posts
    1

    Form-Subform linking in VBA

    Hi!

    I have been trying to apply a solution to my Access application where a user is picked from the combo list, then a subform lists his projects (works).


    Click image for larger version. 

Name:	1.png 
Views:	21 
Size:	111.6 KB 
ID:	52983


    Well, everything works just fine, until I try to list projects based on position of a user. When I enter this code it works:



    Click image for larger version. 

Name:	3.jpg 
Views:	21 
Size:	124.7 KB 
ID:	52984


    But when I try to insert an IIf condition it returns error:





    Click image for larger version. 

Name:	2.jpg 
Views:	21 
Size:	127.8 KB 
ID:	52985


    What should I do to make this code work?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Why the brackets around the True and False values?
    Normally, (well always), one of those would be assigned to a variable/control?, so your syntax is way off.

    When in doubt look up the syntax.
    https://support.microsoft.com/en-gb/...0IIf%20returns.

    However that is the same as yours , and only works in that form as a souce for a control.
    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
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    I would suggest you move your IIF() to set the value to a string variable (Dim sChildField as String) then use that to set the linking property. It makes cleaner and easier to debug...
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    xps35's Avatar
    xps35 is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    299
    Why do you want to do it in VBA? The fields (on main and sub) will always be the same, no matter the selected value. So use the relevant properties of the subform.
    Groeten,

    Peter

  5. #5
    Join Date
    Apr 2017
    Posts
    1,792
    So you have an unbound form as main one, where you select an user (I assume this as you refer to combo control as MasterField property of subform instead of userID of main form's source), and the list of selected users projects in subform?
    And in table of users as a field which determines a position of given user (I hope it is not a description of position, but PositionID). And you want the subform to display only projects matching with position selected in another combo of main form?

    On fly!
    Have the RowSource of position selectin combo something like
    Code:
    SELECT PositionID, Position FROM tblPositions
    UNION SELECT 0 AS PositionID, "No Filter" FROM tblPositions
    ORdeR BY 1
    For position combo, design a Change event, which:
    In case the value of combo is 0, sets the subform's Filter property to "", and FilterOn property to False;
    Otherwise sets Filter property to "ChangeID=" & ValueOfCombo, and FilterOn property to True.

  6. #6
    Join Date
    Apr 2017
    Posts
    1,792
    So you have an unbound form as main one, where you select an user (I assume this as you refer to combo control as MasterField property of subform instead of userID of main form's source), and the list of selected users projects in subform?
    And in table of users as a field which determines a position of given user (I hope it is not a description of position, but PositionID). And you want the subform to display only projects matching with position selected in another combo of main form?

    On fly!
    Have the RowSource of position selectin combo something like
    Code:
    SELECT PositionID, Position FROM tblPositions
    UNION SELECT 0 AS PositionID, "No Filter" FROM tblPositions
    ORDER BY 1
    For position combo, design a Change event, which:
    In case the value of combo is 0, sets the subform's Filter property to "", and FilterOn property to False;
    Otherwise sets Filter property to "PositionID=" & ValueOfCombo, and FilterOn property to True.

    Scrap it! I assumed you want to filter subform for position of project! It looks like you want to select only users of certain position.
    So instead use Change event of position combo to update the RowSource of users combo like:
    In case the value of position combo is 0, sets the users combo RowSource to "SELECt UserID, User FROM tblUsers ORDER BY 2";
    Otherwise sets the users combo RowSource to "SELECt UserID, User FROM tblUsers ORDER BY 2 WHERE PositionID=" & ValueOfCombo.

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

Similar Threads

  1. Replies: 2
    Last Post: 01-21-2018, 09:19 PM
  2. Replies: 7
    Last Post: 02-02-2017, 05:01 PM
  3. Replies: 15
    Last Post: 03-05-2013, 11:57 PM
  4. Replies: 0
    Last Post: 03-04-2011, 10:28 AM
  5. Subform to subform linking
    By Mitka in forum Forms
    Replies: 1
    Last Post: 01-15-2011, 03:43 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