Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

Sometimes you can get runtime errors when you are trying to assign the value of a control to some VBA object.

For example, consider this statement:

TempVars!NewTempVar = Form_orders.order_id

What we are trying to do is set the NewTempVar in the TempVars system collection to the value of the order_id field in the orders form. This seems relatively straightforward, but we can get an error in this case:

And what this is saying is that we are trying to place a reference to a form control (in this case order_id) into a TempVar.

TempVars are a system object that can only contain string variable types. It’s often used to store global data. In this case, we are trying to assign a control to an object that requires a string type.

Normally, Access will IMPLICITLY convert this to a string for you. It would be similar to using the statement:

TempVars!NewTempVar = CStr(Form_orders.order_id)

CStr is a function that does an explicit conversion of the value to a String type.

But in some cases, the implicit conversion doesn’t work. If there had been a variable already defined as a string, that would do the implicit converstion like this:

Dim testStr As String
testStr = Form_orders.order_id

Another way to implicitly convert a variable to a string is to append a string to it like this:

"" & Form_orders.order_id

This is the same with all variable types. Occasionally you can get into strange situations due to implicit conversions. Note the differences in the following situations in the immediate pane:

So you can see that in the first case, it is concatenating 2 strings, in the next 3 cases it is implicitly converting the values to integers.

Be aware of your variable types and what you are assigning. Often doing explicit conversions will help prevent errors and unexpected results.