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

    Referencing a Subform in a Variable

    EDITED
    I have nearly 20 forms. I want to use a Public Sub written in a module to prevent writing some lines of code on each form's Got Focus event.

    So, To lock an active form (on its Got focus or any similar event) , I wrote this code in a module:



    Option Compare Database
    Public strFormName as string
    _____________________________

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

    Then, on Got Focus event of the form, I wrote the following code:

    strFormName = Me.Name
    LockThisForm ' (a Sub name defined in a module as written above)

    It worked for Main Form perfectly, but not working for Subform, and shows runtime error number 2450 (Access cannot find the referenced form "Orders Subform")

    Subform name is referenced differently in a variable but I don't know how? And where to change it, in the module or on Got Focus event of the Form.

    Some practical guidelines will be appreciated

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Instead of using a public variable, use a parameter:
    Public Sub LockThisForm(strFormName)

    Then:
    Call LockThisForm("Forms!formname![Orders Subform]")

    Best practice is to never use spaces in object names.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    forms!mainformName!subformName!form.allowedits = false


    use the BUILDER, it will show you the path name correctly. (good for query critiera and vb)
    pick your db,
    pick forms,
    loaded forms,
    your form,
    your subform

  4. #4
    Athar Khan is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Mar 2017
    Posts
    63
    Thanks for your attention Aytee111 & Ranman256

    Please try to understand that I don't want to use the form name manually anywhere, instead I want two variables to store the Main and Subform names so that I could use those variables for referring the forms

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The way you have it written above, you are adding the call statement to each form, so why not add its name at the same time? Otherwise you would need to call a macro which runs a common function.

  6. #6
    Athar Khan is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Mar 2017
    Posts
    63
    I can and have to use form name or Me.Name on got focus event of each form, no problem, but I need to use a variable for form name in the MODULE only, so that a single module could get the name of any active form which gets the focus. The only issue is in the Public sub (in a module) where I want to use the code below ONLY ONCE , and don't want to repeat this code to each form separately:

    Public Sub LockThisForm(strFormName)

    SELECT CASE UserID

    Case 1 'Admin
    'code to Allow everything

    Case 2 'Power User
    'code to Allow Additions and disable Deletion

    Case 3 'Viewers
    'Code to Disable everything for user
    Forms(strFormName).AllowEdits = False
    Forms(strFormName).AllowEdition = False
    Forms(strFormName).AllowDeletion = False

    END SELECT

    End Sub

  7. #7
    Athar Khan is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Mar 2017
    Posts
    63
    I must put the form name to a variable on Got Focus event or any similar event of each form.
    That variable will then be used to allow or disallow Edits, Additions and Deletions for an active form.

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

Similar Threads

  1. Replies: 6
    Last Post: 11-20-2014, 03:37 PM
  2. Referencing form fields with a string variable
    By ts_hunter in forum Programming
    Replies: 3
    Last Post: 02-13-2014, 01:06 PM
  3. Replies: 2
    Last Post: 01-18-2014, 11:51 AM
  4. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  5. Referencing a textbox using variable values from a function?
    By omahadivision in forum Programming
    Replies: 3
    Last Post: 12-20-2012, 09:23 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