Providing you with plenty of sample MS Access VB code and other database goodies.

Beware of scope when adding items to Collection objects

When adding items to a Collection object, remember not to add something

The following code will add a particular field's value from an ADODB.RecordSet object to a Collection. However, the scope of the RecordSet object is only within this function. After it completes, each item within the Collection object will have a null value.

Public Function GetPeriods() As Collection
Dim rs As ADODB.Recordset
Dim col As Collection

Set rs = New ADODB.Recordset
Set col = New Collection

The WRONG way to clear a table

I swear, I haven't seen something this stupid in a very long time (and I work with Access everyday).

DoCmd.OpenTable "TblOracleUploadAdjs", acViewNormal, acEdit
DoCmd.RunCommand acCmdSelectAllRecords
DoCmd.RunCommand acCmdDelete
DoCmd.Close acTable, "TblOracleUploadAdjs"

For the uninitiated, what this does is open a table up, select all the records, deletes them, then closes the table. The better way to do this would be to execute the following query:

delete from TblOracleUploadAdjs;

Or to fully solve this:

Boundary Checking for ADODB.RecordSet Objects

Not that this is a very elegant solution, but I needed to get rid of a bug in production. The cause of this was the check for one of the recordset's fields. After looping a second time and MoveNext() bringing the recordset to EOF, you couldn't check its values anymore. So to fix this I added the following line:

If Not rsUinvcat.EOF Then

This checks your boundary conditions before attempting to read a value that might not currently exist. Always check your bounds!


For i = 0 To intCols
If Not rsUinvcat.EOF Then

Common Database Problems

When coming into a business area, you as a dba may encounter numerous problems with your clients' databases. This is usually due to a lack of formal, in house knowledge. These may have been created for smaller tasks, but years later are now mission critical. Below are some common problems.

  • Table normalization - Lack of 3NF leads to update, insert, and delete anomalies.
  • Fixed width text fields too large - Datatype of char(255) where a smaller width or varchar should be used.

Working From Home? Setup a good work area first.

Working from home can be great. All you need is:

  • company laptop
  • VPN connection
  • worthless winter car (Mustang)
  • 2 really bad snowdays
  • Avoid Loops as Performance Killers

    Loops are a performance killer. They shouldn't be used when they don't have to. On the plus side, the run time of a standard loop is O(n) which is linear. Nevertheless, know your language and the functions available. It's a mistake to loop through a data structure when it has a built in function call to do what you need more efficiently. I learned this the hard way as you'll see.

    I was clearing a list box control in an Access form by removing each item from the list:

    'clear the listbox
    For i = lstAcct.ListCount - 1 To 0 Step -1
    lstAcct.RemoveItem i
    Next i

    Microsoft Access VB Code Samples, Articles, FAQs, and Database Development

    Feel free to use these examples for your own personal use.

    Syndicate content