Where is db declared? Not in the InsertNewCustomer() method, evidently, so that is obviously going to puzzle the compiler as it’s not within the method’s scope, with the effects you see. You either:
pass db through as an argument to the InsertNewCustomer() method; or:
declare it in the module or window of which InsertNewCustomer() is a method. Then it will be within scope for that method.
Private Function InsertNewCustomer(db As SQLiteDatabase) As Boolean
If db = Nil Then Return False
Try
If db.Connect Then
db.ExecuteSQL("INSERT INTO customers (custCompany, custAddress, custPostcode, custCheckbox, custDelCompany, custDelAddress, custDelPostcode, custName, custTel, custMob, custEmail, custNotes ) " + _
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", _
CustCompName.Text, _
CustCompAdd.Text, _
CustCompPostcode.Text, _
CustCompCheck1.Text, _
CustDelName.Text, _
CustDelAdd.Text, _
CustDelPostcode.Text, _
CustName.Text, _
custTel.Text, _
custMob.Text, _
custEmail.Text, _
custNotes.Text)
Return True
End If
Catch err As DatabaseException
// Do your DB Errorhandling here...
End Try
Return False
End Function
Assuming “db” is a Property of “App” and a Public Property, you could now do a
Hi thanks for replying. I’m afraid I’m not yet at the level of understanding your reply.
in my pressed event handler i’m simply typing InsertNewCustomer as this is what my method is called.
Should i type something else in the pressed event handler?
Your InsertNewCustomer method has a return type set. If it has a return type you must make use of that value. For example if your method was:
Function InsertNewCustomer( somedata ) as Boolean
// Some code
Return True
End Function
Then you must you cannot simply say
// Invalid
InsertNewCustomer( SomeData )
// Valid
if InsertNewCustomer( SomeData ) then
end if
// Also valid
Var bResult as Boolean = InsertNewCustomer( SomeData )
If your method was:
Sub InsertNewCustomer( SomeData )
// Insert the values
End Sub
// Valid
InsertNewCustomer
ahh ok. thank you. Well am i being too simplistic by thinking… one of the two options…
1, use the return to show a message saying something like “Success”
or…
2, remove the return bit and actually have the window close (i actually prefer this option)
or am i misunderstanding the purpose of the return?
The return value is up to you. It can do what ever you want. However, if you are going to return success or failure then I would use Boolean as the return type. This has two values True and False.
If the insert succeeds then return True, otherwise return False.
The in your call to InsertNewCustomer you can do the following:
If InsertNewCustomer( SomeValues ) then
// All worked and you can
Self.Close // close the window
else
// Present some sort of error message and do not close the window
end if
It can be nice for the user to have some sort of feedback that the operation worked. If your main window has a list of customers that will be updated once the Insert window is closed then it is perfectly OK to close it.
Leaving the window open if it worked can also confuse the user, making them thing the change hasn’t happened.
Lets say your customer database consists of a Code and a Name (I know it would have more than that but for now)…
In your new customer window you can layout the controls for the information, allow the user to edit them and then some sort of Add or OK button. In the OK button pressed event you should read the contents of the window and then pass them to the method.
The Method would then deal with storing them into the database returning a value to indicate success or failure. So your method would look like this:
Function InsertNewCustomer( Code as Integer, Name as String ) as Boolean
// What ever code is required to save the data into your database etc.
return True or False
End Function
On the window, the Pressed event of the OK/Add button you have something like the following:
If InsertNewCustomer( edtCode.text.ToInteger, edtName.text ) then
self.close
else
labelError.Caption = "Unable to save"
end if
If you have your InsertNewCustomer code within the window then it would be possible to access the control directly, however, as a general rule keep the interface details away from the database details.
ps. It would also be worth adding some error checking before calling InsertNewCustomer so you don’t get obvious issues. For example if they use the name blank or miss some sort of vital information.
right… so ive tried wrapping my method with the function like you said above but i’m getting a syntax error. On my method I also have a ‘Try’ so I’m not entirely sure where abouts to slot the function in…
you have used: (Code as Integer, Name as String ) as an example. so just for simplicity sake can i just change that to custCompany which is the database column? The corresponding text field is called custCompName.
so would it look like this?
Function InsertNewCustomer( custComapny as String ) as Boolean
// What ever code is required to save the data into your database etc.
return True or False
End Function
and then the Pressed Event would look like this?
If InsertNewCustomer( custCompany.text ) then
self.close
else
labelError.Caption = “Unable to save”
end if
Just for info you are better pressing the </> button when posting code, rather than the quote button. That’s how I get the nicely formatting text, colouring etc.
So, just a few things. A method that returns a value is internally labelled as Function. One that doesn’t as Sub. I use them here because you can actually copy and paste them into Xojo.
Try statements are a good thing to include so that errors are captured. They only operate within the scope of a single Method. They have three parts:
Try
// Some code to do the work
Catch oError as SomeRuntimeExceptionClass
// Deal with things that happen in the try section here
End Try
You can actually have more than one Catch block for different types of Exception. Can you show me the syntax error or provide the code that causes it?
Syntax errors are the “easiest” ones to deal with, they simply mean the the code is wrongly structured, has a typo or other such things. For example If you typed “Fi” instead of “If” that would be a syntax error.
If you want to send me your program or your code I can take a look. One thing:
return True or False
Is it’s self a syntax error. You can only return one value, so either “Return True” or “Return False”. It was meant to indicate what do depending upon circumstances.
It’s really important that you understand the code you write/use. In the long run it will not be enough to copy our code and only understand our snippets.
yes i completely understand what you’re saying. But i really needed this starting point from you guys some i’m very grateful. This has given me something to start with. thanks again