New to Xojo. Trying to set an IF for the save button to update an Existing entry

I am currently under the wing of someone at work that is teaching me Xojo and I have been tasked with the occasional homework to get use to it. I’m stuck on making a contact that is editable to update instead of saving as a new contact. I have a habit of overthinking so any advice towards the right direction would be great.

Hi @Gage_Cook , welcome to the Xojo Community !

As far as I understand, you have a single button that is used to create or update a contact. When you create a new contact you start with an empty window but when you need to edit an existing contact you first need to query this contact and fill the fields in the window. So right at the beginning of the entire process you already know if you will create or edit.

I don’t know the code of the app, but one thing I may guess is that a method will be invoked to have the proper window to be displayed. This method may have a parameter passed. A single parameter is enough to achieve your goal. This parameter would be the pk_ID (key) to a contact to edit. If the parameter is ‘empty’, then you know it’s a new contact, otherwise you need to edit an existing contact. The If would check if a value was passed in the parameter or not.

This is one way to code what you need, that depends on the code that already exists.

If you nee more help, just ask for it !

I use exactly the same window for creating a New contact as well as Editing an existing contact. The Update button is what I use to determine a New or an Update function. By default the button caption is called Create. But if I’m doing an Edit, I change the caption of that button to Update instead.

In the button Method, if the Caption is “Create”, I do an SQL INSERT operation, whereas if the Caption is “Update” I do an SQL UPDATE .

Funny…

1 Window, many buttons…

New      Clears the interface
Create   Create a new Record
Modify   Update the current Record
Delete   Delete the  current Record
Close    Close this window

As others have said, I also use a single button for save and update. Here, I would take a step back and look at the data structure and the overall logic of the process.

  • does the contact have a unique ID, say a contact number? If not, I would recommend that one is used. Just relying on the name is risky: more than one dog is called Fido…
  • When selecting a contact for edit, I would fetch all the contact data, including the unique ID.
  • At save,. it becomes easy: if you have a unique ID (also presumably the database table key), then you update. If the unique Id is nil, then you save and assign a new ID.

I most often use PostgreSQL underneath my applications. I have only a save routine in Xojo, where I feed the data to a prepared statement. In PostgreSQL, the prepared statement executes a function, where I execute either the create or the update of the data… Basically, the save routine in Xojo is dumb and blind. The intelligence is in the PostgreSQL function. Not everyone likes that approach, but it works for me. This concept can be extended to create/update data spread over multiple tables. For example, you update the customer master basic data, and create new records for the customer in a new company code. Here you have a mix of create and update for the same “save” process.

Notice that I dissociate the process and the user interface. This is something that I try to do as much as possible. Distil the process to simplify the user interface. I will not have a create button, an update button, a display button etc. if I can avoid it. My “Save” button just switches to the appropriate branch of the process based on context. Here, others will prefer a different approach. There is not a single best solution. it depends on the context and the purpose of the application.

Now, some people like captions. My applications are used by people speaking different languages within the same organization. Whenever possible , I prefer icons to captions. An icon is easy to explain in any language and users sharing the process “speak the same language” when icons are used. Here, a diskette icon tells everyone that this is a save button. (with younger users, I may have to update this to a USB key…) Again, not everyone likes this approach. Works for me, may not work for you. I do use captions where an icon does not provide an immediate clear indication of the button purpose. And it does happen that users don’t like having an icon on a specific button. It is easy to place a caption instead - provided that the user interface is not too dense and cluttered. And this is a whole other topic.

Evening Gilles!

So I realized that in order for me to update an existing entry I need to create a new method for the “update” portion of the button. I have the code for saving a new contact but am unsure how to go about writing out for updating.

This is the code for saving a new entry

// DBInsertRecord(tblname As String, flds() As String, fldvalues() As String) As Boolean
Var b As Boolean
Var fldNames(),fldVals() As String

// Crate the array of field names
fldNames.Add(“First_Name”)
fldNames.Add(“Last_Name”)
fldNames.Add(“Nickname”)
fldNames.Add(“Middle_Name”)
fldNames.Add(“Address_1”)
fldNames.Add(“Address_2”)
'fldNames.Add(“Zip_Code_ID”)

// Crate the array of field values
fldVals.Add(TextField_FirstName.Text)
fldVals.Add(TextField_MiddleName.Text)
fldVals.Add(TextField_LastName.Text)
fldVals.Add(TextField_Nickname.Text)
fldVals.Add(TextField_Address1.Text)
fldVals.Add(TextField_Address2.text)
'fldVals.Add()

b = DBInsertRecord(“contacts”,fldNames,fldVals)
If b = True Then
MessageBox(“The new contact record was successfully inserted into the contacts database.”)
Else
// Error Message
End If

I’m still learning the functions of Xojo but I know I’m on the right road for this. It’s just putting pen to paper per say that I’m having a hard time with.