Results 1 to 13 of 13

Thread: [RESOLVED] Simple form dilemma

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Portland, OR
    Posts
    47

    Resolved [RESOLVED] Simple form dilemma

    Hey,
    I have a form that has a drop down list. I want the form to unload when a user makes a selection, instead of having them have to hit a command button. I tried to do it from the click event, but that gives the 'cant unload in this context' error. What would be a clever way to do this effectivly?
    thanks!

    VB Code:
    1. If Me.cboPlant.ListIndex <> 0 then
    2.          Unload Me
    3.          MsgBox "the user chose " & Me.cboPlant.Text
    4.     End If

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Simple form dilemma

    try using DoCmd.Close instead of Unload Me

  3. #3
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: Simple form dilemma

    It might be as simple as calling the msgbox prior to the unload command...seems to me trying to unload the form and then having the form fire off a message box could be problematic..

    Alternately, you could call a sub in a module.

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Simple form dilemma

    Hmm. The only way that worked for me was the LostFocus event.
    VB Code:
    1. Dim blnUnload As Boolean
    2.  
    3. Private Sub cboPlant_Click()
    4. If Me.cboPlant.ListIndex > 0 Then
    5.     MsgBox "the user chose " & Me.cboPlant.Text
    6.     blnUnload = True
    7.     Text1.SetFocus
    8. End If
    9. End Sub
    10.  
    11. Private Sub cboPlant_LostFocus()
    12. If blnUnload Then
    13.     Unload Me
    14. End If
    15. End Sub
    I had a hidden textbox, and put focus to that when the user selected the right option.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Simple form dilemma

    calling the msgbox after the unload would not affect it, I think. As it would simply skip over it and never actually reach the msgbox

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Simple form dilemma

    Quote Originally Posted by kfcSmitty
    try using DoCmd.Close instead of Unload Me
    What is DoCmd? Has that anything to do with the original question?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Simple form dilemma

    Quote Originally Posted by kfcSmitty
    calling the msgbox after the unload would not affect it, I think. As it would simply skip over it and never actually reach the msgbox
    This is also not true. I believe it wouldn't unload because more window messages had to be sent after the combobox was clicked.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Portland, OR
    Posts
    47

    Re: Simple form dilemma

    regardless of where the msg box is in sequence, the code is all being fired from the click event which does not allow for unloading.
    I tried to use DoCmd.Close, but on compile it said "variable not defined" for DoCmd, do i need to declare it as something?
    I know the hidden text box thing would work, it just seems a little hacked but if worse comes to worse that's what I'll do

  9. #9
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Simple form dilemma

    Hah. There are many things in VB which need a little hack to achieve. Another example is this post, which uses a simlar approach for a different problem

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  10. #10
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Simple form dilemma

    Quote Originally Posted by chemicalNova
    What is DoCmd? Has that anything to do with the original question?

    chem

    Oops, I was thinking VBA, sorry. DoCmd.Close works in VBA in this instance..So maybe if you add the reference "Visual Basic for Applications" it will work?

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Portland, OR
    Posts
    47

    Re: Simple form dilemma

    the reason DoCmd works in a VBA instance is because, as i understand it, in vba you're essentially in a With block whose variable is the application
    so in VBA when you do something like DoCmd.Close , you're really referencing Application.DoCmd.Close
    because this is just a form, there is no applicatiton so there is no DoCmd...but maybe my understanding is wrong but regardless it didn't work that way. It worked really smoothly with chemical nova's little trick, so i think i'll go with that unless something better presents itself

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Simple form dilemma

    VB Code:
    1. Option Explicit
    2. Private mbPS As Boolean
    3.  
    4. Private Sub cboPlant_Click()
    5. If Me.cboPlant.ListIndex <> 0 Then
    6.      MsgBox "the user chose " & Me.cboPlant.Text
    7.      PlantSelected = True
    8.      Command1.SetFocus
    9. End If
    10.  
    11. End Sub
    12.  
    13. Private Sub Command1_GotFocus()
    14.  
    15. If PlantSelected Then
    16.     Unload Me
    17. End If
    18.  
    19. End Sub
    20.  
    21. Public Property Get PlantSelected() As Boolean
    22.  
    23.     PlantSelected = mbPS
    24.  
    25. End Property
    26.  
    27. Public Property Let PlantSelected(ByVal bTF As Boolean)
    28.  
    29.     mbPS = bTF
    30.  
    31. End Property

  13. #13
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Simple form dilemma

    Meh, I forgot the CommandButton has a SetFocus method aswell. Meh, I'm tired. There really is no difference though.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

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