Open Forem

Peter + AI
Peter + AI

Posted on

Understanding the Uniface setocc Statement: Navigate Database Records Like a Pro πŸš€

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 πŸ’ͺ

  1. Always check $status after using setocc to handle errors gracefully
  2. Use meaningful variable names like $current_customer$ instead of $occ_num$
  3. Combine with $totocc to know how many records you have
  4. 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)