Thankyou for the offer Rick, I’ve changed it already now and everything is good.
The original question came about because i was coding to add a new UserID in a database of existing users. It included doing a check to see if the new UserID already existed.
Having supplied the requested UserID to the function, I then used the standard database method to fetch all records from the Users table and do a compare; the ResultCode was set depending on the following conditions :
- Does the database open correctly (db.Connect)
- Does the SQL work OK (db.Error)
- Do any users exist at all (Different action if this is the first ever user to be added)
- If Users exist, do any of them match the newly supplied User ID
Using Return worked for the most part, but failed in the While… Wend, which itself was within an If… Then… Else.
So I moved the Return statement outside of the While… Wend but it was still within the If… Then… Else. Thats where my original question came from. The code I posted here originally was poor and I put ‘Exit’ in the code without having checked it properly first. Note to self : Check code before posting in future
Having said all that, I have changed the code anyway to be more efficient.
I query the database first using ‘Select Count(*)’, which answers number 3 above. If the result here is zero then no further checking is needed since we know no users exist. At this point I close the RecordSet and Database, then exit gracefully with a ResultCode.
If that result is a non-zero value, I then setup a simple SQLitePreparedStatement, using the newly requested UserID as the search parameter.
A zero result here tells me some users do exist but do not match the supplied one. Of course a non-zero result here means the UserID does indeed exist already.
If either of the above two conditions are met, I can still close the database and recordset then specify the ResultCode cleanly.
Searching the database for the supplied UserID was more efficient overall than looping through every record and doing a compare, even if it means 2 queries instead of one.
The function is currently defined as :
CheckUserNameExists(NewUserID As String) As Integer
And the code :
[code] Dim ResultCode As Integer
Dim UserDatabase As New SQLiteDatabase
UserDatabase.DatabaseFile = GetFolderItem(“MyDatabase.sqlite”)
If UserDatabase.Connect Then
Dim UserCount As RecordSet
UserCount = UserDatabase.SQLSelect("Select Count(*) From Users")
If UserCount.IdxField(1).IntegerValue = 0 Then ' No users currently exist
UserCount.Close
UserDatabase.Close
ResultCode = 202
Else ' We have users so check if any match our supplied input
Dim CheckMatchingUsers As SQLitePreparedStatement = UserDatabase.Prepare("SELECT * FROM Users WHERE Username =?")
CheckMatchingUsers.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
Dim UserExists As RecordSet = CheckMatchingUsers.SQLSelect(NewUserID)
If UserExists.RecordCount = 0 Then ' The UserID does not already exist
ResultCode = 203
Else ' The UserID already exists
ResultCode = 204
End If
UserCount.Close
UserExists.Close
UserDatabase.Close
End If
Else
ResultCode = 201 ’ Unable to find/connect to database
End If
Return ResultCode[/code]
Here I have used ResultCode’s starting at 201 and increasing, so they do not conflict with the SQLite error codes. The only thing left to do is add error checks after the 2 SQL statements. If either of these are non-zero then the SQLite error code will be returned as the ResultCode.
If ayone can suggest further improvements to the above, then please feel free to post them.