PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → New Unique ID problem
New Unique ID problem
Débuté par Garry, 28 fév. 2017 23:09 - 2 réponses
Membre enregistré
30 messages
Posté le 28 février 2017 - 23:09
I have been working on this all day and cannot find a way to solve it, although it seems it should be easy.
For historical reasons I would like to use my own unique ID's for a filed in one file rather than AutoID's.
The problem is that I can't seem to perform any operations on a file while it is in use in an edit window although I experimented with HSavePosition/HRestorePosition unsuccessfully.

So... i created a LOCAL Procedure and used an alias
PROCEDURE New_Comp_ID()
sID is string
dsCompAlias is Data Source
IF HAlias(RBL_COMP,dsCompAlias) THEN
sID = Upper(Left(EDT_CompNAME,3))
sExt is string
i is int
IF HReadSeek(dsCompAlias,"COMP_ID",sID) THEN // BUT THIS LINE DOESN'T WORK!
WHILE dsCompAlias.COMP_ID [= sID
HReadNext()
END
HReadPrevious()
sExt = Right(dsCompAlias.COMP_ID,3)
i = Val(sExt) +1
sID = sID +NumToString(i, "03d")
ELSE
sID += "001"
END
END

RESULT sID

I have probably been working on this too long and I need some help. Surely someone else has dealt with this problem!

--
Garry
Posté le 01 mars 2017 - 10:40
Hi Garry,

The problem is that I can't seem to perform any operations on a file
while it is in use in an edit window although I experimented with


I agree, it is a problem, and I don't see why you would have this
problem, except of course if you are locking the record or file by code
when entering the window. No automatic mechanism will prevent you from
creating or modifying a record from inside a window.

So... i created a LOCAL Procedure and used an alias
PROCEDURE New_Comp_ID()
sID is string
dsCompAlias is Data Source
IF HAlias(RBL_COMP,dsCompAlias) THEN
sID = Upper(Left(EDT_CompNAME,3))
sExt is string
i is int
IF HReadSeek(dsCompAlias,"COMP_ID",sID) THEN // BUT THIS LINE
DOESN'T WORK!


Halias is not enough. It is just defining a NAME for a file. You then
have to link that name to your file connection. This is described in the
help.

best regards

--
Fabrice Harari
International WinDev, WebDev and WinDev mobile Consulting

Ready for you: WXShowroom.com, WXReplication (open source) and now WXEDM
(open source)

More information on http://www.fabriceharari.com
Membre enregistré
30 messages
Posté le 01 mars 2017 - 15:41
Perhaps I am solving the wrong problem.
First: I am doing no locking.

The code for opening my edit window is:
PROCEDURE MyWindow(LOCAL psComp_ID is int = 0)

gbModifyMode is boolean
IF psComp_ID > 0 THEN
gbModifyMode = True
ELSE
gbModifyMode = False
END
IF gbModifyMode = False THEN
HReset(RBL_COMP)
END
FileToScreen()

and the code on my Save Button is:
ScreenToFile(WIN_CompanyEdit)

IF gbModifyMode THEN
HModify(RBL_COMP)
ELSE
// RBL_COMP.COMP_ID = New_Comp_ID()
HAdd(RBL_COMP)
END
Close()

It works as expected unless I uncomment RBL_COMP.COMP_ID = New_Comp_ID() and then I get totally unexpected values in the new record (actually the fields for the 1st record in the file.)
Strangely enough, when I fixed the New_Comp_ID function according to your instructions everything worked (although I had to open the WAlias example to get the syntax correct because the Help example has its own syntax error).
As usual, it is a case of "Beginner mistakes produce unexpected consequences' but I'm starting to wonder how long it will take to advance beyond Beginner.
Thanks Fabrice - once again you are my saviour!
Garry

--
Garry