-
Aug 4th, 2005, 09:53 AM
#1
Thread Starter
Member
[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:
If Me.cboPlant.ListIndex <> 0 then
Unload Me
MsgBox "the user chose " & Me.cboPlant.Text
End If
-
Aug 4th, 2005, 09:58 AM
#2
Re: Simple form dilemma
try using DoCmd.Close instead of Unload Me
-
Aug 4th, 2005, 10:02 AM
#3
Fanatic Member
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.
-
Aug 4th, 2005, 10:04 AM
#4
Re: Simple form dilemma
Hmm. The only way that worked for me was the LostFocus event.
VB Code:
Dim blnUnload As Boolean
Private Sub cboPlant_Click()
If Me.cboPlant.ListIndex > 0 Then
MsgBox "the user chose " & Me.cboPlant.Text
blnUnload = True
Text1.SetFocus
End If
End Sub
Private Sub cboPlant_LostFocus()
If blnUnload Then
Unload Me
End If
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
-
Aug 4th, 2005, 10:04 AM
#5
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
-
Aug 4th, 2005, 10:06 AM
#6
Re: Simple form dilemma
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
-
Aug 4th, 2005, 10:07 AM
#7
Re: Simple form dilemma
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
-
Aug 4th, 2005, 10:16 AM
#8
Thread Starter
Member
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
-
Aug 4th, 2005, 10:23 AM
#9
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
-
Aug 4th, 2005, 10:24 AM
#10
Re: Simple form dilemma
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?
-
Aug 4th, 2005, 10:33 AM
#11
Thread Starter
Member
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
-
Aug 4th, 2005, 10:37 AM
#12
Re: Simple form dilemma
VB Code:
Option Explicit
Private mbPS As Boolean
Private Sub cboPlant_Click()
If Me.cboPlant.ListIndex <> 0 Then
MsgBox "the user chose " & Me.cboPlant.Text
PlantSelected = True
Command1.SetFocus
End If
End Sub
Private Sub Command1_GotFocus()
If PlantSelected Then
Unload Me
End If
End Sub
Public Property Get PlantSelected() As Boolean
PlantSelected = mbPS
End Property
Public Property Let PlantSelected(ByVal bTF As Boolean)
mbPS = bTF
End Property
-
Aug 4th, 2005, 10:44 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|