Two events in one Sub - vba

Private Sub btnBestellen_Enter()
Call bestelling()
End Sub
Private Sub btnBestellen_Click()
Call bestelling()
End Sub
I have two seperate subs here with the same code in it. Is there any possible way to make it into one sub? For example like this, of course this doesn't work.
Thank you in advance
Private Sub btnBestellen_Click,btnBestellen_Enter()
Call bestelling()
End Sub

Unfortunately not. VBA has no syntax for grouping Event Subs as you described.
The only work around would be to create a listener function that checks for your events manually. It would be down to you to catch them and handle them correctly. Although I'm guessing that would be out of scope here :)

Related

How to end one procedure (Sub, Function) from another in VB.net?

I want to end a main sub from another sub.
Here is an example code to illustrate what I need to do:
Sub main()
endMainSub()
'do other stuff
End Sub
Sub endMainSub()
**'here I need a code to end main Sub**
End Sub
From endMainSub, I would like to terminate main sub before "do other stuff".
I need something like "End" in VBA.
End exists in VB.NET too, so you could use it. You should NEVER do so though. End simply stops executing the application at that point, with no regard for what state it might be in. It's like closing applications by using End Task in Task Manager, which I hope you use only as a last resort.
As suggested, you should be returning a result from the second method that indicates that the application should exit and the first method should explicitly choose to exit or not based on that, e.g.
Sub Main()
If EndMainSub() Then
Return
End If
'...
End Sub
Function EndMainSub() As Boolean
Dim result As Boolean
'Some work that sets result here.
Return result
End Function
I think you should use
Application.Exit
It's more object orientated and allows to execute all closing and disposing events. "End" still exists, but it will end immediately and no clean up routines will be executed.

VBA Excel Call and Run breaking backtracking

I'm making a workbook with a whole lot of different subs, and in an effort to avoid a user accidentally activating a sub that erases the code of a sheet for example, I've tried making all the subs private instead.
My subs can now only be activated by clicking buttons on the worksheet, and it all works as intended. Untill a sub of mine tries to call a private sub in another module of course.
To get around this I used Application.Run rather than Call, which worked and also allows me to call in a variable "NextSub" from the previous sub, which gives me some flexibility that I need, and apparently cant get with the Call.
Eg.
Sub FirstSub()
*Something going on
Application.Run "SecondSub", SomeVariableSub
End sub
Sub SecondSub(Nextsub as String)
If something Then
*Do something
Application.Run NextSub
Else
Application.Run NextSub
I thought that the Application.Run had solved all my problems, but I used to have a line that called an errorhandler, which in turn called a sub. It seems that the program can no longer backtrack to the sub that contained the errorhandler as it could when I used Call.
Does Applciation.Run break this functionality? If yes, can I then use Call with a variable NextSub as I am doing it now? And if I can't use the Call that way, then is all this fixed by adding a On Error GoTo ErrorHandler in the affected subs?
I know that the whole thing about calling Private Subs across modules is probably pretty bad practice, but I was compltely new to this when I started out, and the project is too extensive to fix that without rewriting all of the code.
Instead of making all subs private, either put Option Private Module on top of each module, or add a dummy argument to each routine:
Sub SomeHiddenRoutine(bDummy As Boolean = False)
'Routine can be called as usual using:
SomeHiddenRoutine
End Sub
If I understand you are trying to call a function specified by a string.
The correct way is to use something like this, which allows you to call all the private subs (as long as it is in the same module as the private functions):
Sub CallFunction(FuncName As String)
Select Case FuncName
Case "Func1": Func1
Case "Func2": Func2
Case "Func3": Func3
End Select
End Sub

How to make a function/sub available to all forms in a project?

I have a sub that I want to be available to (or "callable from") all forms:
' Call some Sub from another form.
someSub()
I'm having difficulty finding examples online thus my suspicion that the crux of my issue may be terminological.
How?
Can anyone guide me as to what I should be looking at?:
Global Sub
Static Sub
Shared Sub
Private Sub
Where?
Where should I position my "universal" sub?:
Arbitrarily place it in one of the forms.
Create a code file and somehow integrate into it each form?
declare it as public Sub inside a Module
Public Module GlobalFunctions
Public Sub YourSub()
End Sub
End Module
hope this will help you
Make it public.
Public Sub MySub()
Then you can call it from every form.
Myform.MySub()

When I call an pre-existing event handler from a subroutine, it doesn't come back. VB

I'm looking to call a pre-existing event handler subroutine from the form_Load event handler.
But the below doesn't work because control doesn't come back and I want to do more.
UPDATE:
I'm new to this so I don't know what the proper protocol is but...
The reason for the non-return was that a statement like the below ended the subroutines execution.
If aLabel.Tag = 1...
the fix was adding New to the declaration to create an instance of it, ie..
changing....
Dim aLabel As Label
... to ...
Dim aLabel As New Label
I'm surprised I didn't get a warning but instead they just abruptly stopped execution of the sub. That wasn't very helpful :)
Thanks again for your time guys...
(Maybe this question should be deleted now that it has served its purpose)
#konrad #karl
END OF UPDATE
What doesn't work is....
Private Sub Form1_Load...
button1_Click(sender, e) 'But Control doesn't come back.
end sub
Do I change the sender to something?
Thanks in advance
Dave
Invoking event handlers like this is a bad idea, because you are trying to simulate the event context by making sender and/or EventArgs be something else.
Instead, put the logic that you want to invoke into a Subroutine or Function and have your Form1_Load method call that; likewise if you really do have a real click event handler, then that handler code can call the method too, like this:
Private Sub Form1_Load()
DoSomeWork()
End Sub
Protected Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
DoSomeWork()
End Sub
Private Sub DoSomeWork()
' Put logic here that you want to do from form load and a button click
End Sub
This has the benefit of making the code cleaner, clearer and easier to maintain as you only need to change the logic in one place should you need to change the logic.
Note: Obviously, you can pass parameters to the DoSomeWork method, if need be, and change it to a Function if you need it to return something.

Public Labels(Pointers)

I am working with VB 2010 for an application which requires me to go back to a sub. After some digging, I found out GoSub is no longer supported. I tried using Goto but apparently you can't use it outside the sub the label is in. I tried calling the sub put the parameters were not known to me.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
I do not want to use the RunWorkerAsync because the worker will already be working. Please advise me on this matter.
It sounds like you are trying to solve a problem using procedural logic with an object-oriented tool.
If I understand you correctly, the sub is calling another section of code, and needs to return to the sub after it's finished. Make the called code itself a function or sub, and call it from the primary sub routine.