{"title":"Query.Close() Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/query/queryinstance-close-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/query/queryinstance-close-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/query/queryinstance-close-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":"Closes a query data set and returns the query instance to the initialized state.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":274,"chars":35},{"level":2,"title":"Parameters","slug":"parameters","at":309,"chars":88},{"level":2,"title":"Remarks","slug":"remarks","at":397,"chars":546},{"level":2,"title":"Example","slug":"example","at":943,"chars":1335},{"level":2,"title":"Related information","slug":"related-information","at":2278,"chars":133}],"text":"> Version: _Available or changed with runtime version 1.0._\nCloses a query data set and returns the query instance to the initialized state. The following code shows the syntax of the CLOSE method. Query is a variable of the Query data type that specifies the query object.\n## Syntax\n```al\n Query.Close()\n```\n## Parameters\nQuery\nType: query-data-type\nAn instance of the query-data-type data type.\n## Remarks\nThe Close method does not clear any filters that are set by the SetFilterS method. If you want to clear the filters, then you must call the Clear method.\nIn most cases, you do not have to call the Close method explicitly. The Close method is called implicitly when the following conditions are true:\n When the query variable goes out of scope.\n When the Open method is called on a dataset that is currently open.\n When the queryinstance-setfilter-method or queryinstance-setrange-method are called on a dataset that is currently open.\n## Example\nThe following example demonstrates how to use the Close method on a query. The example code sets filters on the query, opens the query, and then reads the dataset. For each row in the dataset, a message box is displayed that contains the values of the columns in the row.\nThis example requires that you create a query called Customer_SalesQuantity that is links table 18 Customer with table 37 Sales Lines from the CRONUS International Ltd. demonstration database. Include columns for the Name and No. fields from the Customer table and the Quantity field from Sales Lines table.\nThe following AL code opens the query, reads each row of the dataset, and then closes the query. You can add the code to a codeunit, and then run the codeunit to see the results.\n```al\nvar\n  MyQuery: Query \"Customer SalesQuantity\";\n  Text000: Label 'Customer name = %1, Quantity = %2';\nbegin\n    // Sets a filter to display only sales quantities greater than 20.\n    MyQuery.SetFilter(Quantity, '>20');\n    // Runs the query.\n    MyQuery.Open;\n    // Reads each row in the dataset and displays a message with column values.\n    // Stops reading when there are no more rows remaining in the dataset (Read is False).\n    while MyQuery.Read do\n    begin\n      Message(Text000, MyQuery.Name, MyQuery.Quantity);\n    end;\n    MyQuery.Close;\nend;\n```\n## Related information\nquery-data-type\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}