Sub EMailForm(ID As Int)
' Define the form field values with default values --- without the ID fields (as automatically updated)
Dim idvalue As String = "" 'ignore
Dim namevalue As String = "new name"
Dim emailvalue As String = "new email"
Dim notesvalue As String = "new content"
' Set the OK button text
If ID = -1 Then
Dlg.OKButtonText = "Add"
Else
Dlg.OKButtonText = "Update"
End If
' Create the list of fieldmaps
Dim fieldlist As List
fieldlist.Initialize
' Get the record values if ID <> -1 and assign to the values
Dim fieldMap As Map = DBUtils.ExecuteMap(sqlObj, $"SELECT * FROM ${TableContacts} where ID=?"$, Array As String(ID))
If fieldMap.IsInitialized Then
idvalue = fieldMap.Get("id")
namevalue = fieldMap.Get("name")
emailvalue = fieldMap.Get("email")
notesvalue = fieldMap.Get("notes")
End If
' Create the field list with new or updated values
fieldlist.Add(CreateMap("id":"name", "label":"Name","value":$"${namevalue}"$, "type":"T"))
fieldlist.Add(CreateMap("id":"email", "label":"E-Mail","value":$"${emailvalue}"$, "type":"T"))
fieldlist.Add(CreateMap("id":"notes", "label":"Notes","value":$"${notesvalue}"$, "type":"A"))
' fieldlist.Add(CreateMap("id":"id", "label":"ID","value":$"${idvalue}"$, "type":"T"))
'Open the dialog
Dlg.SimpleFormSplitFields = cbSimpleFormSplitFields.Checked
Dim resultMap As Map = Dlg.SimpleFormDialog("Contact Form", "Details", fieldlist)
If resultMap.IsInitialized Then
If ID = -1 Then
Dim l As List
l.Initialize
l.Add(resultMap)
Dim rt As String = DBUtils.InsertMaps(sqlObj, TableContacts, l)
FillTableView
LOg($"New record inserted. Result: ${rt}"$)
Else
Dim wherefieldequals As Map = CreateMap("ID":ID)
Dim rt As String = DBUtils.UpdateRecord2(sqlObj, TableContacts, resultMap, wherefieldequals)
FillTableView
Log($"Record (ID:${ID} updated. Result: ${rt}"$)
End If
End If
End Sub
Sub FillTableView
tvContacts.Items.Clear
DBUtils.ExecuteTableView(sqlObj, $"SELECT * FROM ${TableContacts}"$, Null, 0, tvContacts)
End Sub