Use TryGet pattern for transient values

A number of objects may have extra properties or something where it might not exist for all instances. A good example might be finding property on an variable: Not all controls have a property and thus can fail to run. A common approach is to use OERN: This will work OK but this can significantly…

Normalize all constraints’ & indices’ names

Constraints that are created by SSMA or by user via SSMS tend to have ugly names. Worse, auto-generated names are not stable across backups. Meaning if you restore a new database based on a copy of another database, the constraint names will change. That create huge problems during migrations because scripts that references constraint will…

“Too Few Parameters” error fix

Symptoms Whenever you run a SQL statement you get a “Too Few Parameters, expected X.” Cause Most common cause is misspelling or missing fields in the SQL statement. Resolution Check the SQL statement. If necessary, copy and paste to an Access query and test in the query to get it to highlight which part of…

Use properties instead of constant or magic numbers

A common programming mistake is to hard-code literals that corresponds to some property. That is particularly true with certain controls like tab controls & pages. The tab control exposes a property that indicates the current tab and the pages contains a property that indicates its position in the tabs of the control. Thus, it’s common…

Use controls, not fields

Sometimes we need to use fields from a RecordSource that aren’t shown on the form. It is legal to reference them directly even if they don’t have a control. For example, we can have a form bound to a RecordSource like so: And only display in a textbox but not show the . In code,…

Designing a lookup table with logic

It is very common for us to embed some kind of logic based on a lookup table or in some cases, a column of a table. Because we have application logic connected to it, the code are fragile and subject to changes as the requirements develops. We want to avoid this situation where we might…

ID vs Code

There are cases where a table’s primary key is referred to often in the code. In fact, it might have some special meanings in code. It is highly encouraged to avoid the use of or (or any auto-incrementing schema) for those keys because it is also usually desirable to create a VBA that corresponds to…

Truthy and Falsy Values

“Truthy” and “Falsy” Values

VBA is quite very loose in what it deems “truthy” and “falsy”. For example, this procedure can produce surprising result: At the first glance, it seems expected that if the bit field is set to , it would succeed. But in fact, it wouldn’t because is equivalent to , not . Unless we convert the…