# Business Logic — modUtil.LogError() [7 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `modUtil.LogError` |
| Layer | Utility |
| Module | (unknown) (Package: `ar-reconciliation-vba-access/vba-source/modUtil.bas`) |

## 1. Role

### modUtil.LogError()

The `LogError` procedure is a shared utility that records a structured error record into the `tblErrorLog` table within the Access database. It is invoked by the import pipeline (`modImport.RunImport`) whenever a row-level processing failure occurs during an Accounts Receivable reconciliation import. The method receives three pieces of contextual information — the batch identifier (`batchID`), the row number (`rowNo`) where the error was detected, and a human-readable error message (`msg`). Before persisting the message, it sanitizes any embedded single quotes by doubling them (a standard SQL injection guard in VBA), and then inserts the record with the current system timestamp via `Now()`. This approach ensures that all import failures are captured in a single auditable error log that downstream screens or reports can query for diagnostics and corrective action. The procedure implements a simple append-only write pattern — it does not read or update any existing rows, nor does it perform validation beyond the quote-escaping guard.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart LR
    START["Start: LogError(batchID, rowNo, msg)"] --> INIT["Set db = CurrentDb()"]
    INIT --> ESCAPE["Replace msg: Replace(msg, chr(39), chr(39) + chr(39))"]
    ESCAPE --> EXEC["Execute SQL: INSERT INTO tblErrorLog (BatchID, RowNo, ErrorMsg, [Timestamp])"]
    EXEC --> CLEANUP["Set db = Nothing"]
    CLEANUP --> END_NODE["Return / Exit Sub"]
```

The flow is linear — there are no conditional branches or loops in this procedure. Each step:

1. **INIT** — Acquires a `DAO.Database` handle against the current Access database (`CurrentDb()`).
2. **ESCAPE** — Escapes embedded single quotes in the error message by replacing each `'` with `''`, preventing SQL syntax errors and injection.
3. **EXEC** — Executes an `INSERT` statement into `tblErrorLog`, binding `batchID`, `rowNo`, the sanitized `msg`, and the current timestamp (`Now()`).
4. **CLEANUP** — Releases the `DAO.Database` object.
5. **END_NODE** — Exits the sub (`Exit Sub`, implicit return).

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `batchID` | `Long` | The import batch identifier — links this error record to a specific import run in `tblImportBatch`, enabling batch-level error auditing and drill-down. |
| 2 | `rowNo` | `Long` | The 1-based row number within the imported data file where the error occurred — enables precise identification of the problematic record for manual correction. |
| 3 | `msg` | `String` | A human-readable error description — explains what went wrong at the row level (e.g., data type mismatch, validation failure, missing field). Single quotes within the message are doubled to preserve SQL integrity. |

## 4. CRUD Operations / Called Services

### Database Tables (code↔DB map, from the code graph)

| Access Table | Foreign-key reference → |
|---|---|
| tblErrorLog | tblImportBatch |

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| C | `LogError` | (none — direct DAO) | `tblErrorLog` | Inserts a new error log record containing the batch ID, row number, sanitized error message, and current timestamp. |

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

No screen/batch entry points found within 8 hops. Direct callers found: 1 methods.
Terminal operations from this method: tblErrorLog [C]

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Module: `modImport` | `RunImport()` -> `LogError(batchID, rowNo, msg)` | `tblErrorLog [C]` |

**Context:** `modImport.RunImport` is the data import driver for the Accounts Receconciliation module. It orchestrates the row-by-row import process and delegates error recording to `modUtil.LogError` whenever a row fails validation or processing. The batch ID used in the log is the same one returned from `GetNextBatchNumber`, linking all errors from a single import session together.

## 6. Per-Branch Detail Blocks

> This procedure has no conditional branches or loops. The entire execution flows linearly from start to end.

**Block 1** — [LINEAR EXECUTION] (L22)

> The procedure body executes sequentially without any conditional logic, loops, or error handling blocks.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Dim db As DAO.Database` // Declare database handle variable |
| 2 | SET | `Set db = CurrentDb()` // Acquire connection to the current Access database |
| 3 | EXEC | `db.Execute "INSERT INTO tblErrorLog ..." & "VALUES (" & batchID & ", " & rowNo & ", '" & Replace(msg, "'", "''") & "', Now())"` // Insert error record — note: `Replace` doubles single quotes to prevent SQL injection / syntax errors; `Now()` injects the current system timestamp |
| 4 | SET | `Set db = Nothing` // Release the database object (implicit cleanup of implicit connection) |
| 5 | RETURN | (implicit `Exit Sub`) // Return to caller |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `tblErrorLog` | Table | Error log table — stores all row-level failures encountered during data import operations, with fields for batch ID, row number, error message, and timestamp. |
| `tblImportBatch` | Table | Import batch master table — tracks each import run with its unique batch ID. `tblErrorLog.BatchID` is a foreign key to this table. |
| `batchID` | Field | Batch identifier — a unique numeric ID assigned to each import session, used to group all errors from the same import together. |
| `rowNo` | Field | Row number — 1-based index within the imported file indicating which row triggered the error. |
| `ErrorMsg` | Field | Error message — a free-text description of the failure reason, stored as a string with single quotes escaped. |
| `[Timestamp]` | Field | System timestamp — automatically set by `Now()` to the server's current date and time at insertion. |
| `CurrentDb()` | API | DAO function — returns a `DAO.Database` object referencing the currently open Access front-end's database. |
| `Now()` | API | VBA function — returns the current system date and time as a Date/Time value. |
