KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Updating 4D Server via 4D Open
PRODUCT: 4D Open | VERSION: 2003.1 | PLATFORM: Mac & Win
Published On: November 13, 2003

With 4D Open, you can connect to a 4D Server from any 4D application and access, edit, delete, retrieve or update information on the server. When updating information on the server from your 4D application, one pitfall to be careful about occurs during after binding the fields to either variables or local fields. Once the fields are bound, if you load a record on the server side, the values of that record will be bound to the local client variables or local fields you specified in the bind. If you want to update the server record with new information, you will have to change the data that is currently bound to your variables or fields. If you immediately perform an OP UPDATE RECORD, the data you updated on the server will be the same as before

The code below will cause 4D Server to update its record with the same values:

C_TEXT(v1;v2)
C_REAL(v3)

  `Finding and opening a connection to the 4D Server
$err:=OP Find 4D Server(29;"10.96.0.6";serverid)
$err:=OP Open connection(serverid;conid;"gou";"";"";"connection")

  `Define a bind and the fields associated with the bind
$err:=OP Create bind(bind)
$err:=OP Get one field number(conid;"[t1]f1";vtable;vfield1)
$err:=OP Get one field number(conid;"[t1]f2";vtable;vfield2)
$err:=OP Get one field number(conid;"[t1]f3";vtable;vfield3)

$err:=OP Define bind by numbers(bind;vtable;vfield1;0;0;"v1")
$err:=OP Define bind by numbers(bind;vtable;vfield2;0;0;"v2")
$err:=OP Define bind by numbers(bind;vtable;vfield3;0;0;"v3")

  `Sets and access the record
$err:=OP Set access mode(conid;vtable;1)
$err:=OP All records(conid;vtable)
$err:=OP Load record(conid;bind;vtable;$locked)

  `Update the record on the server and terminates the connection
$err:=OP Update record(conid;bind)
$err:=OP Close connection(conid)

The default action of the 4D Open Command, OP LOAD RECORD is to load the current record(s). The bind ID that is passed into this command will force the values of the fields on the server into either the variables defined on the local client machine or the fields in the local record.

If binding of the server table fields to either locally defined variables or local table fields, before the OP LOAD RECORD is executed, then the values defined in the locally defined variables or the local table fields will contain the values retrieved from the server. Any attempt to update the server at this point, will result in updating the server table with the same field values as before.

To prevent this from happening, either remove the call to the OP LOAD RECORD command all together, or create a bind before the call to OP LOAD RECORD and call the bind once again after the call to OP LOAD RECORD.

Here are two solutions:

C_TEXT(v1;v2;vblah;)
C_REAL(v3)
C_LONGINT(vtable;vtemp)

  `Finding and opening a connection to the 4D Server
$err:=OP Find 4D Server(29;"10.96.0.6";serverid)
$err:=OP Open connection(serverid;conid;"server";"";"";"connection")
$err:=OP Set access mode(conid;1;1)
$err:=OP All records(conid;1)

  `Define a temporary bind
$err:=OP Create bind(bind)
$err:=OP Get one field number(conid;"[t1]f1";vtable;vtemp)
$err:=OP Define bind by numbers(bind;vtable;vtemp;0;0;"vblah")

  `Loads the server record into memory, value of the first field is now bound to the variable vblah
$err:=OP Load record(conid;bind;vtable;$locked)

  `Queries local table for primary key values matching variable vblah
QUERY([t1];[t1]f1=vblah)
v1:=[t1]f1
v2:=[t1]f2
v3:=[t1]f3

  `Redefine the same bind and the fields associated with the bind
$err:=OP Create bind(bind)
$err:=OP Get one field number(conid;"[t1]f1";vtable;vfield1)
$err:=OP Get one field number(conid;"[t1]f2";vtable;vfield2)
$err:=OP Get one field number(conid;"[t1]f3";vtable;vfield3)

$err:=OP Define bind by numbers(bind;vtable;vfield1;0;0;"v1")
$err:=OP Define bind by numbers(bind;vtable;vfield2;0;0;"v2")
$err:=OP Define bind by numbers(bind;vtable;vfield3;0;0;"v3")

  `Update the record on the server with the new information and terminates the connection
$err:=OP Update record(conid;bind)
$err:=OP Close connection(conid)

-OR-

C_TEXT(v1;v2;vblah;)
C_REAL(v3)
C_LONGINT(vtable;vtemp)

  `Finding and opening a connection to the 4D Server
$err:=OP Find 4D Server(29;"10.96.0.6";serverid)
$err:=OP Open connection(serverid;conid;"server";"";"";"connection")

  `Sets and access the record, without loading the record
$err:=OP Set access mode(conid;1;1)
$err:=OP All records(conid;1)

  `Sets values to be bound to the fields on the server
v1:="blah blah"
v2:="blahblahblah"
v3:=23.656

  `Define a bind and the fields associated with the bind
$err:=OP Create bind(bind)
$err:=OP Get one field number(conid;"[t1]f1";vtable;vfield1)
$err:=OP Get one field number(conid;"[t1]f2";vtable;vfield2)
$err:=OP Get one field number(conid;"[t1]f3";vtable;vfield3)

$err:=OP Define bind by numbers(bind;vtable;vfield1;0;0;"v1")
$err:=OP Define bind by numbers(bind;vtable;vfield2;0;0;"v2")
$err:=OP Define bind by numbers(bind;vtable;vfield3;0;0;"v3")

  `Update the record on the server with the new information and terminates the connection
$err:=OP Update record(conid;bind)
$err:=OP Close connection(conid)