---
title: Generate VB Case statement block
summary: Read code-table rows and emit a VB `Case`/`Me.Add(...)` block.
status: draft
owner: SAIF DocGen
last_reviewed: 2026-07-30
library: SAIF.Framework.CodeGen
reference_type: package
operation_id: OP-CODEGEN-002
trigger: Caller invokes CreateCaseStatement
---

# Generate VB Case statement block

This operation reads `code`/`code_descr` reference rows from a caller-supplied table and turns each row into a pair of VB source lines shaped for a `Case`-based mapping method. Along the way it derives a legal VB identifier for every row, disambiguates any identifier that repeats, and preserves the caller's requested indentation before returning the finished text.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-CODEGEN-002 |
| **Trigger** | Caller invokes `CreateCaseStatement(table, enumName, rowItem, ...)` |
| **Inputs** | `table`, `enumName`, `rowItem`, optional `useDescriptionAsCode`, optional `tabIndex` |
| **Outputs** | Generated VB source text containing `Case` lines and `Me.Add(...)` calls |
| **Primary components** | `CodeTableMap.CreateCaseStatement`, `CodeTableMap.GetResult`, `CodeTableMap.GetEnumValue` |
| **Entry point** | `SAIF.Framework.CodeGen\CodeTableMap.vb` |

## Steps

1. `CreateCaseStatement` calls `GetResult(table)`, which builds a `SqlHelper.ResultSetQuery`, formats `select code, code_descr from {table}` against the caller-supplied table name, and executes it as [SQL-001](../sql/SQL-001-select-code-table.md) against [CODE_TABLE (caller-supplied)](../database-tables.md#code_table-caller-supplied).
   - The formatted SQL carries no bind parameters, filter, or ordering clause; the table name is the only value the caller controls ([SQL-001](../sql/SQL-001-select-code-table.md)).
2. The delegated query object routes that SQL through the shared portal handle established by [Establish/refresh Oracle data connection](establish-refresh-oracle-data-connection.md).
   - `ResultSetQuery` obtains a new portal request from `m_dpi.NewPortalRequest()` and marks it as a result-set request.
   - `Execute(sql)` assigns the formatted SQL text to `DataRequest.SQLStatement`.
   - `Execute()` calls `m_dpi.ProcessPortalRequest(...)` and returns the resulting `clsResultSet` to `GetResult`.

   > **Origin**: Steps 2.1-2.3 are implemented in `SAIF.Framework.Core`'s data-portal pipeline and execute on behalf of this operation; see [SAIF.Framework.Core interaction model](../../saif-framework-core/architecture.md#interaction-model).
3. `CreateCaseStatement` initializes an empty output buffer and a `Dictionary(Of String, Boolean)` that tracks which identifiers have already been emitted.
4. For each row returned by the query, `GetEnumValue` derives a candidate member identifier.
   - It starts from the row's raw `code` value.
   - When `useDescriptionAsCode = True` (the default), it instead strips every non-alphabetic character from `row("code_descr")` and uses what remains ([BR-003](../business-rules.md#br-003-enum-member-identifier-derivation-strips-non-alphabetic-characters-from-the-description)).
   - If the resulting candidate is purely numeric, it is prefixed with the literal `Code` so the emitted member name stays legal VB syntax ([BR-002](../business-rules.md#br-002-generated-enum-member-identifiers-must-be-legal-vb-identifiers)).
5. Before writing the row, the method checks whether the candidate identifier is already present in the dictionary.
   - If it is, the method appends the row's raw `code` value to the candidate so the emitted name becomes unique ([BR-001](../business-rules.md#br-001-enum-member-identifiers-must-be-unique-within-a-generated-block)).
   - It then records the final identifier in the dictionary, so subsequent rows are compared against every name already emitted, not just the immediately preceding one ([BR-001](../business-rules.md#br-001-enum-member-identifiers-must-be-unique-within-a-generated-block)).
6. The method appends two tab-indented lines to the output buffer for the row: `Case "{code}"` followed by `Me.Add(rowItem, enumName.enumValue, row("code_descr").ToString)`.
7. Once every row has been processed, the method returns the accumulated buffer as the finished `Case` block.
   - If [SQL-001](../sql/SQL-001-select-code-table.md) returns zero rows from [CODE_TABLE (caller-supplied)](../database-tables.md#code_table-caller-supplied), the loop body never executes and the method returns an empty string.
   - No local exception handling surrounds query execution or string assembly, so any failure in either stage propagates directly to the caller.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([Caller invokes CreateCaseStatement]) --> Query[Read code-table rows]
	Query --> Loop[For each row derive identifier]
	Loop --> Dup{Identifier already used?}
	Dup -->|Yes| Disambiguate[Append raw code value]
	Dup -->|No| Emit[Emit Case and Me.Add lines]
	Disambiguate --> Emit
	Emit --> More{More rows?}
	More -->|Yes| Loop
	More -->|No| EndOk([Return generated Case block])
```

## Failure and edge-case behavior

- Duplicate computed identifiers are disambiguated by appending the row's raw `code` value before the line is emitted, guaranteeing every member name in the generated block is unique ([BR-001](../business-rules.md#br-001-enum-member-identifiers-must-be-unique-within-a-generated-block)).
- Description-derived identifiers have non-alphabetic characters stripped and are then prefixed with `Code` when the stripped result is purely numeric ([BR-003](../business-rules.md#br-003-enum-member-identifier-derivation-strips-non-alphabetic-characters-from-the-description), [BR-002](../business-rules.md#br-002-generated-enum-member-identifiers-must-be-legal-vb-identifiers)).
- The emitted `Me.Add(...)` line embeds the literal expression `row("code_descr").ToString` as generated VB text; it does not substitute the actual description value at generation time.
- Query failures bubble up unhandled, because neither `GetResult` nor `CreateCaseStatement` wraps or catches exceptions raised while executing [SQL-001](../sql/SQL-001-select-code-table.md).

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)
- [SQL catalog](../sql/index.md)
- [Database tables](../database-tables.md)

## Sources
- SAIF.Framework.CodeGen\CodeTableMap.vb
- SAIF.Framework.Core\V1\DataAccessLayer\SqlHelper.vb
- SAIF.Framework.Core\V1\Interfaces\iDataPortalInfo.vb
