Jump to content

Login GUI with M Access


Recommended Posts

Hello All,

Can anyone help me to find what problem on my script.

When i using id : User1 and password : 321 that GUI can work fine,

but when i using id thienfu or user login that problem come out,

why on main GUI have text " Incorrect User Name or Password and Please Try Again" ?

Below my script and attachment are my database.

Sorry for my bad english, hope u all can understand.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#Include <WinAPI.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>

Const $conProgName = "Test Login"
Const $Db = "test.mdb"
Const $Table = "Login"
Const $conOK = 1, $conEdit = 1, $conAdd = 0

Global $oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
Global $objDBSystem, $dbDatatBase, $recRecordSet

Global $LoginGUI, $MainGUI
Global $Password, $user

If Checkdatabase() = $conOK Then
    loginscreen()
EndIf

Func Checkdatabase()
    $objDBSystem = ObjCreate("DAO.DBEngine.36")
    If $conOK <> FileExists(@ScriptDir & "\" & $Db) Then
        MsgBox(16, $conProgName, "Database File is not found " & @LF & @ScriptDir & "\" & $Db)
        ;Return 0
        Exit
    EndIf
    $dbDatatBase = $objDBSystem.OpenDatabase(@ScriptDir & "\" & $Db)
    $recRecordSet = $dbDatatBase.OpenRecordset("Select * From " & $Table)
    Return $conOK
EndFunc

func loginscreen()
Opt("GUIOnEventMode", 1)

$LoginGUI = GUICreate("Log In", 250, 150) 
GUISetOnEvent($GUI_EVENT_CLOSE, "End")

GUICtrlCreateLabel("User Name :", 10, 10)
$user = GUICtrlCreateInput("", 80, 10, 150, 20)
GUICtrlCreateLabel("Password :", 10, 30)
$password = GUICtrlCreateInput("", 80, 30, 150, 20, $ES_PASSWORD)

$LOGINBUTTON = GUICtrlCreateButton("Log In", 100, 60, 60, "", $BS_DEFPUSHBUTTON)
GUICtrlSetOnEvent($LOGINBUTTON, "login")

GUISetState(@SW_SHOW)

 While 1
        $Msg = GUIGetMsg()
        Switch $Msg
            Case $LOGINBUTTON
                login()
        EndSwitch
WEnd
    
EndFunc 

func login ()
    $username = GUICtrlRead($user)
    $Passwordlogin = GUICtrlRead($password)
    
    $a = $dbDatatBase.OpenRecordset("Select * From Login")
    if ($a.Eof = -1 And $a.Bof =-1)  Then
        GUICtrlCreateLabel("Incorrect User Name or Password", 50, 90)
        GUICtrlCreateLabel("Please Try Again", 90, 110)
    Else
    $count = 0
    While Not $a.EOF
        for $b = $count to $count
        if $a.Fields(1).Value == $username and $a.Fields(2).Value == $Passwordlogin then
        main()
        ExitLoop
        Else
        GUICtrlCreateLabel("Incorrect User Name or Password", 50, 90)
        GUICtrlCreateLabel("Please Try Again", 90, 110)
        EndIf   
        Next
        $count = $count+1
        $a.movenext 
    WEnd
    EndIf
EndFunc

func main ()
    $MainGUI = GUICreate("Main", 500, 500, 570, 215)
    GUISetOnEvent($GUI_EVENT_CLOSE, "End")
    $username = GUICtrlRead($user)
    GUICtrlCreateLabel("Welcome", 10, 10)
    GUICtrlCreateLabel($username, 60, 10)
    GUISetState(@SW_SHOW)
    GUIDelete($LoginGUI)
EndFunc

Func End()
    Switch @GUI_WINHANDLE
        Case $LoginGUI
            GUIDelete($LoginGUI)
            Exit
        Case $MainGUI
            GUIDelete($MainGUI)
            Exit
    EndSwitch
EndFunc

test.rar

Link to comment
Share on other sites

func login ()
    $loggedin = 0                                                                              ;new variable
    $username = GUICtrlRead($user)
    $Passwordlogin = GUICtrlRead($password)
    
    $a = $dbDatatBase.OpenRecordset("Select * From Login")
    if ($a.Eof = -1 And $a.Bof =-1)  Then
        GUICtrlCreateLabel("Incorrect User Name or Password", 50, 90)
        GUICtrlCreateLabel("Please Try Again", 90, 110)
    Else
    $count = 0
    While Not $a.EOF
        for $b = $count to $count
        if $a.Fields(1).Value == $username and $a.Fields(2).Value == $Passwordlogin then
            $loggedin = 1                                                                        ;new line
        main()
        ExitLoop
        Else
            if $loggedin = 0 then                                                                ;new section
                GUICtrlCreateLabel("Incorrect User Name or Password", 50, 90)
                GUICtrlCreateLabel("Please Try Again", 90, 110)
            endif
        EndIf   
        Next
        $count = $count+1
        $a.movenext 
    WEnd
EndIf

#include <ByteMe.au3>

Link to comment
Share on other sites

func login ()
    $loggedin = 0                                                                              ;new variable
    $username = GUICtrlRead($user)
    $Passwordlogin = GUICtrlRead($password)
    
    $a = $dbDatatBase.OpenRecordset("Select * From Login")
    if ($a.Eof = -1 And $a.Bof =-1)  Then
        GUICtrlCreateLabel("Incorrect User Name or Password", 50, 90)
        GUICtrlCreateLabel("Please Try Again", 90, 110)
    Else
    $count = 0
    While Not $a.EOF
        for $b = $count to $count
        if $a.Fields(1).Value == $username and $a.Fields(2).Value == $Passwordlogin then
            $loggedin = 1                                                                        ;new line
        main()
        ExitLoop
        Else
            if $loggedin = 0 then                                                                ;new section
                GUICtrlCreateLabel("Incorrect User Name or Password", 50, 90)
                GUICtrlCreateLabel("Please Try Again", 90, 110)
            endif
        EndIf   
        Next
        $count = $count+1
        $a.movenext 
    WEnd
EndIf

Hi sleepydvdr thanks for reply, but i dunno which script error / wrong.

that func login for check id and password to database if correct then call to main() module,

but i dunno why on main() module have text " Incorrect User Name or Password and Please Try Again", should that text just for incorrect id or password.

sorry for my bad english, hope u can understand.

Edited by thienfu
Link to comment
Share on other sites

Hi sleepydvdr thanks for reply, but i dunno which script error / wrong.

that func login for check id and password to database if correct then call to main() module,

but i dunno why on main() module have text " Incorrect User Name or Password and Please Try Again", should that text just for incorrect id or password.

sorry for my bad english, hope u can understand.

I don't understand. Did you replace the login() function with what I posted? What is happening is you have a loop to check the username / passwords like so:

Loop #1: check thienfu / thienfu

Loop #2: check user / 123

Loop #3: check user1 / 321

The reason why user1 / 321 worked correctly is because it is at the end of file (eof = -1). At that point, it quits checking usernames and passwords.

When the username/password is thienfu / thienfu, it gives a successful login BUT it still continues to check until the eof. On the next loop, it fails the check and gives the message. That is why I added the line:

$loggedin = 1

After a successful password check, the next loop will fail, but if $loggedin = 1 then it does not give any more messages. It only gives the message if $loggedin = 0.

I hope this clarifies.

#include <ByteMe.au3>

Link to comment
Share on other sites

Re-reading your post, I think I understand what you were asking. Continuing what I said before, let's look at thienfu / thienfu:

Loop #1: thienfu / thienfu - correct login. Call main() and it pulls up the new gui. It is not at the eof, so it continues to check usernames / passwords.

Loop #2 user / 123 - incorrect login. You already have the new gui from main() but it has failed the password check on this loop, so you have it to call these two lines:

GUICtrlCreateLabel("Incorrect User Name or Password", 50, 90)

GUICtrlCreateLabel("Please Try Again", 90, 110)

Autoit does not know you meant for those two labels to be only for your login gui. All it knows to do is to add those two labels to whatever gui you have running. At this point, it is currently the gui from the main() function.

Does this help?

#include <ByteMe.au3>

Link to comment
Share on other sites

Re-reading your post, I think I understand what you were asking. Continuing what I said before, let's look at thienfu / thienfu:

Loop #1: thienfu / thienfu - correct login. Call main() and it pulls up the new gui. It is not at the eof, so it continues to check usernames / passwords.

Loop #2 user / 123 - incorrect login. You already have the new gui from main() but it has failed the password check on this loop, so you have it to call these two lines:

GUICtrlCreateLabel("Incorrect User Name or Password", 50, 90)

GUICtrlCreateLabel("Please Try Again", 90, 110)

Autoit does not know you meant for those two labels to be only for your login gui. All it knows to do is to add those two labels to whatever gui you have running. At this point, it is currently the gui from the main() function.

Does this help?

hello sleepydvdr,

ok now i understand :blink: thanks very much, sorry i still newbie ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...