Change the text of a label on a modal form

  • Thread starter Nicholas Scarpinato
  • Start date
N

Nicholas Scarpinato

Hello. I'm having a bit of a hard time wrapping my head around exactly why
this isn't working the way I have it written, because I'm quite sure I've
done this very same thing before and it worked just fine. What I'm trying to
do is use a single form to display a variety of system messages
programmatically, but I want this form to stop the code until the user clicks
the OK button. I don't want to put an infinite loop code into the form
because that seems rather wasteful, and I know a modal form stops the code
from running until the user closes the form... but therein lies the problem.
What's happening is that since the modal property of the form stops the code
from executing, the form opens and stops the code before the line that
changes the text of the label has a chance to be processed. Then when the
form is closed I get the "Access cannot find the form" message. If I open the
form in hidden mode first and change the label and then re-open it in it's
normal state (without saving it and closing it first), the label changes but
the code executes in the background. I'm starting to think that the only way
to solve this is to open the form in hidden mode, make the appropriate
changes, save it, then reopen it again in it's normal mode. I just don't want
to do all that if I can help it.

Here's a sample of the current code:

DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog
Forms![Bin Confirmation Form].[BinSelectionText].Caption = "Bin location
found!" & vbNewLine & "Please use bin " & BinLoc & "."
Forms![Bin Confirmation Form].Modal = True

Am I missing something that should be blatantly obvious, or is there really
no other way to do what I'm trying to do than to save the form and reopen it?
 
N

Nicholas Scarpinato

Albert, in case you're reading this, your article about dialogs and modals
answered a lot of questions I've had about forms in general (and reminded me
of some things I had forgotten)... but I'm still stuck on this problem. I
also see where some of my code is completely unnecessary as well.
 
G

Graham R Seach

Nicholas,

If you want code to run after you've opened the form, it can't be opened
using acDialog, because that makes it modal. This may work for you:

DoCmd.OpenForm "Bin Confirmation Form"

Forms![Bin Confirmation Form]![BinSelectionText].Caption = _
"Bin location found!" & vbNewLine & "Please use bin " & BinLoc & "."

Forms![Bin Confirmation Form].Modal = True

Although making the form modal *after* the form is open will work from the
user's perspective, it is useless from the point of view of preventing
subsequent code from running in the module that called it.

What I'd suggest you do is open the form modally (using the acDialog
argument), then set the Label's caption in the form's Current event. That
way, you get the best of both worlds.

Public SomeSub As Sub()
DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog
End Sub

'In the form...
Private Sub Form_Current()
Me![BinSelectionText].Caption = "Bin location found!" & _
vbCrLf & "Please use bin " & BinLoc & "."
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia


Nicholas Scarpinato said:
Hello. I'm having a bit of a hard time wrapping my head around exactly why
this isn't working the way I have it written, because I'm quite sure I've
done this very same thing before and it worked just fine. What I'm trying
to
do is use a single form to display a variety of system messages
programmatically, but I want this form to stop the code until the user
clicks
the OK button. I don't want to put an infinite loop code into the form
because that seems rather wasteful, and I know a modal form stops the code
from running until the user closes the form... but therein lies the
problem.
What's happening is that since the modal property of the form stops the
code
from executing, the form opens and stops the code before the line that
changes the text of the label has a chance to be processed. Then when the
form is closed I get the "Access cannot find the form" message. If I open
the
form in hidden mode first and change the label and then re-open it in it's
normal state (without saving it and closing it first), the label changes
but
the code executes in the background. I'm starting to think that the only
way
to solve this is to open the form in hidden mode, make the appropriate
changes, save it, then reopen it again in it's normal mode. I just don't
want
to do all that if I can help it.

Here's a sample of the current code:

DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog
Forms![Bin Confirmation Form].[BinSelectionText].Caption = "Bin
location
found!" & vbNewLine & "Please use bin " & BinLoc & "."
Forms![Bin Confirmation Form].Modal = True

Am I missing something that should be blatantly obvious, or is there
really
no other way to do what I'm trying to do than to save the form and reopen
it?
 
M

monma01 via AccessMonster.com

You might also want to create the label caption before you open the dialog
form, and pass the string as an OpenArg to the dialog form.

For instance:

strLabelText = "Bin location found!" & vbNewLine & "Please use bin " & BinLoc
& "."
DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog, strLabelText

Then in the OnOpen event of your dialog form, you can use the OpenArgs
property to set the label, something like this:

Me.BinSelectionText.Caption=Me.OpenArgs
 
N

Nicholas Scarpinato

That sounds like it might be exactly what I need! Thank you very much, I'm
going to try that out right now and see if I can get this working. :)
 
N

Nicholas Scarpinato

Worked like a charm, thank you!

Nicholas Scarpinato said:
That sounds like it might be exactly what I need! Thank you very much, I'm
going to try that out right now and see if I can get this working. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top