Question

In continuation to my application development, I have encountered another intersting problem.

In my MS Access application, I am using a form which contains only two controls - a textbox and a command button. This form is named as HEADER FORM.

HEADER FORM is used as a subform in header section of various other forms.

Figure below shows the subform and its use in one of the main form.

enter image description here

Now, what I want is that when user clicks on Logout button of the subform, the logout action shall happen (which is fine and I am able to figure it out) and all the forms that are open at that point in time shall be closed / unloaded.

I tried using following code to no avail.

Option Compare Database

Private Sub cmdHeaderLogout_Click()
    If (loggedIn = 1) Then
        loggedIn = 0
        DoCmd.Close acForm, Parent.Form
    End If
End Sub

When I try to click on logout button, control reaches DoCmd.Close statement above. And execution stops after giving following error message:

Run-time error: 2498. An expression you entered is the wrong data type for one of the arguments.

and it points to Parent.Form text.

I am not able to figure out how to refer to parent form and unload the same?

Était-ce utile?

La solution

You should refer to the name of the parent of the current form, that is:

DoCmd.Close acForm, Me.Parent.Name

The second argument of DoCmd.Close takes a string.

You can also loop through the forms collection.

FormsCount = Forms.Count - 1

For i = FormsCount To 0 Step -1
    If Forms(i).Name <> Me.Name Then
        DoCmd.Close acForm, Forms(i).Name
    End If
Next
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top