Compile error vba - I want to overwrite it - vba

I read the following:
Compile error Method or data member not found
However, I am not allowed to download anything on the work computer. Hence I'm trying to overwrite the compile error by having a Userform appear with dropdown dates for people to use when they do not have on their computer the Calendar Date picker option available.
Is there any way that I can overwrite a compile error? I have tried the following code:
Private Sub UserForm_Initialize()
On Error GoTo EnterDateManually
If IsDate(ActiveCell.Value) Then
Me.MonthView1.Value = ActiveCell.Value
End If
EnterDateManually:
Enter_Date.Show
End Sub
But it still gives me the compile error. The compile error highlights MonthView1 since on this computer I removed the missing References library since I don't have it on this computer and I'm sure other user won't have it either on their computer.
I know when you get an error with number you can overwrite it (though you should try to fix it first :-) but it seems that overwriting the Compile error is not possible? Or is it...
Thanks for you help

Me.MonthView1 doesn't exist at compile-time, thus MonthView1 isn't a member of Me - hence, Method or data member not found.
On Error statements are executable: by the time they get to run, the code has long been compiled already - you can't use that statement to handle a compile error.
A compile error means your code can't compile: there's an illegal or malformed statement somewhere, Option Explicit is specified and a variable isn't declared, ...or a referenced type library is missing.
By removing a <MISSING> type library, you're telling VBA "this project doesn't really need this library anyway" - and all would be fine if there wasn't anything in the project that did need that library.
In most cases you can late-bind a type library, and blow up at run-time instead of compile-time when that library is missing - and then yes, you can handle that with On Error statements.
However you can't late-bind a MSForms control that needs to show up on a UserForm at design-time, because VBA needs to load that control at design-time, to render the form designer.
The only way you can have a MSForms control that shows up when its library can be loaded, and doesn't when the library isn't there (I've never tried this), is by loading that library at run-time, and conditionally generating the control at run-time. Now one problem with late-binding, is that you can't bind event handlers at compile-time, and VBA doesn't let you bind event handlers at run-time, so your dynamic control can't have event handlers. But I think you'd still be able to inspect its state, so if you really want to do it, it might be feasible.

Related

Microsoft Access VBA on open error

Basically I am populating my form with data from a database. For this I have an onopen event with just this code inside:
Me.Requery
Everything worked perfectly fine until yesterday, were out of the sudden an error occured. The error states:
The expression OnOpen you entered as the event property produced the following error: Instruction invalid outside of a type-block.
Aditional it says in the Discription:
The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure]
There may have been an error evaluating the function, event or macro.
When I go to show help it says:
This error occurs when an event has failed to run because the location of the logic for the event cannot be evaluated. For example, if the OnOpen property of a form is set to =[Field], this error occurs because a macro or event name is expected to run when the event occurs.
However, when I click the message away, everything loaded correctly.
I am using:
-Microsoft Access 2013
Q&A:
Does your code compile successfully?
Yes, the rest works fine. It also loads everything into the form correctly. But Every time I start it, this info filed pops up.
EDIT: I just noticed that all my on click events throw the same error now.
Could you show the whole code?
I actually have everything else commented out to troubleshoot the error. If I delete the onopen event the info box disappears.
As mentioned in the comments, compilation can fix the problem by finding errors like wrong access modifiers
When compilation goes fine, one should delete the Form_Load function and let IDE recreate it. This fixed my problem thought apparently no change in code was observed.
Many thanks
My problem was naming a label with a Farsi name. After correcting it in English, the problem was solved

Avoiding "Can't find project or library" on computer without app installed

