I'm inside the ThisWorksheet code, have more code running in it and that works fine, and upon compilation I get an error saying it expected a function or a variable (then it puts the cursor on the name of the first program - "program1")
I don't know if it is part of a security setting, but this is not working for me:
Private Sub Workbook_Open()
Run Program1
Run Program2
Run Program3
Run Program4
End Sub
PS: even when it does not error it simply does not execute the program (without "run" --just--> "module1.Program1")
Simply use Call "Program_Name".
Related
I'm working within Microsoft Publisher.
I have a macro in module1 (that works) called "Export_as_Image".
I want to run this macro whenever I press F8.
I have created module2 with this code:
Sub SetKey()
Application.OnKey "{F8}", "Export_as_Image"
End Sub
When I run module2 I recieve the error "Compile error: Method or data member not found".
When I press F8 within Publisher, nothing happens.
Can anyone suggest what I may have done wrong, or a code to help me achieve what I'm looking for?
Thanks!
After some experimenting I found that it's needed to run SetKey for the first time firstly. You may call it from an event-driven sub (Workbook_Open, for example). Looks like running it for the first time, registers the key shortcut to the specified code.
Also, add the full path to the code you need to run: "Module2.Export_as_Image".
Update (reply to Charlie's comment below)
You can even move SetKey's code to Workbook_Open and delete SetKey (see image below).
I have a MS Access database that displays an error message about a SQL syntax error on launch. "Syntax Error in query. Incomplete query clause." It also shows another error a few seconds after I hit "OK" on the first one.
Here's the two errors: https://imgur.com/a/PesjIFk
But it doesn't tell me where the syntax error is. There are SQL statements in a bunch of different places all over this project. This is a really large project and it wouldn't be practical to just look through all the code hoping that I notice an error someplace. How can I find out where this error is?
EDIT: Ok, so apparently you have to have a keyboard that has a "Break" key on it in order to even find where the error is. Wow. Fortunately I happen to have one. Here's the code that Access takes me to if I press break when I see the error message. This code is for a subform of another form. It highlights the first line (Private Sub Form_Current()).
Private Sub Form_Current()
If NumEnums > 0 Then
CurrentEnum = val(Nz(bit_value_edit.value)) 'Update CurrentEnum to the currently selected enum
Call UpdateEnumsLabel(Me.Parent![enums label]) 'Update label
End If
End Sub
...and here's UpdateEnumsLabel():
Public Sub UpdateEnumsLabel(ByRef label As Control)
If NumEnums > 0 Then
label.Caption = "Enums: " & CurrentEnum & "/" & NumEnums
Else
label.Caption = "Enums: 0"
End If
End Sub
The definition for CurrentEnum:
Public CurrentEnum, CurrentPort, CurrentFile, CurrentGroup As Long
I'm thinking that this error is unrelated to the code in Form_Current(), but Access is highlighting that line because the error happens when the form is opened. But the form doesn't contain anything that uses a query, so I'm confused as to what query Access has a problem with.
When the error Message pops up, Use Control+Break. It will take you to the line causes the issue.
You should also open a module and form the debug option in the VBA editor select "Compile All Modules"
And since it appears to happening on open/load, you can check both the macros and the main modules to find anything that triggers on AutoExec.
Often ctrl-break will hit the line where you errored out. However, in the case of multiple “events”, and code without error handling, then often the error occurs in the routine that called the code, not the actual code that caused the error.
What I would do launch the application (hold down the shift key to prevent any start up code, or forms running).
Now that you launched the application, but without forms or code running, then check for an autoexecc macro (if there is one, check what code it attempts to run).
If there not an autoexec macro in use, then check under file->options->current database. In this view, you can look/see/determine what the name of the start-up form is.
Once you determined the start-up form (or start up module/code) called from autoexec macro, then you can simply add a blank code module, and place in your code the SAME command that is used to start your application.
So let’s assume no autoexec macro, and you determine that the start-up form is frmMain.
So now, we can launch the application (hold down shift key to prevent any start up code from running). Now, type into a new “test” standard code module the following code:
Sub MyGo
Stop
Docmd.OpenForm "frmMain"
End sub
So you type in above code, hit ctrl-s to save the above code. Now with your cursor anyplace inside of the above code, you simply hit F5 to run.
At this point you hit/stop on the “stop” command in the debugger. At this point, you then can hit f8 to step to the next line of code. If the form in question calls other code etc., you still be able to single step, and “eventually” you hit the line which causes the error.
While the application may be large, a simple look at the start up forms (and huts then the start-up code) means that you ONLY really need to trace and look at the start up code – not the whole application.
Just something weird I noticed, tested on 2 computers with similar config (Windows 10 64-bit up-to-date with Excel 2016 32-bit), one was a clean install :
Simply create a new .xlsm workbook, and place the following in the ThisWorkbook.cls class module:
Private Sub Workbook_Open()
Debug.Print Tab(10); "Hello!"
End Sub
Save. Close. Open (if opens in PROTECTED VIEW mode, Enable editing, close and re-open). Excel crashes.
Minimal, Complete, Verifiable example.
Now the big question: why?
I tried to delay the problematic Debug.Print Tab(10); "Hello!" by placing it in a Sub which I call a few sec after the Open event with Application.OnTime Now() + (1/3600/24) * 5, "SubName", still crashing. I call it manually, it works just fine.
MSDN says nothing about it: Tab Function
Edit 19/04/2021
First, I have to say that I am a bit stunned : I asked this question on Apr 19 '18 at 7:42 (2018-04-19 07:42:36Z). There was not at this time, and there still is not, any reference to this bug on Stack Overflow. I have not encountered this bug since. And today, exactly 3 yers later to the day, I stumbled on it again. Fate is quite amazing.
Anyway, it appears this bug occurs as soon as a single Tab character is attempted to be printed in the VBE Immediate window with the Debug.Print and method without the VBE window loaded (meaning not opened once in the current Excel instance). That's why it was easier encountering it during an Workbook_Open event... But if you place the following sub in a workbook, close it, and run it from the Developper tab > Macro button without opening the VBE, Excel will also crash:
Private Sub test_MRE()
Debug.Print Tab
End Sub
So one must be very careful not to send Empty variables to the console (or you will probably look for hours before finding out what's happening, speaking knowingly...), because this will also cause Excel to crash (the , being a Tab and noting being sent before it):
Private Sub test_MRE()
Dim Var1 'Variant/Empty
Debug.Print Var1, "hello"
End Sub
I managed in my project to make the error we can hear before Excel crashes to appear, and it is an Automation Error, if it helps someone to better understand what's really happening:
As a first step in creating a VBA script to resize a currently selected image to 100% x 100%, I'm trying to reproduce the example in http://msdn.microsoft.com/en-us/library/ee814736(v=office.14).aspx. The macro is very simple:
Sub Test()
MsgBox ("Hello world")
End Sub
The VBA script was simply created in "Project1" which opens by default when one presses Alt+F11. However, I keep getting the error "Sub or Function not defined" when trying to run the VBA script (Figures 1 and 2).
How can I make the VBA script 'accessible' to Outlook?
Figure 1 Running the "Test" macro in Microsoft Outlook
Figure 2 "Sub or Function not defined" error, with module tree in the background
I solved the problem by following the instructions on msdn.microsoft.com more closely. There, it is stated that one must create the new macro by selecting Developer -> Macros, typing a new macro name, and clicking "Create". Creating the macro in this way, I was able to run it (see message box below).
I had a similar situation with this issue. In this case it would have looked like this
Sub Test()
MsqBox ("Hello world")
End Sub
The problem was, that I had a lot more code there and couldn't recognize, that there was a misspelling in "MsqBox" (q instead of g) and therefore I had an error, it was really misleading, but since you can get on this error like that, maybe someone else will notice that it was cause by a misspelling like this...
This error “Sub or Function not defined”, will come every time when there is some compile error in script, so please check syntax again of your script.
I guess that is why when you used msqbox instead of msgbox it throws the error.
This probably does not answer your question, but I had the same question and it answered mine.
I changed Private Function to Public Function and it worked.
I need to add that, if the Module name and the sub name is the same you have such issue.
Consider change the Module name to mod_Test instead of "Test" which is the same as the sub.
I think you need to update your libraries so that your VBA code works, your using ms outlook
I'm trying to create an AutoExec Macro that'll run a function upon loadup.
I have the function declared and written in a macro called: checkUser
I created an AutoExec Macro, it's code is set to the "RunCode" command, and when I type the "checkUser()" function name, it auto-populates, so obviously it can see it.
However, once I run the AutoExec macro I get this error:
The expression you entered has a function name that Microsoft Access can't find
I've also tried to convert the Autoexec to VBA, and then manually enter the code, when I do I either nothing happens, or I get an error stating that it can't be repeated more than 19 times.
Am I just putting the function in the wrong spot?
As you hint at in your comments, it looks like you have a name resolution issue. If your module name is the same as the function name, you need to further qualify the function name like checkUser.CheckUser()