order of events in userform - events

So,
If i have this function in a userform:
function myFunc() as String
Me.Show vbModal
stringVar = "Second"
end function
and I have the click event of the lone command button on said userform:
Private Sub cmdbutton_Click()
Unload Me
stringVar = "First"
End Sub
My question is, when I call that function ("MyFunc") from somewhere, will the click event of the button ("cmdButton") continue processing before the function is allowed to continue, or is it basically a coin toss? this stringVar variable is a private variable of the userform. it gets set in the click event of that button, but I want to use it in the function, after the click has happened. (there will be other components on the form, calculations, etc...)
so is there any risk that the function will continue processing before the click event finishes? If the function continues before the click event, then the variable wont be set properly

My question is, when I call that function ("MyFunc") from somewhere, will the click event of the button ("cmdButton") continue processing before the function is allowed to continue, or is it basically a coin toss?
Let me explain it with an image followed by a description
When you run the function, the first line is executed. Now since the form is shown as modal, the next line which is stringVar = "Second" will not run till the time the userform is unloaded.
The next line which will execute in the above scenario is Unload Me. However, it doesn't mean that any code after that will not run. Unload Me is not like Exit Do or Exit For. It just unloads the object and in no way stop further execution of the script/code.
So obviously the next line which is executed is stringVar = "First" and it reaches End Sub
This is the time the control has been returned to the function and it continues to execute the next line which is stringVar = "Second"
Hope this makes sense. If it doesn't then please feel free to ask whatever doubts that you have.
You can also see the execution of the code by stepping through it using F8.
For further reading: Debugging VBA Code

Related

Blocking code in custom message box in VB6

In my vb6 project, I created my own msgbox using a form, due to difficulties in changing
the font/language of in-built msgbox. A search with google gave the idea of own msg-
box rather than trying msgbox of VB. Now the problem is : - when the user exits the
program, 3 options are given: to close, to restart and to cancel exit. The user need not
again go through the process of giving password etc in restart option. If I give cancel =
true in the QueryUnload event, then 2nd option does not work, 3rd option works. If
cancel = true is not given, 2nd option works, but 3rd option does not. It appears that
the main form does not get unloaded if cancel = true. Unless & until the main form
unloads, the program will not work with the fresh data to be given by the user in the
initial Form. Since the code after "msgbox.show" depends on options, it is not possible
to write that code in the same sub, not even in the same form code. Is there any way
to stop the subsequent code after "msgbox.show" and continue the same after getting
option? (like in the in-built msgbox of VB.) I am not an expert in VB, so please correct
if I made some mistake; also help with advice/suggestions.
EDIT:- [Extended explanation]
The 3 forms in my project:
Initial form for password, data etc. This is input Form for user.
Main Form. This Form shows the results after process of input.
frmMsgBox. This is a custom msgbox created using a form.
Main Form code portion. Code for closing the program:
Private Sub Form_QueryUnload(Cancel as.......)
cancel = True
frmMsgBox.Label1.caption = Do you wish to 1.Exit 2.Restart
3.Cancel the exit?
frmMsgBox.Show
End sub
(The above msgbox is almost like an in-built msgbox in VB with
vbYesNoCancel buttons) The message is in regional language,
which was the main reason forced me to use my own msgbox.
After MsgBox appears, the user selects one of the above options
using 3 commandButtons placed in that Form. The code after
clicking these buttons is written in the code portion of frmMsgBox:
Command1_Click 'This is for Exit from the Program.
All Forms.unload, All forms set to nothing, end.
Command2_click 'This is for restarting the Program.
Unload Main Form, set to nothing
Load Initial Form
Initial Fom.show
frmMsgBox.Hide
Command3_Click 'This is for cancelling the exit request.
Main Form.Show
frmMsgBox.Hide
With the above code, I have no problem with options 1 & 3,
i.e; to exit from the program or to start. The frmMsgBox hides,
the initial form shows - these are OK, but the main form does
not unload nor it is removed from memory. Because of this,
whatever new data is given by the user in the initial form now
is not being processed, the main form is struck with the old results.
If cancel = true is removed from the code above, Options 1 & 2
are OK, but option 3 does not work. Then the Main Form loses
all its results (all labels, texts etc in that Form turn blank.)

end program using keypress in visual basic

I want to end program in visual basic using keypress procedure. I use this code but every time i press a key it doesn't end the program.
Private Sub Form_KeyPress(KeyAscii As Integer)
End
End Sub
I write this and it works :
Private Sub Form_DblClick()
End
End Sub
but pressing doesnt work
You've put your code in the Form KeyPress event. If the form has any controls on it, the active control gets the event. The form only sees the KeyPress event when you set KeyPreview = True. In the Form properties try setting the KeyPreview to True, then test it again.
To intercept keypresses from anywhere on the form, set the Form property KeyPreview to True.
You can set this at runtime with Me.KeyPreview = True in the Form's Initialize or Main method.
Once you are capturing all keystrokes, use System.Windows.Forms.Application.Exit() within the method to end the program. Check thisenter link description here out for more info on exiting the application.

