How to debug a MS Access Macro

SachAccess

Active member
Local time
Today, 22:18
Joined
Nov 22, 2021
Messages
405
Hi,
I am aware this is a very generic question and can not have a specific answer.
However, I would still go ahead and seek help.
If I get any MS Access Macro for issue resolving, what are the few things I should check.
Some basic parameters or settings in DB, table etc. what would you suggest.
Can anyone please help me in this.
 
I find them a nightmare. You can single step, but when you amend everything, you have to save and start again. Unlike VBA where you can correct a line and continue. Not as flexible as VBA, so I avoided them unless for some back processing.
Nowadays as I am a little better at VBA, would do it all in VBA probably, but that is unlikely to happen now as retired.
 
My apologies to everyone, I meant, VBA code, not MS Access Macro.
 
My apologies to everyone, I meant, VBA code, not MS Access Macro.
Well in that case Debug.Print is your friend.
Set breakpoints and walk through your code line by line if needed and inspect the variables fields, when thing do not work as you expect.
Be consistent in your code
I Dim everything at the top, even if it might not get used do an early exit.
Put plenty of comments in your code, as if your memory is anything like mine (unlikely :) ) you will wonder what you were trying to do 6 months ago.
I tend to try and keep thing simple, so might have 3 lines of code when it could all be done in one huge line of code with nested functions.
Put sql strings/criteria strings into a string variable and debug.print them until you get them correct, then use them in place of the actual text.
 
Hi @Gasman thanks a lot for the help. Have a nice day ahead.
If am not wrong, is it somewhat similar to how we debug or design Excel VBA code.
 
Thanks @CJ_London . Have a nice day ahead. :)
Are there any more guidelines I should follow especially related to MS Access.
 
Generally speaking, Access out of the box comes with settings set up for 95% of requirements. The one thing I do when developing is to change the view from tabbed documents to overlapping windows (File>Options>Current Database). With overlapping windows, you can have tables/forms/queries etc open side by side, resize them etc. - much better than flitting between tabs. Whether you go back to tabbed documents once the app is finished is up to you.

Recommend the other setting to change is to put Option Explicit at the top of every module which will identify errors when you compile rather than at runtime. If you subsequently get runtime errors, this usually means an issue with the data. You can have this happen by default for new modules by going to Tools>Options in the VBE and ticking 'require variable declaration'

Beyond that - 'normal' coding behaviour - avoid spaces and non alphanumeric characters in field and table names and make them meaningful (don't use ID or Date use customerID, invoiceDate. For relationship fields I prefer to use customerPK/customerFK (Primary Key, Foreign Key) so I know which end of a relationship a field relates to). Document as you go as to what the code is supposed to be doing etc

Be aware of reserved words - should not be used for table/field names.

If you have come from an Excel background and new to databases, read up on database normalisation. Be aware that Excel combines data and presentation into one view whilst databases store data in tables and use queries, forms and reports for presentation. Consequently Excel tends to store data horizontally (short and wide) whilst databases stores data vertically (tall and narrow). Trying to apply Excel methods to Access will at best result in a app that is difficult and time costly to maintain and at worst, won't work at all.
 
Hi @CJ_London thanks a lot! This certainly helps. :)

'Option Explicit at the top of every module' do this in Excel, habit will help.
'If you have come from an Excel background and new to databases', I agree, I do.
 
Last edited:
Other thing for those with an excel background - in databases there is no concept of next, previous or first/last without providing an order. Data is stored randomly in table 'buckets' and when retrieved in a query the query engine may decide to retrieve data in a way that is most efficient for the query. Won't go into the reasons why here but there are plenty of threads on the subject on this and other forums
 

Users who are viewing this thread

Back
Top Bottom