BIBD
BIBD

Reputation: 15384

Problems caused by Spaces in Field and Control Names

Obviously including spaces in table names, field names and control names is a bad idea (since it is used as a separator in practically all the imperative programming languages use commercially today*). You have to surround those controls and fields with [], etc. I'm trying to demonstrate another one of the problems with spaces to someone else.

I seem to recall that there is a situation that can arise where because a field name has a space in it (e.g., "Foo ID") and a control based on it is also called "Foo ID", that you can end accidentally referencing the underlying field instead of the control.

e.g., you update Foo ID from empty to "hello world" and then you need to check the value for null before the record is saved; something like "me.[Foo ID]" returns Null instead of "Hello World"

How can I duplicate this unexpected behaviour?

(* - Lisp, Prolog and and APL aren't imperative programming languages)

Upvotes: 3

Views: 6926

Answers (1)

mwolfe02
mwolfe02

Reputation: 24207

Since the control name can't have spaces in it when you reference it in code using the default controls property of the form (ie, Me.Foo_ID), the spaces are replaced with underscores. So in your example, Me.Foo_ID would refer to the control, but Me![Foo ID] would refer to the underlying field. (Even this statement appears incorrect on further consideration: Me![Foo ID] almost certainly refers to the control named "Foo ID".)

As David Fenton rightly points out, the control itself can be named with spaces. And it can be referenced in code with spaces when referenced as follows: Me.Controls("Foo ID") or Me![Foo ID] as those forms can properly handle spaces. But if you want to use the shorthand, you'll need to add the underscore: Me.Foo_ID.

In that case Me.Foo_ID would return "Hello World" before the record is saved (while the form is Dirty), but Me![Foo ID] would return Null.

EDIT: After some testing I have not been able to actually reproduce the odd behavior you are after (using several different combinations).

Thanks to David Fenton for setting me straight (please let me know if I'm still off somewhere).

Upvotes: 1

Related Questions