script problem; linking to db

I want to link id’s to a database. I’ve made a script to do this. It works very good. Now I need to enlarge the script because I need more variables.

The id I’m going to link is like this: [35673#S]
The script will link this to the ID 35673 and uses the field “S”

Now I need more fields, so I want to use a field AA, so I want to link [35673#AA] to the ID 35673 and use the field “AA”
After linking the document, I see dat the ID is linked to the field “A” and not to the field “AA”

set ErrorArray to {}
set tekstarray to {}
set myLoc8 to {}
set myLoc7 to {}
set myLoc6 to {}
set myLoc5 to {}
set myLoc to {}
set Myvar to false
set myTekstLoc8 to "[^?^?^?^?^?^?#^?^?]"
set myTekstLoc7 to "[^?^?^?^?^?#^?^?]"
set myTekstLoc6 to "[^?^?^?^?^?^?#^?]"
set myTekstLoc5 to "[^?^?^?^?^?#^?]"
set myTeksts to "§^?^?^?^?^?#^?§"
--set loopCount to 0
set Loop5 to 1
set Loop6 to 1
set Loop7 to 1
set Loop8 to 1
set textloop to 1
set AantalLinks5 to 0
set AantalLinks6 to 0
set AantalLinks7 to 0
set AantalLinks8 to 0
set aantalteksts to 0
set aantallinks to 0
-- zoek de valabele links in het actieve doc
on findlocs()
	global myTekstLoc5
	global myTekstLoc6
	global myTekstLoc7
	global myTekstLoc8
	global AantalLinks5
	global AantalLinks6
	global AantalLinks7
	global AantalLinks8
	global aantalteksts
	global tekstarray
	global myTeksts
	global Loop5
	global Loop6
	global Loop7
	global Loop8
	global textloop
	global myLoc5
	global myLoc6
	global myLoc7
	global myLoc8
	tell application "Adobe InDesign CS3"
		activate
		--display dialog "hello world"
		set find text preferences to nothing
		set change text preferences to nothing
		--Search the document for the string "Text".
		--Set the find options
		set case sensitive of find change text options to false
		set include footnotes of find change text options to false
		set include hidden layers of find change text options to true
		set include locked layers for find of find change text options to false
		set include locked stories for find of find change text options to false
		set include master pages of find change text options to false
		set whole word of find change text options to false
		try
			set find what of find text preferences to myTekstLoc5
			set myLoc5 to find text of active document
			set AantalLinks5 to count (myLoc5)
			if AantalLinks5 = 0 then
				set Loop5 to 0
			end if
		on error
			set Loop5 to 0
		end try
		try
			set find what of find text preferences to myTekstLoc6
			set myLoc6 to find text of active document
			set AantalLinks6 to count (myLoc6)
			if AantalLinks6 = 0 then
				set Loop6 to 0
			end if
		on error
			set Loop6 to 0
		end try
		try
			set find what of find text preferences to myTekstLoc7
			set myLoc7 to find text of active document
			set AantalLinks7 to count (myLoc7)
			if AantalLinks7 = 0 then
				set Loop7 to 0
			end if
		on error
			set Loop7 to 0
		end try
		try
			set find what of find text preferences to myTekstLoc8
			set myLoc8 to find text of active document
			set AantalLinks8 to count (myLoc8)
			if AantalLinks8 = 0 then
				set Loop8 to 0
			end if
		on error
			set Loop8 to 0
		end try
		try
			set find what of find text preferences to myTeksts
			set tekstarray to find text of active document
			set aantalteksts to count (tekstarray)
			if aantalteksts = 0 then
				set textloop to 0
			end if
		on error
			set textloop to 0
		end try
		
	end tell
	return tekstarray
	return myLoc5
	return myLoc6
	return myLoc7
	return myLoc8
end findlocs

I can not tell what part is your id or field or what part of this script actually does the linking with AA?
This looks like it is just part of a bigger script!
You need to post the part that defines the field, so maybe someone can tell whats going on.

Also You can cut your script down like so… And there is still room for improvement, But I do not have InDesign so can not test .
Cheers

set ErrorArray to {}
set tekstarray to {}
set myLoc8 to {}
set myLoc7 to {}
set myLoc6 to {}
set myLoc5 to {}
set myLoc to {}
set Myvar to false
set myTekstLoc8 to "[^?^?^?^?^?^?#^?^?]"
set myTekstLoc7 to "[^?^?^?^?^?#^?^?]"
set myTekstLoc6 to "[^?^?^?^?^?^?#^?]"
set myTekstLoc5 to "[^?^?^?^?^?#^?]"
set myTeksts to "§^?^?^?^?^?#^?§"
--set loopCount to 0
set Loop5 to 1
set Loop6 to 1
set Loop7 to 1
set Loop8 to 1
set textloop to 1
set AantalLinks5 to 0
set AantalLinks6 to 0
set AantalLinks7 to 0
set AantalLinks8 to 0
set aantalteksts to 0
set aantallinks to 0
-- zoek de valabele links in het actieve doc

set myTekstLocNumbers to {myTekstLoc5, myTekstLoc6, myTekstLoc7, myTekstLoc8, myTeksts}
set AantalLinksNumbers to {AantalLinks5, AantalLinks6, AantalLinks7, AantalLinks8, aantalteksts}
set myLocpNumbers to {myLoc5, myLoc6, myLoc7, myLoc8, tekstarray}
set LoopNumbers to {Loop5, Loop6, Loop7, Loop8, textloop}


global myTekstLoc5, myTekstLoc6, myTekstLoc7, myTekstLoc8, myTeksts
global AantalLinks5, AantalLinks6, AantalLinks7, AantalLinks8, aantalteksts
global myLoc5, myLoc6, myLoc7, myLoc8, tekstarray
global Loop5, Loop6, Loop7, Loop8, textloop
global myTekstLocNumbers, AantalLinksNumbers, LoopNumbers, myLocpNumbers

on findlocs()
	tell application "Adobe InDesign CS3"
	 activate
set find text preferences to nothing
		set change text preferences to nothing
		Search the document for the string "Text".
		Set the find options
		set case sensitive of find change text options to false
		set include footnotes of find change text options to false
		set include hidden layers of find change text options to true
		set include locked layers for find of find change text options to false
		set include locked stories for find of find change text options to false
		set include master pages of find change text options to false
		set whole word of find change text options to false
	
	
	repeat with i from 1 to number of items in myTekstLocNumbers
		
		set myTekstLocNumber to item i of myTekstLocNumbers
		set myLocpNumber to item i of myLocpNumbers
		set AantalLinksNumber to item i of AantalLinksNumbers
		set LoopNumber to item i of LoopNumbers
		
		
		try
			set find what of find text preferences to myTekstLocNumber 
			set myLocpNumber to find text of active document
			set AantalLinksNumber to count (myLocpNumber)
			if AantalLinksNumber = 0 then
				set LoopNumber to 0
			end if
		on error
			set LoopNumber to 0
		end try
		log {myTekstLocNumber, myLocpNumber, myLocpNumber, LoopNumber}
	end repeat
	
	end tell
	return tekstarray
	return myLoc5
	return myLoc6
	return myLoc7
	return myLoc8
end findlocs

yes indeed it is a part of a bigger script.

I hope this will do

set DataBron to "PriceData"
set Tekstbron to "TextData"
set TekstVeld to "transtext_text"
set CountryArray to {"01.België", "02.Duitsland", "03.Internationaal", "04.Nederland", "05.Frankrijk", "06.United Kindgom retail", "07.Zwitserland", "10.Tjechië", "12.Denemarken", "16.Italië", "21.Rusland", "24.United Kingdom facturatie"}
set languageId to choose from list CountryArray with prompt "Selecteer de catalogus prijzen die na het linken moeten worden geladen." default items {"01.België"} OK button name "Update document"
if languageId is false then return
set CountryId to characters 1 thru 2 of item 1 of languageId as string as number
set OriginalTextSQL to "SELECT * from vwtranslations where transtext_fk_lang_id like " & languageId & " AND (NOT (trans_keywords IS NULL)) order by Trans_id"

-- SQL statements
set SqlQuery to "SELECT prod_id, prod_code AS Prod_code, 'linking now' AS Description, prod_id*100 AS S, prod_id*100 AS A, prod_id*100 AS B, prod_id*100 AS C,prod_id*100 AS D, prod_id*100 AS E, prod_id*100 AS F, prod_id*100 AS G, prod_id*100 AS H, prod_id*100 AS I, prod_id*100 AS J, prod_id*100 AS K, prod_id*100 AS L, prod_id*100 AS M,  prod_id*100 AS N, prod_id*100 AS O, prod_id*100 AS P,prod_id*100 AS Q, prod_id*100 AS T, prod_id*100 AS U,prod_id*100 AS V, prod_id*100 AS Breedte, prod_id*100 AS Hoogte, prod_id*100 AS Lengte, prod_id*100 AS Gewicht, prod_id*100 AS Type, prod_id*100 AS W, prod_id*100 AS X, prod_id*100 AS Y, prod_id*100 AS Z,'1' as Lang_id FROM tbproducts"
-- globale vars
set ErrorArray to {}
set tekstarray to {}
set myLoc8 to {}
set myLoc7 to {}
set myLoc6 to {}
set myLoc5 to {}
set myLoc to {}
set Myvar to false
set myTekstLoc8 to "[^?^?^?^?^?^?#^?^?]"
set myTekstLoc7 to "[^?^?^?^?^?#^?^?]"
set myTekstLoc6 to "[^?^?^?^?^?^?#^?]"
set myTekstLoc5 to "[^?^?^?^?^?#^?]"
set myTeksts to "§^?^?^?^?^?#^?§"
--set loopCount to 0
set Loop5 to 1
set Loop6 to 1
set Loop7 to 1
set Loop8 to 1
set textloop to 1
set AantalLinks5 to 0
set AantalLinks6 to 0
set AantalLinks7 to 0
set AantalLinks8 to 0
set aantalteksts to 0
set aantallinks to 0
-- zoek de valabele links in het actieve doc
on findlocs()
	global myTekstLoc5
	global myTekstLoc6
	global myTekstLoc7
	global myTekstLoc8
	global AantalLinks5
	global AantalLinks6
	global AantalLinks7
	global AantalLinks8
	global aantalteksts
	global tekstarray
	global myTeksts
	global Loop5
	global Loop6
	global Loop7
	global Loop8
	global textloop
	global myLoc5
	global myLoc6
	global myLoc7
	global myLoc8
	tell application "Adobe InDesign CS3"
		activate
		--display dialog "hello world"
		set find text preferences to nothing
		set change text preferences to nothing
		--Search the document for the string "Text".
		--Set the find options
		set case sensitive of find change text options to false
		set include footnotes of find change text options to false
		set include hidden layers of find change text options to true
		set include locked layers for find of find change text options to false
		set include locked stories for find of find change text options to false
		set include master pages of find change text options to false
		set whole word of find change text options to false
		try
			set find what of find text preferences to myTekstLoc5
			set myLoc5 to find text of active document
			set AantalLinks5 to count (myLoc5)
			if AantalLinks5 = 0 then
				set Loop5 to 0
			end if
		on error
			set Loop5 to 0
		end try
		try
			set find what of find text preferences to myTekstLoc6
			set myLoc6 to find text of active document
			set AantalLinks6 to count (myLoc6)
			if AantalLinks6 = 0 then
				set Loop6 to 0
			end if
		on error
			set Loop6 to 0
		end try
		try
			set find what of find text preferences to myTekstLoc7
			set myLoc7 to find text of active document
			set AantalLinks7 to count (myLoc7)
			if AantalLinks7 = 0 then
				set Loop7 to 0
			end if
		on error
			set Loop7 to 0
		end try
		try
			set find what of find text preferences to myTekstLoc8
			set myLoc8 to find text of active document
			set AantalLinks8 to count (myLoc8)
			if AantalLinks8 = 0 then
				set Loop8 to 0
			end if
		on error
			set Loop8 to 0
		end try
		try
			set find what of find text preferences to myTeksts
			set tekstarray to find text of active document
			set aantalteksts to count (tekstarray)
			if aantalteksts = 0 then
				set textloop to 0
			end if
		on error
			set textloop to 0
		end try
		
	end tell
	return tekstarray
	return myLoc5
	return myLoc6
	return myLoc7
	return myLoc8
end findlocs
on updateSql(DataBron, locations1, locations2, TextBron, TextArray)
	global SqlQuery
	global TextQuery
	global ActiveDoc
	--display dialog ActiveDoc
	set theSqlStatement to {}
	set thelocations to {}
	set thelocations to locations1 & locations2
	
	set numberoftekst to count the items in TextArray
	if numberoftekst > 0 then
		tell application "Adobe InDesign CS3"
			tell DataSource TextBron of EasyCatalog object
				odbc connection statement TextQuery
				synchronize with data source
			end tell
			try
				tell DataSource TextBron of EasyCatalog object
					purge deleted
				end tell
			end try
		end tell
	end if
	
	set numberOfLocations to count the items in thelocations
	if numberOfLocations > 0 then
		set theQuerynums to {}
		set theQuerynums to OrigCodeArray(thelocations)
		--display dialog theQuerynums
		tell application "Adobe InDesign CS3"
			tell EasyCatalog object
				tell DataSource DataBron
					set s to 1
					set theSqlStatement to SqlQuery
					repeat until s > number of theQuerynums
						if s = 1 then
							set theSqlStatement to theSqlStatement & " WHERE"
						end if
						if s > 1 then
							set theSqlStatement to theSqlStatement & " OR"
						end if
						set theQueryCode to item s of theQuerynums
						set whereStatement to " (prod_id > " & theQueryCode - 1 ¬
							& ") and (prod_id < " & theQueryCode + 100 & ")"
						set theSqlStatement to theSqlStatement & whereStatement
						set s to s + 1
					end repeat
					odbc connection statement theSqlStatement
					synchronize with data source
				end tell
				try
					tell DataSource DataBron
						purge deleted
					end tell
				end try
			end tell
		end tell
	end if
end updateSql
-- de noodzakelijke stappen
findlocs()
updateSql(DataBron, myLoc5, myLoc6, myLoc7, myLoc8, Tekstbron, tekstarray)
GetData5()
GetData6()
GetData7()
GetData8()
SetTexts()
finalAct()
-- herconfiguratie van de prijs dataset na het linken
on finalAct()
	global DataBron
	global insertedAtEnd
	global CountryId
	global langQuery
	global OriginalSql
	global Tekstbron
	global OriginalTextSQL
	
	tell application "Adobe InDesign CS3"
		activate
		set ActiveDoc to active document
		tell DataSource DataBron of EasyCatalog object
			set insertedAtEnd to count inserted
		end tell
		if CountryId ≠ 0 then
			set langQuery to " where tbproductproperties.prop_fk_lang_id like " & CountryId
			delay 1
			tell DataSource Tekstbron of EasyCatalog object
				odbc connection statement OriginalTextSQL
				synchronize with data source
			end tell
			set AutoUpdate to (display dialog "Automatisch updaten?" buttons {"Ja", "Nee"} default button {"Ja"})
			if button returned of AutoUpdate is "Ja" then
				tell DataSource DataBron of EasyCatalog object
					odbc connection statement OriginalSql & langQuery
					synchronize with data source
					try
						purge deleted
					end try
					try
						update document doc ActiveDoc
					end try
				end tell
				display dialog "Prijzen zijn up-to-date"
			else
				tell DataSource DataBron of EasyCatalog object
					odbc connection statement OriginalSql & langQuery
					synchronize with data source
					try
						purge deleted
					end try
				end tell
				display dialog "Je kan de prijzen nu updaten"
			end if
		end if
		tell application "Finder"
			global ErrorArray
			set the clipboard to ErrorArray as string
			set errorCount to count the items in ErrorArray
		end tell
	end tell
end finalAct
--zoekt de 5 cijferige links
on GetData5()
	set Loop5 to 1
	global DataBron
	repeat while Loop5 ≠ 0
		tell application "Adobe InDesign CS3"
			activate
			global AantalLinks5
			global linkRaw
			global myLoc5
			if Loop5 ≤ AantalLinks5 then
				set linkRaw to get item Loop5 of myLoc5
				set kolom to character 8 of linkRaw
				set theCode to (characters 2 thru 6 of linkRaw as string)
				select item Loop5 of myLoc5
				if Loop5 ≥ AantalLinks5 then
					set Loop5 to 0
				else
					set Loop5 to Loop5 + 1
				end if
			else
				set Loop5 to 0
			end if
		end tell
		try
			linkEm(theCode, kolom, DataBron)
		end try
		
	end repeat
end GetData5
-- zoekt de 6 cijferige links
on GetData6()
	set Loop6 to 1
	global DataBron
	repeat while Loop6 ≠ 0
		tell application "Adobe InDesign CS3"
			activate
			global AantalLinks6
			global linkRaw
			global myLoc6
			if Loop6 ≤ AantalLinks6 then
				set linkRaw to get item Loop6 of myLoc6
				set kolom to character 9 of linkRaw
				set theCode to (characters 2 thru 7 of linkRaw as string)
				select item Loop6 of myLoc6
				if Loop6 ≥ AantalLinks6 then
					set Loop6 to 0
				else
					set Loop6 to Loop6 + 1
				end if
			else
				set Loop6 to 0
			end if
		end tell
		try
			linkEm(theCode, kolom, DataBron)
		end try
	end repeat
end GetData6

--zoekt de 7 cijferige links
on GetData7()
	set Loop7 to 1
	global DataBron
	repeat while Loop7 ≠ 0
		tell application "Adobe InDesign CS3"
			activate
			global AantalLinks7
			global linkRaw
			global myLoc7
			if Loop7 ≤ AantalLinks7 then
				set linkRaw to get item Loop7 of myLoc7
				set kolom to (characters 8 thru 9 of linkRaw as string)
				set theCode to (characters 2 thru 6 of linkRaw as string)
				select item Loop7 of myLoc7
				if Loop7 ≥ AantalLinks7 then
					set Loop7 to 0
				else
					set Loop7 to Loop7 + 1
				end if
			else
				set Loop7 to 0
			end if
		end tell
		try
			linkEm(theCode, kolom, DataBron)
		end try
		
	end repeat
end GetData7
-- zoekt de 8 cijferige links
on GetData8()
	set Loop8 to 1
	global DataBron
	repeat while Loop8 ≠ 0
		tell application "Adobe InDesign CS3"
			activate
			global AantalLinks8
			global linkRaw
			global myLoc8
			if Loop8 ≤ AantalLinks8 then
				set linkRaw to get item Loop8 of myLoc8
				set kolom to character 10 of linkRaw
				set theCode to (characters 2 thru 7 of linkRaw as string)
				select item Loop8 of myLoc8
				if Loop8 ≥ AantalLinks8 then
					set Loop8 to 0
				else
					set Loop8 to Loop8 + 1
				end if
			else
				set Loop8 to 0
			end if
		end tell
		try
			linkEm(theCode, kolom, DataBron)
		end try
	end repeat
end GetData8
-- link eventuele tekstvelden
on SetTexts()
	set textloop to 1
	global Tekstbron
	global TekstVeld
	repeat while textloop ≠ 0
		tell application "Adobe InDesign CS3"
			activate
			global aantalteksts
			global linkRaw
			global tekstarray
			if textloop ≤ aantalteksts then
				
				set linkRaw to get item textloop of tekstarray
				set kolom to TekstVeld
				set theCode to (characters 2 thru 6 of linkRaw as string)
				select item textloop of tekstarray
				if textloop ≥ aantalteksts then
					set textloop to 0
				else
					set textloop to textloop + 1
				end if
			else
				set textloop to 0
			end if
		end tell
		try
			linkEm(theCode, kolom, Tekstbron)
		end try
	end repeat
end SetTexts
-- bepaald welke arrays in de sql query dienen te staan
on OrigCodeArray(locs)
	set resultarray to {}
	set sendarray to {}
	set h to 1
	repeat until h > number of locs
		set groupnum to get characters 2 thru 7 of item h of locs
		if item 6 of groupnum = "#" then
			set groupnum to reverse of rest of reverse of groupnum
		end if
		set groupnumstart to ({(groupnum as string as number) / 100 - 0.5} as integer) * 100 as number
		
		set groupselection to groupnumstart as string
		
		set resultarray to resultarray & groupselection
		set h to h + 1
	end repeat
	set sendarray to CleanTheCodes(resultarray)
	return sendarray
	display dialog sendarray
end OrigCodeArray
-- ruimt alle dubbels uit de code array
on CleanTheCodes(myarray)
	set TheCleanArray to {}
	set i to 1
	set j to 1
	repeat until i > number of myarray
		set mycheckedvalue to get item i in myarray
		set itsClean to true
		repeat until j > number of TheCleanArray
			set theExister to get item j of TheCleanArray
			if theExister = mycheckedvalue then
				set itsClean to false
			end if
			set j to j + 1
		end repeat
		set j to 1
		if itsClean is true then
			set TheCleanArray to TheCleanArray & mycheckedvalue
		end if
		set mycheckedvalue to null
		set i to i + 1
	end repeat
	return TheCleanArray
end CleanTheCodes
-- maakt van het geselecteerde veld een koppeling
on linkEm(MyCode, MyColumn, DataSet)
	global ErrorArray
	--display dialog " the code = " & MyCode & " dataset = " & DataSet & " kolom = " & MyColumn & " linkraw = " & linkRaw
	
	tell application "Adobe InDesign CS3"
		activate selection
		--set myDocument to active document
		--set myIndex to index of first character of selection as number
		--set thepage to parent story of first character of selection
		--set myFrame to index of parent text frames of first character of selection
		--set myInsertionPoint to insertion point myIndex of text frame myFrame of thepage
		set myDocument to active document
		set myIndex to index of first character of selection as number
		set myFrame to parent text frames of first character of selection
		--set thepage to parent story of myFrame
		set myInsertionPoint to insertion point myIndex of item 1 of myFrame
		try
			tell EasyCatalog object
				tell DataSource DataSet
					tell Field MyColumn of Record MyCode
						insert tagged content story offset myInsertionPoint
					end tell
				end tell
			end tell
			delete selection
			
		on error
			set errorItem to "DS:" & DataBron & " Code:" & MyCode & " Kolom:" & MyColumn
			set ErrorArray to ErrorArray & errorItem
		end try
	end tell
end linkEm