Update code

Good evening. The following code use to work (I believe as it’s from an old program, maybe four years ago) but it doesn’t work in that when a change is made, the record is not updated. Going out and then back into the program shows no change occurred. I’m using an ID to determine the row and test it to be certain the correct row is being updated, which it is. I click on the row to change, place the fields in text fields, make the change and then save the information as below. But the row is not revised to show the changes. From the information I’ve been able to find, the code looks right but it’s evident I’m missing something. Would anyone see what I’m doing wrong.

Thank you for your time and help. Jim Backus

// Revise the record and save to the database
Dim SLivingDB as SQLiteDatabase
Dim rec as DatabaseRecord
Dim rs as recordSet
Dim IDNo as String
Dim ExpenseID as String

// Get the ID of the record to be revised
ExpenseID = IDNo

// Select the Purchases record that has been updated
rs = App.SLivingDB.SQLSelect(“SELECT * FROM Purchases WHERE ID = '” + ExpenseID + “'”)
rs.edit

// Update the database
rs.Field(“ExpenseType”).stringValue = txtExpenseType.text
rs.Field(“PayMethod”).stringValue = txtPayment.text
rs.Field(“Company”).stringValue = txtCompany.text
rs.Field(“Amount”).stringValue = txtAmount.text
rs.Field(“Note”).stringValue = txtNote.text
rs.Field(“Month”).stringValue = txtMonth.text
rs.Field(“Year”).stringValue = txtYear.text

rs.Update
App.SLivingDB.Commit
UpdatePurchasesLB

This looks suspicious. The local variable SLivingDB is not being used.

IDNo is zero because you haven’t initialized it which makes ExpenseID zero, which makes the select statement return nothing. I’m surprised you’re not getting an exception.

Greg O, the IDNo is found when clicking on a row in table. This I do to load the existing record into text fields to allow the user to make changes. I do get a readout of whether the IDNo exists and it does.

Andrew, thank you for the reply. I’m not exactly professional gradeso if you could elaborate a little I’d appreceiate it. Thnak you.

That’s not what your supplied code does.

To support what everyone else has said, but to point at specifics:

At the top of the method, you declare these variables:
Dim SLivingDB as SQLiteDatabase
Dim rec as DatabaseRecord
Dim rs as recordSet
Dim IDNo as String

When you use the database, you are talking to a different object, which is a property of the APP
rs = App.SLivingDB.SQLSelect

First delete the line Dim SLivingDB as SQLiteDatabase from this method - it is creating an unused, unintitialised variable.

Next, and more significantly, you have done the same with Dim IDNo as String

Although you say

The IDNo is found when clicking on a row in table. This I do to load the existing record into text fields to allow the user to make changes.

You are not putting ‘whatever that number is’ into THIS iDNo.
THIS iDNo is defined as a string, is empty, and is not filled before being copied into ExpenseID, which is then itself empty.

delete the line Dim IDNo as String from this method - it is creating an unused, unintitialised variable.

Finally, on the line

ExpenseID = IDNo

You need to fix this so that it uses the IDNo you actually filled with the reference.
It may be App.IDNo?
It may be a global variable - we cant tell here.
But if that line gives you an error when you run it, search the code for where IDNo is actually defined, make sure it is visible to this method (public?)

2 Likes

Jeff and GregO, thank you for the replies. I’ve looked over the instruction manual, read much of it, followed Internet leads to non-existing Xojo pages read comments on the Xojo forum and still I feel like I’m reading a different book than you men are. Serious question - what’s the best way to become somewhat proficient and learn what you’re collectively able to discuss?

Write code.
See errors
Bang head
Change code
Note what worked.
Remember for next time.

Same as every development tool on the planet really… :slight_smile:

In this case, the specific issue (it seems to me) is about the nature of ‘Scope’

A variable declared inside a method exists during that method, and dies afterwards. (there is an exception - ‘static’… which we will ignore for now)
This is the same in every language.

A variable added as a property of a window, is available to all the methods of that window.
A variable added as a property of the App, is avaliable throughout the application, as long as you prefix it with app.(variablename)
A variable created as part of a module, and set to ‘global’ rather than ‘private’, is available to any window and method of your application.

Again - most languages I have used work in this way

Now, the issue here is that is you have a variable -( lets call it Banana rather than IDNo so that the odd name sticks in the mind) , which is on the app, or in a module,
You can set the value to be ‘Cavendish’ anywhere in your app.
You can use the value anywhere else in your app.

BUT if you create ANOTHER variable called Banana, inside a method, anything you do with Banana inside that method will not be using the earlier one available everywhere.
Instead it will be using the one you created locally.
Your local one makes the global one become ‘invisible’ for a while.
Almost as if the variables were actually called globalBanana and localBanana , but you don’t get to ‘see’ the prefix.

Xojo will issue a warning about such ‘shadowing’ of variables if you analyse the project.

1 Like

Was there anything specific in @Jeff_Tullin 's posts that you found confusing?

Everything is making sense now. I’ve got the columns to add correctly so life is good. Thank you all for giving me a hand. It is appreciated. Try to not ask too many other questions. Jeff’s first six lines make sense; seems to always work but I was hoping to find the scholarly approach. Less bumps, more notes.

Again, I appreciate everything you have done for me. Off to buy a helmet.