Unable to Debug Code - vba

I have a very weird issue with Access. I have a form for which I created a command button with a simple sub for the Click event:
MsgBox "Hello"
When I put a break point in, it doesn't stop at the break statement, it just executes the sub. I don't program in access everyday but I have been coding on/off with Access for over 10 years. I have never had this issue, and I have worked on this clients Access Database before.
Any advice would be appreciated.

When you check Access Options->Current Database->Use Access Special Keys option, break points will no longer be triggered. Having this option unchecked also prevents things such as F11 opening the access objects window.

Related

Access VBA: How do I make the VBA editor stop auto-filling values?

I already turned off auto-syntax check but this problem still persists. Whenever I try to modify a time, let's say from 08:30:00 AM to 08:30:05 AM, Access sometimes fills in a zero after I backspace to clear the 0. It forces me to have to quickly change the value otherwise it gives me an annoying pop up message saying syntax error....but the editor caused the error, not me!
How do I fix this? Please help me fix this.
This happens when code is running, which forces Access to constantly compile the VBA source code.
Typical example: a form is open that uses the OnTimer Event (has TimerInterval > 0).
To prevent this: Close all forms while editing code (Design view is ok).
If it's not forms, press the "Stop" button in the VBA toolbar (the blue square) to stop all running code.

Changing form name causes error message "Error accessing the system registry"

I am programming the On_Click method for the button labeled "View" in the first printscreen below. The method will load a form with data corresponding with the specific address id in the row containing the "View" button. This code worked perfectly when pointing to a target form called "Addresses". However, when I decided to rename the form "Address", I started receiving the following error message when clicking on the view button:
I did some research on the web about this error message, which lead me to try to delete any outdated references in the VBA editor. But when I clicked on Tools-->References in the VBA editor, I got the following error message:
It seems that MS Access' entries in the system registry might have been corrupted. But I am not certain of this because the documentation of this on the web is sparse and inconsistent.
Here is a link to the database on a file sharing site: http://jmp.sh/b/9Uyx6J2YzWbs8zPq2h6g
If the problem is in the database, you can recreate the problem by opening the form "Main" and clicking on the button "View" for the record shown in the printscreen above, or for other records.
Can anyone show me how to get past this error message?
my advices?
You could rename your Forms!....SourceObject to "Address" instead of "Addresses"
You could copy/paste the form, delete the original, and retry
You could install a decent debugging tool like MZ-Tools for VBA, that will help you manage your errors. Check the details here
EDIT: I used to get similar bugs years ago, when I was writing specific form event procedures. As we decided to switch to a model where forms did not need to be debbuged anymore (check this here), we stopped getting this kind of message. And I think I forgot the trick we used to solve this kind of error. If I were you:
I'd try to open another access database and import all objects ...
I'd put aside/cancel the faulty onClick procedure
And I'd install MZ Tools because otherwise VBA debugging is a nightmare ...
Since access was not liking the command button in each record of the continuous form, I choose to put the view button's logic in the on click event of a text box in each row, which I configured as enabled but locked. This produces a separate link from each record of the continuous form to a unique detail page with more of the chosen record's data.
This solution works perfectly, and is enabling me to move on with my other coding.
However, it would be nice if someone else were able to show how to get the command button solution to work.

Having to call SetFocus twice in MS Access 2010

I found that for a couple of hours this code worked:
If KeyCode = 37 Then
Me.Text12.SetFocus
End If
Then it mysteriously stopped working. I thought it might have had something to do with processing the OnEnter subroutine (I move the cursor to the end of the text field).
However editing out the OnEnter subroutine didn't help.
The only other thing I'd done was change my input/keyboard language (to be able to type quote marks and have them appear immediately, huzzah!)
I now have this code:
On Error Resume Next
If KeyCode = 37 Then
Me.Text12.SetFocus
Me.Text12.SetFocus
End If
Which works. So I know the 50+ pages of search results for SetFocus not working which relate to it being hidden, disabled, not visible, out for tea with the vicar etc do not apply here.
I've also tried setting the focus to another control first, and liberally applied DoEvents as well, neither of which helped.
Has anyone run into anything similar or is this just a really weird Heisenbug?
You must have something else going on. Set a breakpoint on the first Me.Text12.SetFocus and watch what happens when you step through the code (F8).
I suspect that you have another process being triggered that's cancelling the first Me.Text12.SetFocus.
Another possibility is your DB as corrupted. This is something Access is prone to do, especially with lots of edits to the forms and/or reports. Go to File - Info, then click on Compact & Repair Database.
It's not a bad idea to have Access do this automatically whenever you close it. Go to File - Options - Current Database (tab), then check Compact on Close.

