Difference between Access written code, and writing it yourself?

Singh400

Registered User.
Local time
Today, 10:21
Joined
Oct 23, 2008
Messages
137
Just wondering, is it better to have Access write the code for you, or to write it yourself?

Case in point:

Quit button.

Mine:
Code:
Private Sub cmdExit_DB_Click()
    DoCmd.Quit
End Sub

Access:
Code:
 Private Sub Command60_Click()
On Error GoTo Err_Command60_Click


    DoCmd.Quit

Exit_Command60_Click:
    Exit Sub

Err_Command60_Click:
    MsgBox Err.Description
    Resume Exit_Command60_Click
    
End Sub

Now both have the same end result, but Access’s has a lot of others stuff in it. Which is “better”?

And while I’m picking your brains, what is the proper/correct way to comment code?

Thanks for any help :)
 
Howzit

The only difference is that Access builds in error checking, so that should something go wrong a message will appear.

I can't say I am an expert on error checking, but it is a good idea to incorporate error checking in your self built code to help troubleshooting. Somewhere along the way you will have errors in your code, that without error checks, it will be more difficult to find where the problem occurred.

In regards to commenting code - I try but am not always successful in putting a description at the start of the expression detailing what I am trying to do. I try to also put an explanation regarding any variables I dimension - again not always successful.
 
Access includes any code, comments and validation it deems necessary for the particular action. However it cannot predict all your actions.

Using the above senario

DoCmd.Quit

Lets say you as the developer have ensured that the user has to click the Exit button to quit the application. But when you are developing you do not want it to quit each time as you will have to open and close Access repeatedly. You could then use something like

If IsMDE Then
DoCmd.Quit
Else
DoCmd.Close
End If

What the above is doing is checking if the current project is an mdb or an mde. If its and mde then Ok quit else simply close. Now as the developer you do not have to open and close Access everytime you hit this button.


Access does not know what you want to do so it simply adds the basic. Regarding commenting code it is always better to include pseudo code or comments in places where certain actions or evaluations are taking place. Just think about revisting the code in 12 months time and thinking "What the hell was I doing here??" by including comments it works as a reminder. Don't forget it may not be you that is reading it in 12 months time.

David
 

Users who are viewing this thread

Back
Top Bottom