Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Suddenly can't close form with DoCmd.Close

Posted on 2009-02-15
7
Medium Priority
?
742 Views
Last Modified: 2013-11-28
Hi

I have a login form that opens a main form when the user has identified him/herself. My login form was very simple, just a list of users in a combobox, user would click on his/her name in the list, and on the combobox click event, I would open the main form, and close the login form with DoCmd.Close acForm, <login form name>, acSaveno

I now added in the login form the entry of a password in a textbox, and, on the Exit event processing of the password text box, I compare the entered pw with the pw in the DB, and if the password is OK,  I open the main form and close the login form with the same DoCmd's as above.

However, this DoCmd.Close now refuses to work and gives me an error "This action can't be carried out while processing a form or report event".

Why ?

I don't see any damn difference between the previous click event on the user list combobox and an Exit event on the password text box. Both are events. Why does the close work on the first event, and not on the second one ?

How can I close that login window now ??

Thanks for help.

Bernard
0
Comment
Question by:bthouin
  • 4
  • 2
7 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 23645613
bthouin,

First try running the compact/Repair utility.

Can you post all of the code on this form?

(I'll be glad to help, as long as you promise not to curse at me.)
;-)

JeffCoachman
0
 
LVL 1

Author Comment

by:bthouin
ID: 23645748
Hi Jeff

I  also thought it was weird and run the compact/repair, but that didn't help.

Here is the code in the login form. The error happens on the DoCmd.Close, the last statement before End Sub.

The form has 3 controls, 1 combobox cbUser (user list), two text boxes, txtPW (password) and txtRole (shows user role from DB). That's all.

No cursing, promised...
Bernard

Option Explicit

Option Compare Database

Public sUser As String

Private Sub cbUser_Click()
    sUser = Me.cbUser
End Sub

Private Sub txtPW_Exit(Cancel As Integer)
    Dim rs As Recordset
    Dim sTemp As String

    Set rs = CurrentDb.OpenRecordset("SELECT userRole, pw FROM tbUsers WHERE userName = '" & sUser & "'", dbOpenDynaset)
    If IsNull(rs.Fields!pw) Then
        CurrentDb.Execute ("UPDATE tbUSers SET pw = '" & Me.txtPW& "'WHERE userName = '" & sUser & "'")
        MsgBox "Password stored", vbOKOnly, "Info"
    Else
        If rs.Fields!pw <> Me.txtPW Then
            MsgBox "Wrong password, try again", vbOKOnly, "Error"
            Me.txtPW = ""
            Exit Sub
        End If
    End If
    Me.txtRole = rs.Fields!userRole
    rs.Close
    Set rs = Nothing

    DoCmd.OpenForm "frmMain", , , , , , sUser
    DoCmd.Close acForm, "frmLogin", acSaveNo
   
End Sub
0
 
LVL 42

Expert Comment

by:dqmq
ID: 23645837
I can't explain that error, only to say that I've had the same problem.  My workaround is to use the OnClick event of a Login button.
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 23647443
bthouin,

A few notes about you login system:

- Can I ask why you are using the textbox Exit Event to do this?
Like dqmq, I use a "Login" button to do this.

- The user name should be just as secure as the password. You should not have a dropdown listing all the user names.
:-O
For a Hacker, getting the user name is half the battle.
Users must know their user name, or they don't log in.
Just like if you don't know your Credit Card Number, you can't make a purchase.
(Remember, we are talking about "security" here, right?)

- The "Role" should be assigned to the user. They should not have to provide it.
If they have to provide it, then it forces them to memorize it. (Giving them one more thing to forget and call you up about)
;-)
If they login successfully, (User name and password), then the role can be "looked up".
(Avoiding confusion if users enter the wrong Role)
If you notice, most security systems will only ask for a UserName and a Password.
Your role is always "inherited" from your successful login.

There are many way to do this, depending on the level of security you need.

Here is the basic system I use.

;-)

JeffCoachman

Access-Basic-LoginLogOnUserNameP.mdb
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 23647449
Note, my above sample file just considers the "LogIn"

You will have to create your own system to kick/lock them out of the database, if they do not log in successfully.
;-)

JeffCoachman
0
 
LVL 1

Author Comment

by:bthouin
ID: 23663865
Hi Jeff

Of course I know everything you are talking about. However, I'm not building a Fort Knox login, but just a very simple minimum security login for a local app, where I do not fear hackers, but just unknowing users that could enter some daft data in the DB. Hence the user list. And the role is pulled out of the DB, not entered, of course. A textbox doesn't necessarily means input, does it ? And of course I kick users out if they do not provide the right password.

That still leaves the original question totally unanswered why an Exit event on a textbox is treated differently from a click event on a pushbutton, and leads to an error when subsequently a DoCmd.Close acForm is issued. IMHO, this is a simple bug from Access.

I of course added a login pushbutton in the meantime, just to get the thing working, but nobody shall tell me that this is entirely justified. What's the value of an additional button to click after you've entered your correct password ? Do you do that when you log into Windows ? No, you just just hit Return... so here is your answer: even Windows is able to react on an Exit event and close a dialog box, but Access can't do that on a form...

I do lots of processing based on On Exit events, and that can be extremely useful. Of course these event processing are affecting open forms or subforms, so that's not the same than issuing  a close command. That still doesn't explain the difference.

Bernard
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 375 total points
ID: 23668820
bthouin.

"That still leaves the original question totally unanswered why an Exit event on a textbox is treated differently from a click event on a pushbutton"

Again, as dqmq and I both stated, using the OnExit event is not the way this is typically done.

The Exit event is hard to "Catch" and is sometimes confused with the Lost Focus event.  It is not always clear what "State" the value is in immediately before or after this event.
This seems to be what is happening in this case.
If you like using the Exit event, then you have to find a way to work around its shortcomings, like this one.

The theory behind using a button is that, all the data gets collected, and then you click "Login".
You can do all of your "processing" and validation on the button click event.

"I of course added a login pushbutton in the meantime, just to get the thing working, but nobody shall tell me that this is entirely justified. What's the value of an additional button to click after you've entered your correct password ? "

I have never seen a login screen/system in a commercial application or website that logs you in just by entering the correct username and password.
You have to click some sort of "OK", or "Login" button.
Ex: Windows, Citrix, Excel, Amazon.Com, ...ect
So the issue here is consistency with existing applications, and user "expectations".

If you have your own way of doing things, then I respect that.

If you are looking for the technical "Differences", you will have consult the Help files on the specific events in question.

;-)

JeffCoachman
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question