{"title":"FieldRef.SetRange([Any] [, Any]) Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/fieldref/fieldref-setrange-method/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/fieldref/fieldref-setrange-method, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/fieldref/fieldref-setrange-method","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Sets a simple filter on a field, such as a single range or a single value.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":1,"title":"FieldRef.SetRange([Any] [, Any]) Method","slug":"fieldrefsetrangeany-any-method","at":0,"chars":2435},{"level":2,"title":"Syntax","slug":"syntax","at":177,"chars":74},{"level":2,"title":"Parameters","slug":"parameters","at":251,"chars":498},{"level":2,"title":"Remarks","slug":"remarks","at":749,"chars":412},{"level":2,"title":"Example","slug":"example","at":1161,"chars":1138},{"level":2,"title":"Related information","slug":"related-information","at":2299,"chars":136}],"text":"# FieldRef.SetRange([Any] [, Any]) Method\n> Version: _Available or changed with runtime version 1.0._\nSets a simple filter on a field, such as a single range or a single value.\n## Syntax\n```al\n FieldRef.SetRange([FromValue: Any] [, ToValue: Any])\n```\n## Parameters\nFieldRef\nType: fieldref-data-type\nAn instance of the fieldref-data-type data type.\n*[Optional] FromValue*\nType: ../any/any-data-type\nThe lower limit of the range. The data type of FromValue must match the data type of the field referred to by FieldRef.\n*[Optional] ToValue*\nType: ../any/any-data-type\nThe upper limit of the range. If you omit this parameter, the FromValue you specified is used. The data type of ToValue must match the data type of the field referred to by FieldRef.\n## Remarks\nThe SetRange method provides a quick way to set a simple filter on a field. If you call this method by using a field that already has a filter, that filter is removed before the new filter is set.\nIf you omit all of the optional parameters, all filters set for that field are removed. The SetRange method fails if no field is selected.\nThis method is like the ../record/record-setrange-method method.\n## Example\nThe following example opens the Customer table as a RecordRef object, creates a reference to the first \\(No.\\) field, and stores the reference in the MyFieldRef variable. The SetRange method sets a filter that selects all records from 10000 to 40000 in the No. field. The ../recordref/recordref-find-method searches and selects the first record in the filter and counts the number of records that are found. The number of records is stored in the Count variable. The process is repeated by looping through all the records in the filter until no more records are found. The number of records that are found in the range is stored in the Count variable and displayed in a message box.\n```al\nvar\n    MyFieldRef: FieldRef;\n    CustomerRecref: RecordRef;\n    Count: Integer;\n    Text000: Label '%1 records were retrieved.';\nbegin\n    CustomerRecref.Open(Database::Customer);\n    MyFieldRef := CustomerRecref.Field(1);\n    MyFieldRef.SetRange('10000' , '40000');\n    Count := 0;\n    if CustomerRecref.Find('-') then\n      repeat\n        Count := Count + 1;\n      until CustomerRecref.Next = 0;\n    Message(Text000 , Count);\nend;\n```\n## Related information\nfieldref-data-type\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}