I have created an Excel file which contains one module which refers to Powerpoint.
I want to use the other modules in this file on a server which does not have Powerpoint installed (long story...), but cannot do this without incurring the dreaded Can't find project or library error message.
Of course I can simply create another version with the offending module deleted, which obviously works fine.
However, there are several reasons why I don't want to do this, not least that I really don't want to have to maintain multiple versions if I can help it.
Is there any way around this, e.g. getting Excel to ignore this module and not try to compile it unless it is called?
I have waded through 41 posts on stackoverflow on this topic, without finding anything that helps, which makes me suspect that I am asking the impossible.
There are some suggestions that late binding might help, but it does not seem to make any difference. Here is the offending code:
Dim PowerPointApp As Object
On Error Resume Next
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
Err.Clear
If PowerPointApp Is Nothing Then
Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
End If
On Error GoTo 0
None of the other modules make any reference to Powerpoint.
Is there any way around this, e.g. getting Excel to ignore this module and not try to compile it unless it is called?
You could try conditional compilation, that's pretty much exactly what it does.
Open up the project properties dialog, and define a project-level conditional compilation constant:
Now wrap the PowerPoint-dependent module with #If directives:
Option Explicit
#If HAS_POWERPOINT = 1 Then
'... entire module body ...
#End If
Then, everywhere you have a piece of code that calls into the conditionally-compiled module, surround it with #If directives:
#If HAS_POWERPOINT = 1 Then
MyProcedureThatRunsOffPowerPoint "foo", "bar", 42
#Else
'PowerPoint-dependent module doesn't exist, what do we do instead?
#End If
Now when you distribute the macro to the machine that doesn't have PowerPoint installed, you go to project properties and set the constant to 0, then recompile the project.

excel vba user type not defined after removing class module

I have an Excel VBA Project which included a custom class module. I changed the code to remove all references to this class, then removed the class module after exporting it. The program then had a compile error
user type not defined
I re-imported the custom class and the program compiled normally. I deleted all code and comments from the custom class module, leaving it as a blank module, and the program still compiled OK. Again, after removing the custom class I get the "user type not defined" error.
No offending code line is highlighted at any of the above steps.
Any ideas?
The problem was experienced using Excel 2016.
The problem was solved by opening the file in Excel 2007, which I fortunately retained before installing Office 365. In Excel 2007 I was able to remove the now empty class module and re-compile and save. On re-opening in Excel 2016, the project again compiled just fine.
I repeated the process on another backup copy with both Excel 2007 and 2016 and the problem consistently occurs with Excel 2016 but not with Excel 2007.
Thanks for other's comments.
I had a similar problem with the same message "user type not defined" running Excel 2007.
The problem arose when I was updating the code in a class module and removed an old private user-defined type and all references to it. This then raised the compiler error "user type not defined".
My temporary solution was (for this description let the type name be "TName"):
Remove "TName" and all references to it from the class module.
Create a new empty class module and name it "TName".
Recompiled and save the project.
Remove the newly created class module "TName".
Recompiled and save the project.
Exit all instances of Excel.
Restart excel with the project.
Done.
Note!
This removes the compiler error "user type not defined" for the moment. However, if any other error appears and it has not even have to be related to the "user type not defined" issue. The error "user type not defined" reappears. So, this is a description to remove the error for the moment. But if further development is done, it is most likely that the error will reappear and if it is not documented thoroughly it will be even harder to fix, since then you probably have forgotten the type name that originally caused the error!
This is a rough description of what I have done. However, I thought I would contribute my work here and let the next person come up with a better solution and description.

Inconsistent VBA Error Message Box?

It seems that the message box that VBA pops-up when an unhandled exception occurs behaves differently depending on... something? To see what I mean, create a new .xlsm, then create a standard module Module1; paste inside this code then paste inside Sheet1 Worksheet object this code:
Public Sub TestErrMsgBox()
Debug.Print "Hello!"
Call Err.Raise(Number:=vbObjectError, Source:="VBAProject.Sheet1", Description:="Lorem Ipsum")
End Sub
On my Excel 2010 Professional Plus, calling the subroutine in the VBE's "Immediate" (Ctrl+G) window:
Call Module1.TestErrMsgBox
Call Sheet1.TestErrMsgBox
will show the error message Automation Error / Invalid OLEVERB structure.
Now, if one calls directly the Raise method from in the "Immediate" window:
Call Err.Raise(Number:=vbObjectError, Source:="VBAProject.Sheet1", Description:="Lorem Ipsum")
it will show the (expected) error message Lorem Ipsum.
What exactly changes in the error handling, or in the Err object, from the first case to the last? and how it may be manipulated? I figured out that in the first case the message depends only on the Number argument in Raise call, but still it doesn't explain...
I find this a bit annoying, because I expected my messages displayed, not something else. There's always available the Pokemon approach (catching all the exceptions and showing custom MessageBoxes based on Err properties), but I'd like to understand what happens. :-)
Later edit:
The behavior described above happens when "Error Trapping" is set on "Break on Unhandled Errors." Once I put "Break on All Errors" it displays the correct message, with the "Debug" button available; however, this is not a good idea, to break execution on every error...
Also, thanks to the feedback from Alex K., roryap and Doug Glancy I figured out that this happens when the TestErrMsgBox subroutine is called from within a Worksheet object, not from a standard Module as I incorrectly reported the first time. The posting is fixed; however, the question is still there. :-)
The reason it happens when your Sub is in a Sheet module and you've set the VBE to Break on Unhandled Errors, has to do with the fact that a Sheet module is a class module. Set Error Trapping to Break in Class Modules, and it will behave correctly.
I always use this setting anyways, so I can debug inside UserForms and classes.
Searching "VBA err.raise invalid oleverb structure break in class module" seems to support the fact that it's related to class modules.
Declare sub as friend in class module instead of public

MS Access RunCode Macro cannot find my procedure

I'm only posting this since I wasn't able to find a solution anywhere. I finally figured it out. Kind of silly really.
When using the RunCode property within an Access Macro, I was trying to run a Sub from my global module. I was getting the error "The expression you entered has a function name that database can't find." I couldn't figure out what the issue was. I followed the advice of everyone that posted on this issue, which was mostly the following:
Use () at the end of the procedure name
DO NOT use the "=" before the procedure name
Still didn't work!
THEN I read the error message carefully. It mentions that it could not find the FUNCTION name. Apparently, the RunCode property specifically requires a "Function" not a Sub. So, I simply changed my Sub to Function and it worked fine!
Hope this helps.
Another solution that worked for me:
The module name can NOT have the same name as the procedure(s) in the module(s).
I had a similar issue with the error message. My VBA code had the following declaration:
private function MyFunction()
....
end function
I removed private declaration to get the Macro Runcode to execute the MyFunction()
For example:
Function MyFunction()
End Function
My mistake was putting the function in a Class Module instead of a regular module.
Access 2013: A function called with MyFunction() from RunCode where MyFunction does not exist gives me error 2425. None of the above work for me, however, and I was still getting Error Number 2001 when the function exists and is public. Database is in a Trusted Location. No compile errors, but something in MyFunction did not work, namely
DoCmd.ShowAllRecords
after GoToControl worked to select my subform. Real problem was my code to remove a filter with VBA. Manual for ShowAllRecords seems to indicate that this should work, but replacing DoCmd.ShowAllRecords with
DoCmd.RunCommand acCmdRemoveFilterSort
fixed my problem.
The database seems to need to have objects in it other than the VBA function being called as well. Without other objects (specifically a table in my case), the function is unable to be found from within the calling environment (eg Excel VBA).
Access Office 365: My subroutine works when called from within VBA and used to work in Macros. Moved function text to a separate module, saved it by itself. Gave the module (not class) a unique name.
I was borrowing a "template" vbasic text from online as a shell. And while I renamed the function in the code section, MSAccess/Vbasic did not show the name change in the function heading of the Module box so when I ran the macro which called this function it said it couldn't find it. Tried repeatedly to fix. Then I noticed the name was different in the code section of vbasic versus the heading of the function dialog box. So I changed the name manually in the heading box and it prompted me to save which I did and now it works. Maybe this is why snake software is so popular. :)
My VSTO C# calls to app.Run("MyMacro") stopped working suddenly and the error message started printing. ("Access cannot find the procedure MyMacro.")
The problem on my machine was that my code (which connected to a running instance of Access) was connecting to a zombie database instance that did not have that VBA macro in it. Go figure. The zombie instance did not show in the Taskbar, in the Alt-TAB display, or in a list of windows that I printed out using a console program.
The way I discovered it was to have my VSTO code print out the name of the database that it was connecting to. The Access database was one that I had been working with earlier in the day, without errors. I had closed the Access app holding it successfully hours before I tried running MyMacro.
I can't think of anything that I did that was unusual, other than upgrading VStudio to the latest version. Probably a reboot will fix things. What a weird problem.