Results 1 to 10 of 10
  1. #1
    GeorgeJ is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    67

    Setting a variable of type form to an existing form

    I am trying to learn how to use VB to change the properties of a Form. I have created an extremely simple database with no tables and just one form named “Form1”. I have created a module (Mod1) with the following code



    Option Compare Database
    Option Explicit
    Dim Frm As Form

    Sub test01()
    Set Frm = Forms("Form1")
    End Sub
    Sub test02()
    DoCmd.OpenForm ("Form1")
    End Sub
    When I run test01 I get a msg saying “Microsoft Access cannot find the referenced ‘Form1’ “
    I know the form exists because it opens when I run test02

    Can someone tell me how to set a variable of type Form to an existing form so I can start playing around with changing the form’s properties? I know one can manipulate controls once the form is loaded by using "Me.__"
    but I would like to be able to manipulate form's properties from code outside the form's code file.

    Thanks in advance

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Try:
    Code:
    Option Compare Database
    Option Explicit
    
    
    Sub test01()
      Dim strFormName As String
      strFormName = "Form1"
      DoCmd.OpenForm (strFormName)
    End Sub
    Sub test02()
      DoCmd.OpenForm ("Form1")
    End Sub
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    GeorgeJ is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    67
    Bob, your code works, but what if I wanted to change a control on a form without opening the form. Suppose, for example, that on Form1 there was a button "Button1" and I wanted to make it invisible using code in Module1.

    I am trying to do something like this


    Forms("Form1").Button1.Visible = true

    but that doesn't work.
    Also, what if I want to use code in Module1 to add a control to Form1 ?


    Any suggestions.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    AFAIK, the object must be open but can be not visible.

    Why would you want to dynamically create controls? Do you intend to save the form with the new control? This must be done in Design View. Programmatic design edits can cause issues.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    GeorgeJ is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    67
    [QUOTE=June7;257179]AFAIK

    I appreciate your point about design issues, but I would like to see if it is possible to modify a form with code outside the form's code module. I think this flexibi I wouldn't necessarily want to save the modifications. I'll see if anyone has any suggestions. Thanks for your response.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I am sure it can be done because code can create an entire form. However, AFAIK, the form still has to be opened in Design View to perform other modifications. http://stackoverflow.com/questions/8...form-in-access
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    As June7 has said, twice, the Form has to be Open, in Design View, in order to set properties!

    Linq ;0)>

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Let me back up a little. Properties of form and controls can be changed during runtime but if you want to save the modifications, AFAIK must be from Design View.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by June7 View Post

    ...Properties of form and controls can be changed during runtime...
    Sure, but the Form has to be open to do this, even in Runtime, and the OP, for whatever reason, wants to do this without the Form being opened, and without code in the Form's Module, and as we know that's simply a no-go.

    Linq ;0>

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Yes, certainly form must be open in some mode. However, the code does not have to be behind the form but makes it more complicated. After all, how could a new form be created if code must be behind the form?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 5
    Last Post: 10-27-2014, 10:33 PM
  2. Setting a variable on one bound form from another table
    By chris.williams in forum Programming
    Replies: 5
    Last Post: 08-21-2012, 01:37 PM
  3. Replies: 11
    Last Post: 05-23-2012, 08:42 AM
  4. Setting Temporary Variable
    By KEVWB in forum Access
    Replies: 1
    Last Post: 02-07-2011, 01:41 PM
  5. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 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