Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MDIForm receive focus when MDIChild closes

Status
Not open for further replies.

saramaia

Programmer
Aug 5, 2002
61
0
0
PT
Hi!

I am doing a simple application where I have a MDIForm and a MDIChild (Which is a login/password window).

When I press "OK" in the MDIChild i unload the MDIChild but i can´t find any event on MDIForm that allows me to go get the login/password introduced...

Any idea how i do this?

Thanks in advance
Sara
 
saramaia,

In MDIChildForm:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MDIForm1.Password = Text1.Text
End Sub

In MDI Form:

Option Explicit

Private mstrPassword As String

Private Sub MDIForm_Click()
MsgBox Password
End Sub

Private Sub MDIForm_Load()
Form1.Show
End Sub

Public Property Get Password() As Variant
Password = mstrPassword
End Property

Public Property Let Password(ByVal vNewValue As Variant)
mstrPassword = vNewValue
End Property


vladk
 
Thanks Harleyquinn and vladk ... but what i really wanted to know was what event is fired in MDIForm when i close MDIChild..

Hope i made myself clear.. :)

Thanks in advance..
Sara
 
saramaia,

I don't think such an event exist.

vladk
 
Pick the example I gave in the first thread...

When the user clicks "OK" in the MDIChild i want to do somwthing in the MDIForm.... My question is? How does the MDIForm knows that the "OK" button in MDIChild was pressed?

THe way it is right know, i press OK and then i don´t know where the focus is... I guess this is where events come in... but fow what i see there are no events to catch this, right?

Sara
 
saramaia,

I think you would need to read Harleyquinn's and my responses more carefully. I believe, Harleyquinn gave you right direction and I gave some code to illustrate it.

vladk

 
I think I understand what you're trying to do, Sara. You're trying to automatically execute code on the MDI parent when the user closes the MDI child. But, the MDI parent doesn't expose any native events that allow this, as vladk points out.

I also believe I see where you're running into trouble. Once the user inputs a password and hits ok, you want to unload the form and start executing code in the parent form, and nothing happens.

Well, this is because you're trying to use an MDI form for a purpose it wasn't intended for. MDI forms are intended to work as discrete units, but related visually and logically by existing in a single container. The container is responsible for containing the children, and that's pretty much it. It's the children that are intended to do the work.

What you really need is to show the form modally, and an MDI child form can't be because it's against its nature. You want to show a form, get a result from that form, and move on based on that result. Personally, I wouldn't use an MDI form, rather I would use a regular form set up as a dialog box. (Set the style property to fixed dialog.)

Here's some code from a project. doLogin shows a login dialog and returns false if not successful, true if successful. If true, it sets some global variables with information from the login. I set this up as a separate proc here because I'm working with multiple forms based on the screen size of the client. You might not need to make a separate proc, but could just put similar code in form_load for your main form.

Code:
Private Function doLogin(lCn As ADODB.Connection) As Boolean
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
doLogin = False
With frmLogin
    Set .lCn = lCn
    .Show vbModal
    Select Case .LoginStatus
        Case my3Strikes
            MsgBox "Third login failure; exiting program"
        Case myCancel
            MsgBox "Login canceled; exiting program"
        Case mySuccess
            DealerCode = .DealerCode
            DealerName = .DealerName
            doLogin = True
    End Select
End With
Unload frmLogin
End Function

frmLogin has 2 text boxes: Dealer code (user code, as it were) and password, as well as an Ok and a Cancel button. Here's the code in frmLogin:
Code:
Option Explicit
Enum LogVals
    my3Strikes = 0
    myCancel = 1
    mySuccess = 2
End Enum
Public LoginStatus As LogVals
Public DealerCode As Long
Public DealerName As String
Public lCn As ADODB.Connection

Private Sub cmdCancel_Click()
LoginStatus = myCancel
Me.Hide
End Sub

Private Sub cmdOK_Click()
'check for correct password
Static TryCount As Integer
Dim rs As ADODB.Recordset
If txtDealerCode.Text = "" Then
    MsgBox "Enter a dealer code"
    txtDealerCode.SetFocus
    Exit Sub
ElseIf txtPassword.Text = "" Then
    MsgBox "Enter a password"
    txtPassword.SetFocus
    Exit Sub
End If
TryCount = TryCount + 1
Set rs = New ADODB.Recordset
With rs
    .Open "select dlr_code, dlr_pword, dlr_name from dealeraccounts where dlr_code = '" & txtDealerCode.Text & "'", lCn
    If .EOF And .BOF Then
        If TryCount = 3 Then
            LoginStatus = my3Strikes
            Me.Hide
        Else
            MsgBox "Invalid Password for Dealer Code " & txtDealerCode.Text, , "Login"
            txtPassword.SetFocus
            SendKeys "{Home}+{End}"
            .Close
        End If
    ElseIf txtPassword.Text = !dlr_pword Then
        LoginStatus = mySuccess
        DealerCode = !dlr_code
        DealerName = !dlr_name
        Me.Hide
    ElseIf txtPassword.Text <> !dlr_pword Then
        MsgBox "Invalid Password for Dealer Code " & txtDealerCode.Text, , "Login"
        txtPassword.SetFocus
        SendKeys "{Home}+{End}"
        .Close
    ElseIf TryCount = 3 Then
        LoginStatus = my3Strikes
        Me.Hide
    End If
End With
End Sub

Notice that I didn't bother with property procedures in this case, since I didn't need any functionality that public variables don't provide. Or, maybe it's because I'm lazy.

Anyway, hope this helps.

Bob
 
Thanks a lot Bob... You really got my point...
I know what to do now. :)

Thanks again
Sara
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top