clicking on particular link using qtp function

I want to automate login process to flipkart by creating a function which is being called in an action
Function Website() 'this is the function
Systemutil.Run("iexplore.exe"), "http://www.flipkart.com"
End Function
Website() 'this is where the function is called in the action
The browser is opening but I don't know how to click on login without doing a record and run , but only through code and the function.
Please help me.
If you want to do it with descriptive programming, this would be a simple example:
(I used the direct login-page, because it is easier than clicking on that login link)
Website()
Function Website()
Systemutil.Run("iexplore.exe"), "https://www.flipkart.com/account/login?from=header"
set objPage = Browser("title:=Flipkart.com: Login.*").Page("title:=Flipkart.com: Login.*")
objPage.Sync()
objPage.WebEdit("html id:=login_email_id1").Set "UsernameXY"
objPage.WebEdit("html id:=login_password1").Set "SecretPassword"
objPage.WebButton("html id:=login-cta").Click()
End Function
You can choose two approaches from here.
1. Object Repository: Press Ctrl + R to Open object repository. Then add objects to your repository. Once you add them, Just Drag and drop.
Descriptive Programming: Just Like answer one

Word VBA Application Event fires more times when more documents opened

I have the following problem. I wrote simple macro which shows MsgBox before print dialog. Here is source:
Public WithEvents App As Application
Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
MsgBox "aaaaa"
End Sub
Private Sub Document_New()
Set App = Application
End Sub
When I open one document from template with this macro, everything is OK. But I have a problem when I open two documents from this template at same time. When I click to print button, MsgBox shown up twice. Any ideas?
Edit:
When I create document from this template and create another new document, which isnt't based on this template (both of this documents are opened at the same time) and I print from that new empty document, MsgBox shown up. This is also wrong right?
You have created application-level events that fire every time any document is being printed. They are triggered once for every document that has this code in it, so every time you print a document you will get the msgbox once for every open document that has the code in it, whether or not the document that's printing has the code in it.
So, the behaviors aren't wrong, although clearly they are not what you want.
You should put the Before_Print event in the ThisDocument module of your template. That way the event will only happen once, and only when the document being printed has the code in it.
You could put a check in your App_DocumentBeforePrint sub to check whether the instance of the application object which is firing the event is the instance that contains the active document:
If Me <> ActiveDocument Then Exit Sub

Selenium Webdriver - How do I skip the wait for page load after a click and continue

I have an rspec test using webdriver that clicks on a button... after clicking the button, the page never fully loads (which is expected and correct behaviour). After clicking the button, I want to wait 2 seconds and then navigate to a different URL... despite the fact that the page has not loaded. I don't want to throw an error because the page hasn't loaded, I want to just ignore that, and continue on as if everything is good. The page should never load, that is the expected and correct behaviour.
How can I avoid waiting until the timeout, and secondly, how can I have that not throw an error which casuses the test to fail.
Thank you!
WebDriver has a blocking API and it will always wait for page to be loaded. What you can do instead, is to press the button via JavaScript, i.e. trigger its onclick event. I am not familiar with Ruby, but in Java it would be:
WebDriver driver = ....; // Init WebDriver
WebElement button = ....; // Find your element for clicking
String script = "if (document.createEventObject){"+
"return arguments[0].fireEvent('onclick');"+
"}else{"+
"var evt = arguments[0].ownerDocument.createEvent('MouseEvents');"+
"evt.initMouseEvent('click',true,true,"+
"element.ownerDocument.defaultView,1,0,0,0,0,false,"+
"false,false,false,1,null);"+
"return !element.dispatchEvent(evt);}" ;
((JavascriptExecutor)driver).executeScript(script, button);
After this you can wait for 2 seconds and continue
I had this similar problem, I tried this solution which #Sergii mentioned but was getting below error:
org.openqa.selenium.JavascriptException: javascript error: element is not defined (Session info: chrome=84.0.4147.89)
As a workaround had to initialize element within java script code.
String script = "var element = document.querySelector('button[type=submit]');" +
"if (document.createEventObject){"+
"return element.fireEvent('onclick');"+
"}else{"+
"var evt = element.ownerDocument.createEvent('MouseEvents');"+
"evt.initMouseEvent('click',true,true,"+
"element.ownerDocument.defaultView,1,0,0,0,0,false,"+
"false,false,false,1,null);"+
"return !element.dispatchEvent(evt);}" ;
Thank you #Sergii for your answer, I was not able to add this as a comment due to limited characters, so added it as a separate answer.
why don't you try a simple trick of using "waiting()" after waitForPageToLoad() which makes it to neglect the previous command in Selenium and never fails that step
What if you used RSpec's expect method:
expect {
Thread.new() {
sleep(2)
raise RuntimeError
}
theButton.click()
}.to raise_error
Send the 'Enter' key to the link or button in question instead of a click, webdriver won't wait for the page to load and will return instantly (this is in C#, just translate to Ruby):
element.SendKeys(Key,Return);

Resources