This blog post was created with AI assistance to help developers understand Uniface programming concepts.
If you're working with Uniface 10.4, you'll encounter the setocc statement frequently. This powerful command helps you navigate through database records in your application. Let's break it down in simple terms! π
What is setocc? π€
The setocc statement stands for "set occurrence" and it makes a specific database record the currently active one. Think of it like selecting a row in a spreadsheet - you're telling Uniface "I want to work with this specific record right now."
Basic Syntax
setocc Entity, OccurrenceNumber
Key Parameters Explained π
- Entity: The name of your database table or entity (like "CUSTOMER" or "ORDER")
- OccurrenceNumber: Which record you want to select (1 = first record, 2 = second record, etc.)
Real-World Examples π‘
Example 1: Jump to the First Customer
setocc "CUSTOMER", 1
This makes the first customer record active. Perfect for starting at the beginning of your data! π₯
Example 2: Go to the Last Record
setocc "CUSTOMER", -1
Using -1 takes you to the very last record. This is super useful when you want to find the most recent entry! π
Example 3: Loop Through All Records
$occ_num$ = 1
repeat
setocc "ENTRANT", $occ_num$
; Do something with this record
$occ_num$ = $occ_num$ + 1
until ($occ_num$ > $totocc(ENTRANT))
This code visits every single record one by one - like flipping through pages in a book! π
Special Features and Tricks β¨
Work with All Entities at Once
You can use "*" as the entity name to affect all entities in your component:
setocc "*", -1
This moves all your database tables to their last record simultaneously! π―
Understanding the Return Values
The $status variable tells you what happened:
- Positive number: Success! Shows which record is now active π
- Negative number: Something went wrong (check $procerror for details) β
Common Use Cases π οΈ
1. Data Validation
Check if all records in a form are properly filled out by looping through them with setocc.
2. Finding Specific Records
Jump directly to the record you need instead of scrolling through hundreds of entries.
3. Batch Operations
Process multiple records automatically by combining setocc with loops.
Important Things to Remember β οΈ
- Occurrence numbers start at 1 (not 0 like many programming languages)
- Using 0 keeps the current record active - it doesn't change anything
- Be careful with setocc "*",-1 - it affects ALL your data tables at once!
- The getFocus trigger activates when setocc completes, which can trigger additional code
Error Handling π¨
Common errors you might encounter:
- -1102: Invalid entity name (maybe you misspelled it?)
- -1203: Trying to access a record that doesn't exist (like record #100 when you only have 50)
Pro Tips for Better Code πͺ
- Always check $status after using setocc to handle errors gracefully
- Use meaningful variable names like $current_customer$ instead of $occ_num$
- Combine with $totocc to know how many records you have
- Test with different data sizes to ensure your code works with both small and large datasets
Conclusion π
The setocc statement is a fundamental building block in Uniface development. It gives you precise control over which database record your application is working with. Whether you're building data entry forms, reports, or complex business logic, mastering setocc will make your Uniface applications more robust and user-friendly.
Remember: practice makes perfect! Try these examples in your own Uniface environment and experiment with different scenarios. Happy coding! π―
Top comments (0)