none
Cannot Get ListBox .ItemsSelected, help! RRS feed

Answers

  • Here's the code:
    ?[Form_Flex2 Hospitals].lstHospitals.ItemsSelected.Count

    More Weirdness

    Must Close Twice:
    The form will not close on first try:
    DoCmd.Close acForm, "Flex2 Hospitals"

    but it WILL close if i execute the same code again.
    DoCmd.Close acForm, "Flex2 Hospitals"


    Not Current Object:
    This code returns the name of a hidden form, instead of the open form:
    ?CurrentObjectName
    ' returns name of a hidden form, instead of the visible one. 

    If i attempt to setfocus to this form, CurrentObjectName STILL returns name of a hidden form. 
    [Form_Flex2 Hospitals].SetFocus 
    ?CurrentObjectName
    ' returns name of a hidden form, instead of the visible one. 

    Here are images of the form:

    http://postimg.org/image/ft8enykt9/

    http://postimg.org/image/6wxmk0u71/


    It appears that you are occasionally -- frequently? -- using the Form_<form name> syntax to refer to your forms. This syntax has many pitfalls, and clearly it is tripping you up.  The Form_<form name> syntax is a reference to the form's class object, not to the form itself, and has these odd quirks:

    1. It can only be used with forms that have a VBA class module.
    2. If the form isn't loaded when you make this reference to its class module, Access will load a hidden copy of the form and add it to the Forms collection, but will not index it by name until you make it visible.
    3. If you have created such a hidden copy of the form and then use DoCmd.OpenForm to open that form, you will have two copies of the form open:  one indexed by its name, one not.  The one indexed by its name is called the "default instance" of the form, and the one that was opened by reference to its class module is a non-default instance.
    4. You can use this technique to have multiple non-default instances of the same form open, which can be handy sometimes.
    5. However, if you have multiple instances of the form open, you don't know which of them a new Forms_<form name> reference will be referring to.  And don't forget that a form could be open as both a main form and a subform at the same time -- which instance does "Form_MyFormName" refer to?
    6. There's a way to manage this sort of thing to control multiple non-default instances of the form by using a collection of object variables and setting them via the New keyword.
    7. If you don't know what you're doing, you can get yourself into trouble.

    In short, my suggestion to you is to remove all uses of Form_<form name> and replace them with the canonical usage:

    • 'DoCmd.OpenForm "FormName"' instead of 'Form_FormName.Visible = True'
    • 'Forms!FormName.ControlName' or 'Forms("FormName").ControlName' instead of Form_FormName.ControlName
    • 'DoCmd.SelectObject "FormName"' instead of Form_FormName.SetFocus
    • etc.

    I expect that will get rid of your problem with the ItemsSelected.Count, because you will now be looking at the correct instance of the form.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Monday, September 2, 2013 7:55 PM
  • if you're correct, how would code requirements or options differ, compared with if the form is indexed by name?

    For a nondefault instance, you can't use object references in the format Forms!FormName or Forms("FormName").  Consider this code:

    Sub TestNondefaultForm()
    
        Dim frm As Access.Form
        Dim strFormName As String
        
        Debug.Print "Before execution, Forms count = "; Forms.Count
        
        Set frm = New Form_Form2
        Debug.Print "After creating nondefault form, Forms count = "; Forms.Count
        
        strFormName = frm.Name
        Debug.Print "Name of nondefault form:", strFormName
        
        Debug.Print "Try to print name of default instance, using name index ..."
        On Error Resume Next
        Debug.Print Forms(strFormName).Name
        If Err.Number <> 0 Then Debug.Print "Error " & Err.Number, Err.Description
        
        DoCmd.Close acForm, strFormName, acSaveNo
        Debug.Print "After DoCmd.Close, Forms count = "; Forms.Count
        
        Err.Clear
        Debug.Print "Attempt reference to nondefault instance via object reference ..."
        Debug.Print frm.Name
        If Err.Number <> 0 Then Debug.Print "Error " & Err.Number, Err.Description
    
    End Sub
    

    I have a form named "Form2", which has a class module. When I run the above procedure, I get the following results in the Immediate Window:

        Before execution, Forms count =  0
        After creating nondefault form, Forms count =  1
        Name of nondefault form:    Form2
        Try to print name of default instance, using name index ...
        Error 2450    Microsoft Office Access can't find the form 'Form2' referred to in a macro expression or Visual Basic code.
        After DoCmd.Close, Forms count =  0
        Attempt reference to nondefault instance via object reference ...
        Error 5       Invalid procedure call or argument

         It wasn't meant as an insult, just a caution

    If you don't know what you're doing about ANY topic you can get yourself into trouble. What's your point? At best, it's a generic comment which is of no help-- you said nothing about VB forms. Your implication is that if i'm not familiar with this one topic, it means I don't know what i'm doing. What course of action does it indicate? 

    I would have thought the implication was that, if you're not familiar with this one topic, you should avoid using the syntax in question.  Your inference is not what I intended.  Again, my apologies for having inadvertently offended you while attempting to help you.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, September 3, 2013 7:33 PM

