How do I set up a Dialog Result behavior for a form

N

Nyx37

I am trying to set up a logon form that has the same behavior as the
FileDialog. I want have it so that when the logon form closes it sends a
result back to the procedure that called it. I need it to send back which
button was pushed (Logon or Quit), the username, and a boolean of successful
logon. I am at a loss for ideas. Any help would be apprecated.
Thanx
 
D

Dirk Goldgar

In
Nyx37 said:
I am trying to set up a logon form that has the same behavior as the
FileDialog. I want have it so that when the logon form closes it
sends a result back to the procedure that called it. I need it to
send back which button was pushed (Logon or Quit), the username, and
a boolean of successful logon. I am at a loss for ideas. Any help
would be apprecated.

At worst, you can do this using global variables; that is, Public
variables defined at module level in a standard module. However, there
are drawbacks to global variables. An alternative approach is to open
your form in dialog mode -- thus making the calling code wait until the
form is closed or hidden -- but instead of closing it when the user
clicks Logon, just hide it by setting the form's Visible property to
False. If the user clicks Quit, on the other hand, then you close the
form.

Either way, the calling code will then resume, and the first thing it
should do is check whether the form is still open (though invisible) or
not. You can use CurrentProject.AllForms(<form name>).IsLoaded to find
that out. If the form isn't open, then you know the user chose to Quit.
If the form is still open, on the other hand, then you know they clicked
Logon, and can proceed to check controls on the form to determine
whether the logon was successful or not, and the other information you
want to pass back.
 
N

Nyx37

Thankyou, I think I will try the second suggestion. That sounds more inline
with what i want. I assume i can close the form from another module? I have
three paths in which the form is to close: Quit button pressed, Logon
successful, and failed logon limit reached.
 
D

Dirk Goldgar

In
Nyx37 said:
Thankyou, I think I will try the second suggestion. That sounds more
inline with what i want. I assume i can close the form from another
module?

Yes, once the form is hidden, so that code in the calling module can run
again, the code in the calling module can close the form.
I have three paths in which the form is to close: Quit
button pressed, Logon successful, and failed logon limit reached.

In that case, I would probably have the Quit button just close the form,
and the other two options hide the form, with controls on the form set
to a state that indicates which occurred, a successful logon or hitting
the filure limit.

The code in the calling module would look something like this:

'----- start of example code -----
Dim strLogonID As String

DoCmd.OpenForm "frmLogon", WindowMode:=acDialog

' Note: execution won't resume here until frmLogon is
' closed or hidden.

If CurrentProject.AllForms("frmLogon").IsLoaded Then

With Forms!frmLogon

If !chkLogonSuccessful = True Then
strLogonID = !txtLogonID
Else
MsgBox "You failed to logon successfully. Goodbye."
End If

End With

DoCmd.Close acForm, "frmLogon", acSaveNo

If Len(strLogonID = 0) Then
DoCmd.Quit
End If

Else
' The user clicked the Quit button or otherwise closed it
' without logging on.

DoCmd.Quit ' maybe

End If
'----- end of example code -----
 
N

Nyx37

Your seggestion work well. I had to create property get statements to access
the return data because it is not held in a control. The return data is a
boolean (logonQuit) and a class module (logonUser). the logonQuit would work
as a public varable for some reason but works good with a Property Get. The
logonUser works as a property get however I can not check if it's empty or
null. I've tryed:
From_frmLogon.logonUser<>Empty
From_frmLogon.logonUser<>Null
IsEmpty(From_frmLogon.logonUser)=True
and IsNull(From_frmLogon.logonUser) =True
<the if then is omitted>
I either get a referance error or a false negative.

I might try something similar to your latest suggestion.
thanks
 
M

mcescher

Your seggestion work well. I had to create property get statements to access
the return data because it is not held in a control. The return data is a
boolean (logonQuit) and a class module (logonUser). the logonQuit would work
as a public varable for some reason but works good with a Property Get. The
logonUser works as a property get however I can not check if it's empty or
null. I've tryed:
From_frmLogon.logonUser<>Empty
From_frmLogon.logonUser<>Null
IsEmpty(From_frmLogon.logonUser)=True
and IsNull(From_frmLogon.logonUser) =True
<the if then is omitted>
I either get a referance error or a false negative.

I might try something similar to your latest suggestion.
thanks

You've misspelled "From" and used an underscore instead of an bang
(exclaimation point)

Try:
Form!frmLogon!logonUser

HTH,
Chris M.
 
N

Nyx37

Yes Form is misspelled. Form_<form name> is a valid way of referencing a
form only in vba. Anyways, I have verified that I am getting the data but
all methods of checking for null/empty that I normally use does not seem to
work.
 
D

Dirk Goldgar

In
Nyx37 said:
Your seggestion work well. I had to create property get statements
to access the return data because it is not held in a control. The
return data is a boolean (logonQuit) and a class module (logonUser).
the logonQuit would work as a public varable for some reason but
works good with a Property Get. The logonUser works as a property
get however I can not check if it's empty or null. I've tryed:
From_frmLogon.logonUser<>Empty
From_frmLogon.logonUser<>Null
IsEmpty(From_frmLogon.logonUser)=True
and IsNull(From_frmLogon.logonUser) =True
<the if then is omitted>
I either get a referance error or a false negative.

I don't know how you have defined your logonUser class, or property that
returns it. However, if this is an object, it may be that you just need
to check for whether the object is Nothing, which you might do like
this:

If Forms!frmLogon.logonUser Is Nothing Then

Note: although the syntax Form_frmLogon works (as a reference to the
form's class module), I don't recommend using that syntax habitually.
There are two drawbacks: first, if there are multiple instances of the
form open, you don't know which one you are referring to; and second, if
the form is not open, that syntax will create an instance of it, rather
than returning an error.
 
N

Nyx37

Thanks Dirk. I was not aware of those drawbacks. I will make the changes
later as I am going to work.
 
N

Nyx37

Thank you for all the help. I got it working. I was using "Is Not Nothing"
and the not was causing some errors. I changed things around and it worked.
Here is the code if interisted:
Public Function Main()
.....
If CurrentProject.AllForms("frmLogon").IsLoaded Then
'frmLogon Hidden
Set m_CurrentUser = Forms!frmLogon.logonUser
If m_CurrentUser Is Nothing Then
'Logon circumvented
MsgBox "Logon circumvented.", vbCritical
DoCmd.Quit
Else
'Logon Successful
DoCmd.OpenForm "frmMain", acNormal, , , , acWindowNormal
End If
DoCmd.Close acForm, "frmLogon", acSaveNo
Else
'Logon was closed
DoCmd.Quit
End If
 
D

Dirk Goldgar

In
Nyx37 said:
Thank you for all the help. I got it working. I was using "Is Not
Nothing" and the not was causing some errors.

Yep, that doesn't work. If you must use negative logic, you have to
write

If Not (SomeObjectName Is Nothing) Then
I changed things around and it worked.

Great! Thanks for posting the code, to complete the cycle.
 

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