As a developer working with Uniface 10.4, understanding the lookup
statement is crucial for efficient database operations. This powerful statement allows you to check if records exist without actually retrieving data - making it a performance-friendly option for validation scenarios. π
Note: This article is based on the official Uniface Documentation 10.4, and I had assistance from AI in structuring this content.
π What Does lookup Do?
The lookup
statement counts the number of occurrences that match the current profile in your database. It's like asking "How many records match this criteria?" without actually loading the data into memory.
π― Key Features
- No Data Transport - Only returns a count, saving bandwidth and memory
- No Read Trigger Activation - Bypasses trigger logic for pure checking
- Primary Key Matching - Uses primary key values when available
- Universal Compatibility - Works in all component types
π Understanding Return Values
The lookup
statement returns its result in $status
:
Value | Meaning |
---|---|
β₯0 | β Number of matching records found |
-1 | π End of file encountered (no record found) |
-2 | π Table is empty |
-3 | β οΈ I/O error (hardware/software issue) |
-4 | β Table/file open failed |
-15 | π Uniface network error |
-16 | π Unknown network error |
π‘ Practical Example: Invoice Number Validation
Here's a real-world example showing how to prevent duplicate invoice numbers:
trigger loseFocus
; field : INVOICE_NUMBER
; Check if another record already has this number
; Prevent user from leaving field if duplicate exists
lookup
if ($status > 0)
message "β οΈ No! This number has been used before!"
return (-1)
else
message "β I/O error %%$status"
endif
end; loseFocus
π Best Practices & Pro Tips
π§Ή Use clear for Total Counts
Want to count all occurrences in a table? Use clear
before lookup
:
clear
lookup
; $status now contains total number of records
β‘ Performance Consideration
While lookup
is efficient, sometimes a retrieve/o
operation might be faster, especially since it also checks data already loaded in the component.
π― Key Matching Logic
- If complete primary key is available β uses primary key values
- If primary key incomplete β uses all field values for matching
π¨ Error Handling
Always check $procerror
for detailed error information:
- -2 through -12: Database I/O errors (
UIOSERR_*
) - -16 through -30: Network I/O errors (
UNETERR_*
)
π― When to Use lookup
Perfect for:
- β Validation routines
- β Checking record existence before operations
- β Counting records without loading data
- β Performance-critical duplicate checks
Consider alternatives when:
- π You need the actual data anyway
- π Component already has relevant data loaded
π Conclusion
The lookup
statement is a powerful tool in the Uniface developer's toolkit. It provides an efficient way to check record existence without the overhead of data retrieval, making it perfect for validation scenarios and existence checks. Remember to always handle the various return codes properly to create robust applications! πͺ
Have you used lookup
in interesting ways in your Uniface applications? Share your experiences in the comments below! π
Top comments (0)