Normal Topic Looping Until A Unique Value Is Returned? (Read 10421 times)
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
Looping Until A Unique Value Is Returned?
Dec 3rd, 2019 at 9:23pm
Print Post Print Post  
Hi folks,

Somehow, I have a GlobalValue in my database that has somehow not incremented to a unique value all the time ... so I'd like to put some code in my database to loop and increment the global ID up by 1 until it finds a unique value.

I know how to write a loop, but not a conditional 'keeping looping till I get a unique value'.  Can someone give me a hand with this?

Here's my existing (not verifying unique value) code:

Code
Select All
		vRSHandle = @XResultSetNew(@FN, "Debtors!DRNotes")

		if (vRSHandle > -1)
		{
			vNewFileID = @ToNumber(@GlobalValue("gFileID")) + 1
			GlobalValue("gFileID", vNewFileID)

			XResultSetCreateNewRecord(vRSHandle)
		 	XResultSetValue(vRSHandle,
				"DRNoteID", vNewFileID, "DRNoteRef", DBID, "DRNoteDate", vserverdate, "DRNoteTime", @servertime(), "DRNoteClt", DBCltNo,
		 		"DRNoteSig", @UserID, "DRNote", aNote)
			XResultSetClose(vRSHandle)

			DBWorked = vServerdate
			FormCommit("")
			if ((@Visibility(NotePanel) = 1) and (aRedraw = 1)) then ForceRedraw()
		}
 

  
Back to top
IP Logged
 
Acebanner
Full Member
***
Offline



Posts: 123
Location: New York City
Joined: Jan 23rd, 2008
Re: Looping Until A Unique Value Is Returned?
Reply #1 - Dec 4th, 2019 at 6:36pm
Print Post Print Post  
Maybe do this:

1. get the GV, +1 as new proposed IDnumber
2. Perform an XResultSetSearch for records with new proposed IDNumber already.
3. If the XResult returns a result (meaning a record was found) then,
4. Keep going, else (-1 returned, so no record found) use the proposed IDNumber

I don't know if looping through XResult returns is very efficient or not.
  
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Looping Until A Unique Value Is Returned?
Reply #2 - Dec 25th, 2019 at 12:37am
Print Post Print Post  
Try this. It always checks to ensure the new GV is actually unique, and provides a little progress meter via @Msg.

Code
Select All
//==================================================================================================
SUBROUTINE cuSetUniqueID(vLE_Name as String)
// Created by Carl Underwood
// Place something similar to one of the following examples in the "[FormName] :: On Form Change" event,
// or in a Main_Program() subroutine which would also be included in the On Form Change event.
// Option 1 is preferred because a name change to the LE will automatically change it in this line too.
// 1a. cuSetUniqueID(@ElementName(RecordID))
// 1b. cuSetUniqueID(@Attribute(RecordID, 23))	// ATTR_ID_NAME = 23
// 2. cuSetUniqueID("RecordID")	// The LE name must be in quotes

var vNewGV as Int
var vCount as Int
var vCounter as Int
var vPercent as Int

If @Modified and @IsBlank( @(vLE_Name) ) and @Mode() < 2	// @Mode() prevents this from triggering in Search mode
{
	vNewGV = @TN(@GlobalValue(@Layout + "_" + vLE_Name)) + 1

	vCount = @CountStringArray(@XListValues(@Fn, @Layout + "!" + vLE_Name))
	// Alternatively use: @XLookupR(@Fn, "9999999", @Layout + "!" + vLE_Name, vLE_Name)

	// Ensure that we don't use a number that is already in the database.
	While @IsUnique( @(vLE_Name), vNewGV) = 0
	{
		vNewGV = vNewGV + 1

		// Progress meter
		vCounter += 1
		vPercent = @Int((vCounter/vCount)*100)
		@Msg("Working... " + @Str(vPercent) + "% " + @Select(@Mod(vCounter, 4) + 1, " / ", "—", " \ ", " | "))
		ForceRedraw()
	}

	@Msg("")
	GlobalValue(@Layout + "_" + vLE_Name, vNewGV)
	@(vLE_Name) = vNewGV
}

END SUBROUTINE
//==================================================================================================
 

  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged