MS Access VBA always calling for Macro when manually running - ms-access

Having this strange behaviour in my Access DB since today. If I try to run code in VBA (i.e. F5) a box comes up and asked to select a macro to run. Never had this problem before as I just want it to run the code I did. Tried to find a solution but no success.
I happens for all my code... like Public Function or Private Sub...
For example I created this code. When I execute it by clicking the button it works but if I run in VBA with F5 it always asks for the macro...
Public Sub Command18_Click()
MsgBox ("Hello")
End Sub

This occurs if you try to hit f5 and run code in a form.
While you can hit f5 for a regular plain jane code in a code module, if you try this for form code, then f5 will pop that macro prompt.
Also, if by accident (or by intent) created a class module? Then again, you see that prompt.
So, for a form code module, or a class code module?
Such code can be started by hitting f5, since you need to create an "instance" of that class before it can be run.
And, a form's code module also is considered a class module - an instance (in this case the form has to be open) must be created first. So, in the case of form's code module, you can set break-point in that code to debug, but you can't hit f5 in such code modules, or inside of class modules - since an "instance" of that object has to be created first, and hitting f5 is not able nor capable of knowing which or even where such code exists to create the instance of that class code module (of which even form's code modules are also this case).
The HUGE giveaway here is that in your comments you note some click() event. Well, then that means your working with code inside of a form's code module - and f5 can't be used to start such code - it is still a class module, and an instance of that code module has to be created first.
So, if I create a class code module like this:
then
Option Compare Database
Option Explicit
Sub TestMsg()
MsgBox "hello"
End Sub
Now, if I hit f5 in above sub, the macro prompt appears!!!!
I would have to create a plain jane regular code module, and say type in this:
Sub TestFun55()
Dim MyClass As New Class1
MyClass.TestMsg
End Sub
So, I can hit f5 in above.
And the SAME goes for code inside of a form's module. You can't hit f5 in that form's code module (which is considered a class code module).

Related

Macro Design - Calling Function