All replies

  • Please show your code.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Monday, September 2, 2013 6:29 PM
  • Here's the code:
    ?[Form_Flex2 Hospitals].lstHospitals.ItemsSelected.Count

    More Weirdness

    Must Close Twice:
    The form will not close on first try:
    DoCmd.Close acForm, "Flex2 Hospitals"

    but it WILL close if i execute the same code again.
    DoCmd.Close acForm, "Flex2 Hospitals"


    Not Current Object:
    This code returns the name of a hidden form, instead of the open form:
    ?CurrentObjectName
    ' returns name of a hidden form, instead of the visible one. 

    If i attempt to setfocus to this form, CurrentObjectName STILL returns name of a hidden form. 
    [Form_Flex2 Hospitals].SetFocus 
    ?CurrentObjectName
    ' returns name of a hidden form, instead of the visible one. 

    Here are images of the form:

    http://postimg.org/image/ft8enykt9/

    http://postimg.org/image/6wxmk0u71/


    • Edited by johny w Monday, September 2, 2013 7:23 PM
    Monday, September 2, 2013 7:21 PM
  • Here's the code:
    ?[Form_Flex2 Hospitals].lstHospitals.ItemsSelected.Count

    More Weirdness

    Must Close Twice:
    The form will not close on first try:
    DoCmd.Close acForm, "Flex2 Hospitals"

    but it WILL close if i execute the same code again.
    DoCmd.Close acForm, "Flex2 Hospitals"


    Not Current Object:
    This code returns the name of a hidden form, instead of the open form:
    ?CurrentObjectName
    ' returns name of a hidden form, instead of the visible one. 

    If i attempt to setfocus to this form, CurrentObjectName STILL returns name of a hidden form. 
    [Form_Flex2 Hospitals].SetFocus 
    ?CurrentObjectName
    ' returns name of a hidden form, instead of the visible one. 

    Here are images of the form:

    http://postimg.org/image/ft8enykt9/

    http://postimg.org/image/6wxmk0u71/


    It appears that you are occasionally -- frequently? -- using the Form_<form name> syntax to refer to your forms. This syntax has many pitfalls, and clearly it is tripping you up.  The Form_<form name> syntax is a reference to the form's class object, not to the form itself, and has these odd quirks:

    1. It can only be used with forms that have a VBA class module.
    2. If the form isn't loaded when you make this reference to its class module, Access will load a hidden copy of the form and add it to the Forms collection, but will not index it by name until you make it visible.
    3. If you have created such a hidden copy of the form and then use DoCmd.OpenForm to open that form, you will have two copies of the form open:  one indexed by its name, one not.  The one indexed by its name is called the "default instance" of the form, and the one that was opened by reference to its class module is a non-default instance.
    4. You can use this technique to have multiple non-default instances of the same form open, which can be handy sometimes.
    5. However, if you have multiple instances of the form open, you don't know which of them a new Forms_<form name> reference will be referring to.  And don't forget that a form could be open as both a main form and a subform at the same time -- which instance does "Form_MyFormName" refer to?
    6. There's a way to manage this sort of thing to control multiple non-default instances of the form by using a collection of object variables and setting them via the New keyword.
    7. If you don't know what you're doing, you can get yourself into trouble.

    In short, my suggestion to you is to remove all uses of Form_<form name> and replace them with the canonical usage:

    • 'DoCmd.OpenForm "FormName"' instead of 'Form_FormName.Visible = True'
    • 'Forms!FormName.ControlName' or 'Forms("FormName").ControlName' instead of Form_FormName.ControlName
    • 'DoCmd.SelectObject "FormName"' instead of Form_FormName.SetFocus
    • etc.

    I expect that will get rid of your problem with the ItemsSelected.Count, because you will now be looking at the correct instance of the form.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Monday, September 2, 2013 7:55 PM

  • In short, my suggestion to you is to remove all uses of Form_<form name> and replace them with the canonical usage:

    • 'DoCmd.OpenForm "FormName"' instead of 'Form_FormName.Visible = True'
    • 'Forms!FormName.ControlName' or 'Forms("FormName").ControlName' instead of Form_FormName.ControlName
    • 'DoCmd.SelectObject "FormName"' instead of Form_FormName.SetFocus
    • etc.

    I expect that will get rid of your problem with the ItemsSelected.Count, because you will now be looking at the correct instance of the form.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Ok, very interesting and helpful. 

    • 'DoCmd.OpenForm "FormName"' instead of 'Form_FormName.Visible = True'

    This i cannot do in this case, because i must first open the forms in hidden mode, in order to retrieve tag information. This is a wizard, so i add all the forms to a collection, and then use the collection of the forms with .visible to nav thru the wizard steps. I add each form to the collection when i open it using OpenForm. Therefor, my collection should contain the correct instances. 

    I need not use the SetFocus, that was only an attempt to resolve this issue. Your syntax fixes it. 

    "

    1. If the form isn't loaded when you make this reference to its class module, Access will load a hidden copy of the form and add it to the Forms collection, but will not index it by name until you make it visible.

    "

    Will one of these duplicate instances, loaded when i reference it's class module, be counted in Forms.Count, even tho it's not referenced by name?

    [EDIT] --YES, the unnamed instances ARE counted in the Forms.Count collection. That enabled me to trace where the extra instance got opened, using a Watch. 

    thx!

    • Edited by johny w Monday, September 2, 2013 8:43 PM
    Monday, September 2, 2013 8:29 PM
    • 'DoCmd.OpenForm "FormName"' instead of 'Form_FormName.Visible = True'

    This i cannot do in this case, because i must first open the forms in hidden mode, in order to retrieve tag information. This is a wizard, so i add all the forms to a collection, and then use the collection of the forms with .visible to nav thru the wizard steps.

    I don't quite follow you there, as you can certainly open a form invisible using DoCmd.OpenForm:

       DoCmd.OpenForm "FormName", WindowMode:=acHidden

    And then you can show or hide it with:

        Forms!FormName.Visible = True
        ' ...
        Forms!FormName.Visible = False

    Or of course you can use an object reference, such as you might have stored in your collection:

        colMyWizardForms("FormName").Visible = True   ' maybe

    I see from your edit that you have solved the rest of your problems.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Monday, September 2, 2013 8:54 PM
  • If the form isn't loaded when you make this reference to its class module, Access will load a hidden copy of the form and add it to the Forms collection, but will not index it by name until you make it visible.

    my testing suggests you may be incorrect on this point. The command

    docmd.close acForm,[Form_Flex2 Hospitals].Name

    worked on both the hidden duplicate instance, as well as the visible one.

    If you don't know what you're doing, you can get yourself into trouble.

    not sure what the point of this insult was, but you gave such a thorough answer i won't flag it for abuse.

    • Edited by johny w Tuesday, September 3, 2013 3:46 PM
    Tuesday, September 3, 2013 3:44 PM
  • If the form isn't loaded when you make this reference to its class module, Access will load a hidden copy of the form and add it to the Forms collection, but will not index it by name until you make it visible.

    my testing suggests you may be incorrect on this point. The command

    docmd.close acForm,[Form_Flex2 Hospitals].Name

    worked on both the hidden duplicate instance, as well as the visible one.

    I have seen the same thing.  The way I interpret it, DoCmd.Close is not using the name index, but looking through the Forms collection for an item that has the specified name, and closing it if it finds it. That behavior would be consistent with the fact that, if the named object isn't open, no error is raised. However, that's just my speculation based on the observed behavior.

    If you don't know what you're doing, you can get yourself into trouble.

    not sure what the point of this insult was, but you gave such a thorough answer i won't flag it for abuse.

    It wasn't meant as an insult, just a caution to anyone who uses the Form_<form name> syntax. I've fielded lots of questions from people who have run into unintended behavior because they used this syntax without understanding it, usually because they come from a Visual Basic background, in which forms behave differently from Access forms. Sorry if you were offended; it was unintentional, I assure you.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, September 3, 2013 4:01 PM
  • I have seen the same thing.  The way I interpret it, DoCmd.Close is not using the name index, but looking through the Forms collection for an item that has the specified name, and closing it if it finds it.
    if you're correct, how would code requirements or options differ, compared with if the form is indexed by name?
    It wasn't meant as an insult, just a caution
    If you don't know what you're doing about ANY topic you can get yourself into trouble. What's your point? At best, it's a generic comment which is of no help-- you said nothing about VB forms. Your implication is that if i'm not familiar with this one topic, it means I don't know what i'm doing. What course of action does it indicate? 
    • Edited by johny w Tuesday, September 3, 2013 4:51 PM
    Tuesday, September 3, 2013 4:49 PM
  • if you're correct, how would code requirements or options differ, compared with if the form is indexed by name?

    For a nondefault instance, you can't use object references in the format Forms!FormName or Forms("FormName").  Consider this code:

    Sub TestNondefaultForm()
    
        Dim frm As Access.Form
        Dim strFormName As String
        
        Debug.Print "Before execution, Forms count = "; Forms.Count
        
        Set frm = New Form_Form2
        Debug.Print "After creating nondefault form, Forms count = "; Forms.Count
        
        strFormName = frm.Name
        Debug.Print "Name of nondefault form:", strFormName
        
        Debug.Print "Try to print name of default instance, using name index ..."
        On Error Resume Next
        Debug.Print Forms(strFormName).Name
        If Err.Number <> 0 Then Debug.Print "Error " & Err.Number, Err.Description
        
        DoCmd.Close acForm, strFormName, acSaveNo
        Debug.Print "After DoCmd.Close, Forms count = "; Forms.Count
        
        Err.Clear
        Debug.Print "Attempt reference to nondefault instance via object reference ..."
        Debug.Print frm.Name
        If Err.Number <> 0 Then Debug.Print "Error " & Err.Number, Err.Description
    
    End Sub
    

    I have a form named "Form2", which has a class module. When I run the above procedure, I get the following results in the Immediate Window:

        Before execution, Forms count =  0
        After creating nondefault form, Forms count =  1
        Name of nondefault form:    Form2
        Try to print name of default instance, using name index ...
        Error 2450    Microsoft Office Access can't find the form 'Form2' referred to in a macro expression or Visual Basic code.
        After DoCmd.Close, Forms count =  0
        Attempt reference to nondefault instance via object reference ...
        Error 5       Invalid procedure call or argument

         It wasn't meant as an insult, just a caution

    If you don't know what you're doing about ANY topic you can get yourself into trouble. What's your point? At best, it's a generic comment which is of no help-- you said nothing about VB forms. Your implication is that if i'm not familiar with this one topic, it means I don't know what i'm doing. What course of action does it indicate? 

    I would have thought the implication was that, if you're not familiar with this one topic, you should avoid using the syntax in question.  Your inference is not what I intended.  Again, my apologies for having inadvertently offended you while attempting to help you.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, September 3, 2013 7:33 PM
  • ok, thanks for your awesome help. 
    Wednesday, September 4, 2013 6:17 AM