Jump to content

Dumb Loop Question...


Recommended Posts

I've stared at this for a good hour now and it has condensed to much smaller than it originally was.. I'm trying to validate an employee ID number to a SQL database in order to continue my script. I have the database stuff all finished, but I'm stuck with a stupid loop problem.. The following code is not my exact script by rather some chunks I cut out to demonstrate it.

While 1
$text = InputBox("Please Login", "Please enter your Badge Number and click OK.", "", "*")
$result = "1" ;in the full script this is the result of a SQL query (in an array) but for demonstration purposes it is 1
_Min(25, 10)
If @error = 1 Then ; THIS is where my problem is. InputBox sets @error at 1 if cancel is pressed, but other functions are setting @error before this is checked resulting in it doing nothing. I changed @error with the simple math function above, breaking my cancel button..
  Exit
ElseIf $result <> 0 Then ;my SQL function returns the array of a query here. This would normally be _ArrayDisplay($result) but to demonstrate it I changed it to a MsgBox
  MsgBox(4096, "test", $result)
  ExitLoop ;exit the loop and continue with code after the While loop
ElseIf $result = 0 Then ;the SQLite function I call returns 0 if there is no data in the query
  MsgBox(4096, "Error", "Invalid Data")
EndIf
WEnd

I've stared at it too long now so if anyone has a suggestion to make my loop register the @error from the InputBox to fix the cancel button it would be greatly appreciated. Thanks!

Link to comment
Share on other sites

While 1
$text = InputBox("Please Login", "Please enter your Badge Number and click OK.", "", "*")
$InputError = @error
$result = "1" ;in the full script this is the result of a SQL query (in an array) but for demonstration purposes it is 1
_Min(25, 10)
If $InputError = 1 Then ; THIS is where my problem is. InputBox sets @error at 1 if cancel is pressed, but other functions are setting @error before this is checked resulting in it doing nothing. I changed @error with the simple math function above, breaking my cancel button..
  Exit
ElseIf $result <> 0 Then ;my SQL function returns the array of a query here. This would normally be _ArrayDisplay($result) but to demonstrate it I changed it to a MsgBox
  MsgBox(4096, "test", $result)
  ExitLoop ;exit the loop and continue with code after the While loop
ElseIf $result = 0 Then ;the SQLite function I call returns 0 if there is no data in the query
  MsgBox(4096, "Error", "Invalid Data")
EndIf
WEnd

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

This should work:

While 1
    $text = InputBox("Please Login", "Please enter your Badge Number and click OK.", "", "*")
    If @error = 1 Then Exit ; You can check @error here as no further processing is needed. All other lines will be processed when @error = 0
    $result = "1" ;in the full script this is the result of a SQL query (in an array) but for demonstration purposes it is 1
    _Min(25, 10)
    If $result <> 0 Then ;my SQL function returns the array of a query here. This would normally be _ArrayDisplay($result) but to demonstrate it I changed it to a MsgBox
        MsgBox(4096, "test", $result)
        ExitLoop ;exit the loop and continue with code after the While loop
    Else ;the SQLite function I call returns 0 if there is no data in the query
        MsgBox(4096, "Error", "Invalid Data")
    EndIf
WEnd
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I should've been more specific about that $result line.. it is actually calling a function which ends up changing @error but I didn't include that in my sample.. I'll post the entire thing because it is somewhat difficult to explain.

#include <SQLite.au3>
#include <array.au3>
$text = "1234"
While 1
$text = InputBox("Please Login", "Please enter your Badge Number and click OK.", "", "*")
$result = CheckDB($text)
If @error = 1 Then
  Exit
ElseIf $result <> 0 Then
  _ArrayDisplay($result)
  ExitLoop
ElseIf $result = 0 Then
  MsgBox(4096, "Error", "Invalid login")
EndIf
WEnd

Func CheckDB($text)
Local $hQuery, $aRow
_SQLite_Startup()
_SQLite_Open(@ScriptDir & "db.db")
_SQLite_Query(-1, "SELECT * FROM Employees WHERE SSN='" & $text & "' OR BadgeNumber='" & $text & "'", $hQuery)
_SQLite_FetchData($hQuery, $aRow)
_SQLite_QueryFinalize($hQuery)
If $aRow[0] = "" Then
  Return 0
Else
  Return $aRow
EndIf
EndFunc   ;==>CheckDB

So all of the SQL functions end up changing @error before it checks the input box @error.. to my understanding..

Edited by digiworks
Link to comment
Share on other sites

You could store the @error macro in a variable.

Local $aInputError = @error

You can also manually set the @error macro using SetError() which is in the helpfile.

#include <SQLite.au3>
#include <array.au3>
$text = "1234"
While 1
$text = InputBox("Please Login", "Please enter your Badge Number and click OK.", "", "*")
Global $aInputError = @error
$result = CheckDB($text)
If $aInputError Then
  Exit
ElseIf $result <> 0 Then
  _ArrayDisplay($result)
  ExitLoop
ElseIf $result = 0 Then
  MsgBox(4096, "Error", "Invalid login")
EndIf
WEnd
Func CheckDB($text)
Local $hQuery, $aRow
_SQLite_Startup()
_SQLite_Open(@ScriptDir & "db.db")
_SQLite_Query(-1, "SELECT * FROM Employees WHERE SSN='" & $text & "' OR BadgeNumber='" & $text & "'", $hQuery)
_SQLite_FetchData($hQuery, $aRow)
_SQLite_QueryFinalize($hQuery)
If $aRow[0] = "" Then
  Return 0
Else
  Return $aRow
EndIf
EndFunc   ;==>CheckDB

I hope that helps.

Edited by Decipher
Spoiler

censored.jpg

 

Link to comment
Share on other sites

That works perfectly! I had tried doing it earlier that way but without the Global so that must've been it! Thanks to everyone who contributed to this thread ^^

I'm glad that resolved your issue.

I doubt that the local or global scope of the variable fixed this issue. You could have used dim $variable.

Look at this line:

If $aInputError Then
  Exit

False = 0

True if <> 0 to my understanding.

So the script would always exit if $aInputError is not 0.

InputBox()

@Error 0 = The string returned is valid. 1 = The Cancel button was pushed. 2 = The Timeout time was reached. 3 = The InputBox failed to open. This is usually caused by bad arguments. 4 = The InputBox cannot be displayed on any monitor. 5 = Invalid parameters width without height or left without top.

From the help file, Later.

Edited by Decipher
Spoiler

censored.jpg

 

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...