Results 1 to 5 of 5
  1. #1
    Athar Khan is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Mar 2017
    Posts
    63

    Referencing a Subform in Module

    To lock each active form in DB, I wrote this code in a module:

    ....
    Public frmFormName as string

    Public Sub LockThisForm()
    Forms(frmFormName).AllowEdits = False
    End Sub

    Then, OnCurrent event of each form, I wrote the following code:

    frmFormName = Me.Name


    LockThisForm

    It worked for Main Forms only, not for Subform, because subform name is referenced differently. I don't know where to change it, in the module or OnCurrent event of the Form, and how to reference it properly.

    Any help please?

  2. #2
    Athar Khan is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Mar 2017
    Posts
    63
    If I try to apply the same code for Subform, I get this error # 2450:
    "Microsoft Access cannot find the reference form 'MyFormName'.

  3. #3
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    The subform reference syntax is like
    Forms!frmFormName.SubformControlName.Form then the property or thing (control) you're trying to affect. As in

    Code:
    Forms!frmFormName.SubformControlName.Form.AllowEdits = False
    SubformControlName is the name of the control containing the subform, not the subform name.
    The idea seems a bit strange. Why have a call for this on every form, especially the current event? It will fire each time the main forms current event fires, which might be often.
    If there is one thing that drives this need (say a form control value changes) just cycle through all the open forms and affect them in one go. If you're not OK with what you're doing, elaborate on the process. Any other suggestions will depend on what it is you're doing - and maybe why, because oddly enough, a subform doesn't appear in an enumeration of open forms when it's only loaded by its parent form.
    Last edited by Micron; 09-25-2017 at 05:57 PM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Athar Khan is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Mar 2017
    Posts
    63
    Thanks you Micron, I am trying to set the properties like Edit, Additions and Deletion for each form (based on current user), but not at once. I want to use a Public Sub to prevent writing lock/unlock code on every form. OnCurrent event was just experimental. I can call the Public Sub from any other event of the form.

    Form!frmFormName.SubformControlName.Form.AllowEdit s = False

    I think it is used normally, but my situation is different. As For Main Forms, I used this shortcut way, not the name of the form, this will get form name itself.

    Forms(frmFormName).AllowEdits = False

    But, for Subforms, I can't use:

    Forms(frmSubFormName).AllowEdits = False

  5. #5
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    But, for Subforms, I can't use: Forms(frmSubFormName).AllowEdits = False
    You can't because it's not the correct syntax for referencing a subform. As previously noted, a subform isn't a member of the Forms! collection when the form is open, but you are trying to access it directly regardless and you cannot. It is part of the main form hierarchy and is not found in the "chain" of that hierarchy where you are trying to get at it from. See the first 2 sentences of my first reply. As for where to put the code, to do this as you're suggesting, you will have to at least put a call to a public sub in every form - probably the form Open event. That method will require a difficult (IMHO) work around to handle sub forms. I have tried but never succeeded in passing a variable referencing a form object or a variable that passes the name as a string to the expression that references a subform. Not saying it can't be done - just that I haven't succeeded. Since you have to put at least a call in every form anyway, it's not much more work to cut and paste a block of code from one form to another, changing the subform name as you go. As for the main form, you can alter it using Me, so you wouldn't have to worry about the main form name. With the following block, to affect the subform on a main form you would have to be concerned with correcting the main and subform names.

    Code:
    With Forms("frmMainFormName").Controls("subFormControlName").Form
       .DataEntry = False
       .AllowAdditions = True
       .AllowDeletions = False
       .AllowEdits = True
     End With
    Last edited by Micron; 09-26-2017 at 11:16 AM. Reason: correction

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

Similar Threads

  1. Help referencing a subform
    By Delta729 in forum Forms
    Replies: 1
    Last Post: 08-13-2015, 01:01 PM
  2. Referencing subform controls
    By cheyanne in forum Forms
    Replies: 7
    Last Post: 04-30-2012, 10:00 AM
  3. Replies: 4
    Last Post: 05-16-2011, 04:58 PM
  4. Referencing a form in a subform
    By 161 in forum Forms
    Replies: 3
    Last Post: 01-24-2011, 03:58 PM
  5. Replies: 3
    Last Post: 10-15-2010, 11:17 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