I have a question about the Macro Design in a table for a "After Update" function. In one database that one of my past employees built the "SetField" value as a function call. One of the functions it calls is called GetUserNAme() it is buried in another bas_AuditLog macro, but in a DB that I am building, it doesn't work, even though I thought I had all of the information copied and correct. I have attached an image here that might show my issue. Notice the red exclamation mark.
Image of Macro Builder with error
Perhaps one of you smart people can help me look at an area that in my DB that may have the missing link.
Thanks.
Ok, so in the other (working) applcaiton, there is a going to be a public function called GetUserName(), and it is a VBA function.
So, in that working applcation, you can hit ctrl-g (get to debug window), and then type in GetUserName and then hit shift f2. Your code editor should now jump to that VBA function. You need to copy that code to your new applcation (place it in a plane jane standard code module (not a forms module, and not a class module). and it might very well also use a api call.
So, when you copy over that code. Test and make sure the VBA code works. In most cases in the access debug window, you can type in this:
? GetUserName()
And it should spit out the current windows user name. (or whatever the code supposed to do). So, get the VBA function working, and once you do, then your data macro should now also work.

VBA - How to keep code from executing while flipping between report views

I'm using a report like a template for similar projects. I have code formatting it when it opens. Problem is, the code executes when switching to Print Preview view. Some formatting the code does is not allowed in that view.
I've got around the problem by putting an invisible textbox, named "txtFormatted" in my report. My code populates it, once done formatting, with "True". The module won't execute while the textbox has that value.
But this solution seems sloppy. What do people do to have code execute when opening a report but not to run again when flipping through views?
Instead of a textbox, use a private variable (private to the report) in the top of the report's code module:
Option Compare Database
Option Explicit
Private IsFormatted As Boolean
It will be False when opening the report.
Then set this to True as you do now, and check this variable before running the formatting code.

MS-Access 2007 on WinXP, can't break out of requery looping in Form_Current sub

I set a form "frmMain" to automatically display when the database is opened by naming it in: Access Options, Current Database, Display Form: frmMain.
In a lapse of judgement of enormous proportion, I included the statement me.requery at the beginning of the class object module Form_frmMain's Private Sub Form_Current() routine.
Now, whenever the database opens, it starts requerying over and over until after a second or so it displays a message: "Run-time error '3420': Object invalid or no longer set." Selecting End or Debug both have the same effect: me.requery is highlighted in yellow and a new "Object invalid or no longer set." message is displayed.
I've tried multiple Ctrl-Breaks, and Escapes, and can't get the console to return any control to me. I can kill the process with the Task Manager, but that of course doesn't let me get into the VBA code to remove my ridiculous me.requery.
Can someone help me out here? Thank you! Dave
After you kill your program from task manager, open your db file(I assume it is .accdb) by pressing down the shift+enter keys.
After you open your file you`ll see your database screen in front of you. Just double click a module to open the VBA editor or simply press ALT+F11. Then, youll be able to find your function.

Running VBA code on an event in an Access web database

I took a already set up database from the Office.com templates (File --> New --> Office.com Templates), since this template already gave me 80% of my requirements.
I then changed the last 20% of this template where I came into the following: It seems as this template is a web database. I am not sure about the differences of a web database and a normal database in Access at all but I did find out, they do not allow me to run VBA code. At least not when I am trying to call it by an event.
Is there any way to achieve this anyways? Or are there possibly any other ways, such as converting the web database into a normal one perhaps?
Actaully, you can call VBA code from the forms event code. However, it requires a bit of a kluge.
However, you don't need to do this in your case.
Do note that you can create client objects and use client VBA code in your web application.
This means you can add/mix VBA forms into that application (these client objects of course cannot run in a web brwoser, but they can be used the client).
Suggestion #1:
to hide record navigation buttons.
Open the web form in layout mode. Move a object (dirty the design).
Whack ctrl-g for VBA command prompt. Type in this:
forms(0).NavigationButtons = False
Now, save the form. The record navigation setting will be saved with the form.
Suggestion #2:
Open the web form with VBA. Use this code:
DoCmd.OpenForm "AssetsDetails"
Forms("AssetsDetails").NavigationButtons = False
Suggestion #3:
Call VBA code from the forms load event. This is a bit of kluge, but does work:
In the forms open event, place this macro code:
OpenForm (frmRunVBA,,,Dialog)
Note that in above WEB MACRO editor the VBA form above called frmRunVBA does NOT appear in the drop down choice list - but you CAN TYPE/force in the name – this is legal.
Now create a VBA form called frmRunVBA). In the forms load event, palce this code:
Private Sub Form_Load()
Forms("AssetDetails").NavigationButtons = False
DoCmd.Close acForm, Me.Name
End Sub
So you can call VBA code, but you really don't need to if you use tip#1.

Access 2000 Class Module Terminates Immediately After Initialization

Any idea why this would be happening? I don't get any error inside the Class_Initialize sub, it just jumps straight to the Class_Terminate sub and the object I assign the class to ends up being Nothing.
I don't have Access 2000. See whether it has an error trapping option "Break in Class Module". For Access 2003, that option can be found from the VB editor's main menu: choose Tools->Options, then select the "General" tab on the Options dialog. The radio buttons for the Error Trapping choices are located on the middle right of the dialog.
It's not clear to me whether your class module is throwing an error silently. That option should (I hope) help you expose the error ... if there is one.
If that effort is not useful, think I would next set a break point on the first executable line in Class_Initialize, and then step through the code one line at a time with the F8 key. You can see which lines are executed, and at any time while you're in break mode you can inspect the values of variables by printing them to the Immediate Window ... with Debug.Print YourVariable or the shorter ? YourVariable
A slicker way to monitor your variables is to open the Watches window (from the menu, View->Watch Window), then add variable to it (Debug->Add Watch).
Finally, if those efforts don't lead you to a solution, create a stripped down version of your class module with only the bare minimum code needed to reproduce the problem. Add that code to your question, and also include enough of the calling code to show us how you're attempting to create and use the object instance.