How to update an Access VBA app with 30 forms?

I need to update an Access VBA app with around 30 forms in it.
I have to amend a screen that seems to have been set up right at the start of the app, it uses a lot of SQL tables. Is there an way of finding my way to the start of the code?
I come from a procedural coding background and I am unused to code that doesn't have a start and an end; I also know a bit of VB, some ASP, some .Net and general computing.
When something "automagically" happens upon opening an Access database, it is almost always because
A "startup form" has been specified. (In Access_2010 that's done in File > Options > Current Database > Display Form.) ...or...
The database has a Macro named AutoExec which is automatically run when the database is opened (unless you bypass it by holding the [Shift] key down while opening).
In addition to #Gord's answer, there's a few things you need to know. I'm going to give you the quick & dirty version.
First, there's 2 types of code in Access. VBA & macros. Sometimes what's called a macro, is really VBA.
In Access, a macro is a set of instructions to do something to the database. It's very limited in what it can do. These are often used by novices who don't know how to program in VBA.
VBA is the real powerhouse behind the scenes. It can do everything a macro can do, but a whole lot more.
Access uses an Event-Driven / Object-Oriented (at least close enough for this discussion) interface. Do a Google search on those meanings. But very quickly, the listbox on a form is an object. It has properties (like width), methods (add an item), and events (click on an item).
To see the code, for macros look to to your navigation window to your left. For VBA (modules), look to the same window, or just press Alt-F11. VBA can be used standalone in a module, or behind the scenes of a form or report.
Once you get the hang of it, you'll find Access to be a handy RAD tool for small projects.
Good luck.
It appears that you already have found the form that opens when the app starts (if not, check out Gord Thompson's answer).
The first things that happen when an Access Form opens (the "start of the code", as you called it) are the Load and Open events.
If there is any code in this form that is connected to these events, then it's in the Form_Load() and Form_Open() functions in the code of the form.

How to force user to deal with the Security Warning when starting Access 2007?

When a user start an Access 2007 database that have macros and vba, a security warning is shown. I want the user to deal with this warning, so if the the content is't enabled, the user should not be able to use the database.
Now I use a macro named AutoExec (opens a form that works like a menu), and that macro is run before the user deal with the security warning. But I want to check if the content is enabled and if not I will show a form that inform the user that they should enable the content.
So what I'm actually asking for is how do I do this:
If vba and macros is not enabled -> show form "information"
If vba and macros is enabled -> show form "start menu"
Ok, after a while I have the solution. Thanks for the answers who led me the right way.
This article from Microsoft is very helpful.
In the AutoExec-macro, I have two lines:
Line one: Conditions: [CurrentProject].[IsTrusted]=False and then I choose witch Form I want to open and in this case it is the "info about security warning form"
Line two: Conditions: [CurrentProject].[IsTrusted]=True and now open the "start menu form"
And that's all!
If the content is disabled, then you cannot check, since your code cannot run....
You might like to consider a start-up form ("information"). This will show without macros.
In addition, you can run some start-up code or a macro that closes the information form and opens the main form ("start menu"), if macros are disallowed, this will not run. However, I think you may get an unsightly warning.
EDIT
Set the timer interval to say, 100 and add a little code to Information form:
Private Sub Form_Timer()
DoCmd.Close acForm, "Information"
DoCmd.OpenForm "start menu"
End Sub
Just to add my solution -- I was just dealing with this issue.
By default, in database options have it set to open with form "notEnabled"
On this "not enabled" form, have some text, pictures, or what have you that lets the user know that he/she needs to 'enable content'.
In the on load event for this form, just put some VBA to open the actual form you want the user to be presented and close the "notEnabled" form.
This way, if the user opens the database without making it trusted, enabling content, they are stuck on the form that tells them how to do that. As SOON as it's trusted, the on-load event of the form will fire and redirect the user to whichever form you want, with content enabled.
If the user opens the database and already has trusted the file, they don't see the form telling them to make it trusted.
You can avoid this by setting the IsTrusted flag to TRUE in your AutoExec macro. See Transitioning Your Existing Access Applications to Access 2007 -- search for IsTrusted to get you to the heart of the explanation of how to handle it.
I don't know why people give suggestions that have not been tested yet. My solution is simple:
If: [CurrentProject].[IsTrusted]=False
RunMenuCommand: CloseDatabase
Else
If: [CurrentProject].[IsTrusted]=True
RunCode: (you run the code or macro you wanted to in the first place here)
This basically closes the database if the security warnings are coming on. If they are not, it opens just fine. The user that is the admin will need to decrease the macro security levels on the computer of whoever wants to access the database. This macro unlike others will actually run because it agrees with what Access wants.
You're Welcome!