Search code examples
ms-accessms-access-97

Access 97 VBA writing code


I found that I can write code just by typing everything or activate element, then click for example OnClick property and write code to between the sub-end sub lines.

One person I met said that one has always use mouse and select the property in which you want to write the code, as it might write something else to some files such that the code won't work.

Is this correct or are those two ways to write code equally good? I'm using Access 97.


Solution

  • The short answer is you CAN do it that way. But it doesn't mean you SHOULD.

    With a little experimentation I've found that you can type in event subs directly in the code window, and their creation will be reflected in the properties window. This might not have always been the case in older versions of MS Access.

    You can also create them by selecting the Control in the code window's top left pull down, and then selecting the event in the top right pull down.

    Why would I choose to use the Code Builder (or at least the second option I provided). Because it prevents you from making mistakes. Both in the name of the control/event and in accidentally creating duplicate Subs. Both of which could be frustrating when debugging your application - how long could you spend looking for a bug because you added an extra "S" to an element name by accident. If you follow through from the properties window, you are guaranteed to be working on the right Sub and that it will be spelled correctly.

    Note: I've also found that you can delete the entry in the properties window, but it leaves the code behind in the code window. So to make sure it is really gone, you do have to go into the code window and delete the Sub there.

    That's not to say that I never do anything directly in the Code window like that - if I've renamed a control, I'll often update the name in the code window. But then I'll go back through the properties window to verify that I've re-connected the subs properly.

    "Look, I'm running with scissors!"