Results 1 to 10 of 10

Thread: Showing a form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Showing a form

    Urgh, surely this is a simple question, but after hours of trial and error / fruitless internet searching, I got no idea where I'm going wrong. All I want to do is load a form in VBA under MS Access...

    Code:
    Dim f As Form_f_employeeSelect
    Set f = New Form_f_employeeSelect
        
    f.Show vbModal, Me
    Code:
    Dim f As New Form_f_employeeSelect
        
    f.Show vbModal, Me
    Code:
    Form_f_employeeSelect.Show vbModal, Me
    None of those seem to work for me. I used to be pretty good with VB6, but this completely simple problem has me stumped.
    Using Visual Studio .NET 2005

  2. #2
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Showing a form

    VB Code:
    1. Docmd.OpenForm "f_employeeSelect"

  3. #3
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Showing a form

    Try..

    DoCmd.OpenForm "f_employeeSelect", acNormal , or whatever the form is called in the access objects window..

    To set it to be modal set that in the design view of the form, under the other tab in the properties window
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: Showing a form

    Thank you both. I take that is basically the only way to do this? You can't just open a form as you would in VB6?
    Using Visual Studio .NET 2005

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Showing a form

    Nope, that is the way to do it for an Access Form.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Showing a form

    Quote Originally Posted by Barguast
    Thank you both. I take that is basically the only way to do this? You can't just open a form as you would in VB6?
    There is one other way that I know of. You can declare a public variable of type Form.

    VB Code:
    1. public frm as Form

    Then you can set frm to a new instance of an existing form.

    VB Code:
    1. set from= New Form_f_employeeSelect

    The reason the frm variable has to be public is that if it is private or just dimmed within a function or sub it will be destroyed when the sub/function exists or the object holding the private variable is closed.

    This method can be used to create multiple instances of the same form, even displaying different data. To do this you declare a public array of type form.

    VB Code:
    1. public frm() as Form

    You will need to create a function that keeps track of how many items there are in the array so that when you add a new instance of a form to the array you know what position to put it in. When you need to reference a form, e.g. to set recordsource, you reference the form from the array.

    VB Code:
    1. frm(3).form.recordsource="select * from sometable"

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: Showing a form

    I've tried kaffenils example and I'm still having problems...
    Code:
    Public m_employeeSelect As Form
    
    Private Sub cmdSelectEmployees_Click()
    
        Set m_employeeSelect = New Form_f_employeeSelect
        m_employeeSelect.Show vbModal
    
    End Sub
    When I run that, I get an 'Application-defined or object-defined error' run-time error on the 'm_employeeSelect.Show vbModal' line.

    The reason I want to use this method is that I still want to be able to access the form after it has been closed so I can get some information about the state it was left in when it was closed (in this case, which employees were selected in a ListView control). As far as I can see you can't do this with the DoCmd.OpenForm method. Or can you?
    Last edited by Barguast; Nov 1st, 2005 at 12:04 PM.
    Using Visual Studio .NET 2005

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: Showing a form

    OK, looks like I might have to explain my situation better.

    Basically, the form I want to load shows a list of names (with checkboxes) that are loaded from the underlying Access database. Once the user has selected the names, the form will close. However, since the form will still have the information of which names are selected, I still want to be able to access the form after it has closed. For example:
    Code:
    'Show the form and wait for it to close
    f_employeeSelect.Show vbModal
    
    'If the form was cancelled then exit
    if (f_employeeSelect.CancelSelected ()) Then Exit Sub
    
    'Get the list of names selected
    Dim c as Collection
    c = f_employeeSelect.GetNameList ()
    As should be demonstrated by the above code, I simply want to be able to access the functions of the form after it has been closed so I can extract the information entered by the user whilst it was open.

    Is this possible? Or am I just going about it all wrong?
    Using Visual Studio .NET 2005

  9. #9
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Showing a form

    An Access form doesn't have a Show method. Instead you must set the Visible property to True.

    VB Code:
    1. Public m_employeeSelect As Form
    2.  
    3. Private Sub cmdSelectEmployees_Click()
    4.  
    5.     Set m_employeeSelect = New Form_f_employeeSelect
    6.     m_employeeSelect.Visible=True
    7. End Sub

    If you want to hide it you simply set the Visible property to False. Then you can still access it's properties and controls. Don't close it (which is done by setting the public variable to nothing) as the form and it's controls no longer will be accessible.

    VB Code:
    1. Set m_employeeSelect = Nothing

  10. #10
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Showing a form

    Quote Originally Posted by Barguast
    As should be demonstrated by the above code, I simply want to be able to access the functions of the form after it has been closed so I can extract the information entered by the user whilst it was open.

    Is this possible? Or am I just going about it all wrong?
    If you don't need to open multiple instances of a form, then there is no need to open it with the metod I descibed above (the one with a public variable holding the form).
    To open the form once (as a dialog), you use
    VB Code:
    1. docmd.OpenForm "f_employeeSelect",,,,,acDialog
    This will cause the code that opens the form to halt until the form is closed. The problem is that you cannot access the form after it is closed, but you can access it if hidden (Visible=False). The way to do it is to create a Ok and a Cancel button on the modal form. When the user clicks Cancel you close the form (Docmd.Close acForm, me.name) and when the user clicks Ok you hide the Form (Me.Visible=True).
    The code that opened the form will now keep running, and the first thing you do is to check if the form f_employeeSelect is still open (which means that the user clicked Ok). To check if a form is open you use the following code
    VB Code:
    1. If SysCmd(acSysCmdGetObjectState, acForm, "f_employeeSelect") > 0 Then
    2.     c = form_f_employeeSelect.GetNameList() ' I don't know what this function do...
    3.     ' We must also close the form
    4.     docmd.close acForm,"f_employeeSelect"
    5. end if

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width