How to know closing event of sub form in parent form - forms

I have a main form with :
a table of data (list of pay)
a button calling a subform (assistant form to create easily a bound of pay for a complete year)
After closing the subform, I have to update the main form data (table view) only if the user click Generate (to avoid data update), ie only if the user generate new payroll.
Is there a way to know if the user cancel or not action in the subform?

Ok, so you NOT really talking about a form + sub form.
There are two ways to do this:
First, you can open the 2nd child form as acDialog (not modal - very big difference).
So, your main form code can do this:
Dim strF As String
strF = "frmChildForm"
DoCmd.OpenForm strF, , , , , acDialog
' CODE WILL HALT and wait for above form to be closed
' code here continues
' can even update data on this main form from that child form.
If CurrentProject.AllForms(strF).IsLoaded Then
' user hit ok - or your save button
Me.LastChildUpdate = Forms(strF)!LastUpdate
DoCmd.Close acForm, strF
Else
' code goes here - user hit cancel button, or X to close - we assume cancel
End If
Now in that other child form, you DO NOT CLOSE when your "done/save/ok" button runs. It simply sets the form's visible property to false
eg:
Me.Visible = False
So, now our calling code will WAIT until the user is done. If the form is closed, then we assume a user "canceled" that child form.
so, the user can hit X to close, OR the user might hit your close/cancel button. Your close/cancel button thus can close the form. eg this code:
docmd.Close acForm,me.name
So, now you know if the user canceled (closed) the form. Your "ok/save" button in that form ONLY sets the forms visible=False. That makes the calling code continue, and then you check if the form is open or not - and thus your code can take appropriate action.
However, you don't even have to use the "halt" code trick above.
What you can do in that child form you launch?
You can in the on-open event, or even on-load? You pick up the name of the calling form.
So, you define this in your code:
Option Compare Database
Option Explicit
Dim frmPrevious As Form
Private Sub Form_Load()
Set frmPrevious = Screen.ActiveForm
End Sub
So, now your save button on this form?
It can call/use/update ANYTHING from the main previous form caller.
So, your save button can now;
frmPrevious.Refresh
Or
frmPrevous!LastName = me.LastName
So, you can do "anything" by replacing "me" with frmPrevous, and any operation (such as your custom save button on that 2nd form) can update, call code, refresh, or do anything it wants to operation on the first main calling form.
And you can even call subs/functions in that main form.
So, you could have a public sub in the main form, and in the 2nd form, on the close event do this:
Call frmPrevous.MySubName ' tell + call code in main form.
docmd.close acForm,me.name
So the above outlines two approaches:
First idea:
You open 2nd form as "acdialog" and the code waits. You either then close the 2nd form (considered a cancel), or you save visible=false and the calling code CONTINUES.
Second idea:
You pick up the previous form in the on-load event. Now your save button can directly update values, controls etc. in "frmPrevious", and then once done, you close the 2nd form in that "ok button" routine.
So, quite much is that in that 2nd form, you can directly update controls and do things to the first calling form with the above 2nd idea.
Or, as noted, you can HALT the calling code that opens the form, wait for user response, and then use the visible=false trick to allow the calling code to continue. Once again, since the 2nd form is still open, you are free to get/grab/take values from that STILL open form, and then when done you thus as a last stop close that 2nd form. So in effect, your 2nd form does NOT close when the user is done - but only visible = false. As noted, this trick only works if you open/call the 2nd form as "acDialog". This is a great idea for say yes/no dialog prompts etc., since then you can upon user selection have the calling code continue, and examine the values in that 2nd form before closing it.
And the last but not least method:
In that 2nd form the code behind your "ok" button can simple update the first form
forms!MyFirstForm!LastName = me.LastName
docmd.close acForm,me.name
However, above has the form(s) name hard coded, and frmPrevious trick is thus somewhat better since then you don't hard code the calling forms name, and thus many forms can call + use that same 2nd form.

Related

How can I toggle a hidden element based on user input (text field)?

I'm looking for a good way to reveal a hidden element (a div) based on specific input into a form field. The application is an order form that isn't using any database. I want to have users input a coupon code and if it matches have the div appear (which contains what is needed to do the discount). And if it doesn't match it either does nothing, loads another hidden element or reveals a message like "invalid code".
PHP would be preferred but I'm open to anything. I don't see how the textbox where users enter the code has to be submitted with a submit button as it would submit the order form. It isn't critical that the discount is 100% secret. In other words, if they think to view the source code to see if they can figure out if there's a discount then they've earned it.
I've thought about using a inline frame for simplicity but I don't think that will work.
You could use the input's oninput function to watch the value entered. On each keypress you could make a call to see if it matches a coupon code. If the coupon codes are always "x" length, you could only do the call when the value reaches that length (if you don't want to call on every single keypress).
Alternatively, you could use a button and use it's onclick attribute. It would be the same idea as above, just instead of making the call on every keypress, you would only make the call on the button's click.
Once the value matches, just set the hidden div from "display: none" to "display: block" or something like that.
oninput reference
onclick reference

In what circumstances would I need to use Protractor ExpectedConditions to wait for an element

I'm new to working with Protractor and I was wondering in what circumstances would you need to use ExpectedConditions (example below) when using Protractor. I thought that Protractor automatically determine when an AngularJS page is fully loaded.
let EC = ExpectedConditions;
let condition = EC.presenceOf(element(by.id("something")));
browser.wait(condition, 10000);
Thanks, Eric
From my experience working with Protractor, the use of ExpectedConditions depends on the behavior of the page you are automating. It's mostly used due to failing if the condition doesn't comply in the specified time.
These conditions will also return a promise that you can handle to your liking.
I'll give you a few scenarios so you can understand where to use them.
alertIsPresent(): This condition will wait till an alert appears.
e.g.: After clicking a button, there will be an alert appearance; however, there's an API call which makes the pop-up take longer and also a small animation, so we want to wait a few seconds and no more than that.
// will click on a button
element(by.id('button')).click();
// will wait for the condition
let EC = ExpectedConditions;
browser.wait(EC.alertIsPresent(), 5000);
The following code will wait for 5 seconds after clicking the button, to see if the alert is present, else it will throw an error.
invisibilityOf(): This condition will wait till the specified element is not being displayed.
e.g.: There's a loader that appears for every single action that is triggered in the page. For this we want to wait till this loader disappears so we can continue with the automation process. By business requirements, this loader shouldn't take longer than 10 seconds.
This loader locks the whole page, so other elements are not interactable while it is up.
// trigger random action on page so loader appears
element(by.id('button2')).click();
// will wait for the condition
let EC = ExpectedConditions;
browser.wait(EC.invisibilityOf(element(by.id('loader'))), 10000);
After clicking a button, we will give a 10 seconds grace for the loader to disappear, else the condition will throw an error.
elementToBeClickable(): This condition will wait till the specified element can be clicked.
e.g.: The button to the login form is disabled by default, so it can't be clicked unless we complete the username and password textfields. The button being enabled after filling the textfields has a fast animation, either way we want to give it 1 second to complete and check if we are able to click it.
// complete both textfields required for the button to be enabled
element(by.id('username')).sendKeys('User1234');
element(by.id('password')).sendKeys('Protractor');
// will wait for the condition and then will click the button
let EC = ExpectedConditions;
browser.wait(EC.elementToBeClickable(element(by.id('loginButton'))), 1000);
element(by.id('loginButton')).click();
After completing both textfields, the condition will wait for 1 second for the element to be clickable, if it is, it will procede with the next line and click it. On the other hand, if it doesn't, an error will be thrown.
presenceOf(): In this case, the condition will check if the element is present in the DOM (Document Object Model) but it won't check if the element is visible or not.
e.g.: On a page with a radio button group containing 3 flavors: chocolate, vanilla and strawberry. Depending on which you choose, you will be shown different questions. Developers mentioned that the questions are in the page at all moments, but are hidden due to which radio button is selected at the moment. In this situation, we just want to check that all questions exist in the DOM, whether or not they will be shown by a radio button being selected.
// check all questions directly, without selecting any radio buttons
let EC = ExpectedConditions;
browser.wait(EC.presenceOf(element(by.id('question-1'))), 1000);
browser.wait(EC.presenceOf(element(by.id('question-2'))), 1000);
browser.wait(EC.presenceOf(element(by.id('question-3'))), 1000);
The time is pretty irrelevant here; nonetheless, using this conditions we will be able to check that the questions, even though hidden, exist in the DOM. If one is missing, an error will cut the test immediately.
These were a few examples I've had to deal with in the past. The use of the conditions is situational and mostly they are useful when you want to use the existing conditions since they save you the time of building them yourself.
PD: More information can be found in the Protractor API.

Dynamically reload a table in a form programatically

I have a form in Dynamics AX which displays a table of two columns in a grid. I also have a button on the form. I am overriding the clicked method of the button to update the Address field of the table. For example, here's my X++ code:
void clicked()
{
AddressTable addr;
ttsBegin;
select forUpdate addr where addr.addressID == 1;
addr.Address = "new address";
addr.update();
ttsCommit;
super();
// reload table here
}
What I would like to do is to add a code to the clicked function which will reload (re-select) the updated records and show them in the form without a need of reopening the window or refreshing it using F5 (for example).
I went through forums and AX documentation and found a few methods like refresh and reread, but they are FormDataSource class methods and I failed to make it happen inside the clicked handler above.
So, what I really want to accomplish programaticallyis what F5 does behind the scenes when clicked on an open form.
Maybe just addressTable_ds.research(true); will do the job.
See also Refresh Issue on the form when the dialog closes.

access 2003 cycle current page not working if subform is in form view

I have a form with multiple sub-forms.
I would like to have the following behaviour: pressing the tab key in the last field of the main form or in the last field of a subform the focus moves to the next subform or back to the main form according to the defined Tab order.
To achieve this, all subforms as well as the main form have the Cycle property set to Current page.
All subforms are shown within the main form as Datasheets except one that is shown in Form view.
Now I get the behaviour I want only for the main form and for the subforms shown as datasheets but not for the subform shown as Form.
Is this the normal behaviour in access 2003, of is there some other property that I have to change?
Note: if I change the default view of the "misbehaving" form to Datasheet as the others, it also behaves as I want...What am I doing wrong?And... if this is the way access 2003 works... what would be the best way to obtain the desired behaviour using vba given that I cannot find an OnTab method?
Edit: While waiting to understand if this can be done setting the right properties in the forms, I acheved the desired behaviour with this code in the first and last fields of the "misbehaving" subform.
'In the last field
Private Sub Posizione_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = vbKeyTab And Shift = 0) Then
KeyCode = 0
Me.Parent!Autore_Subform.SetFocus
End If
End Sub
'in the first field
Private Sub Stanza_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = vbKeyTab And Shift = 1) Then
KeyCode = 0
Me.Parent!Pagine.SetFocus
End If
End Sub
I was just looking for an answer to the same question, and found this via Google: Ctrl-Tab will move from the last control in the subform to the next control in the main form. I found from experimenting (with Access 2007) that it doesn't matter which control has focus in the subform, Ctrl-Tab will pop you out of the subform to the next control in the main form.

VBA form to show cell value

I have an Excel VBA form to enter data into a worksheet.
Last column of my worksheet CC contains a formula which calculates some input fields. It is a very long though simple formula.
Is it possible that while I am entering the data in the form, a field in the VBA form itself keeps showing me running result of the inputs made based on the formula in the worksheet column CC?
I don’t know how to write that long formula in VBA so want to find out if there is a way around it.
If not possible, how about while the data has been entered in the form, there is a button CHECK RESULT. Clicking this button temporarily enters the form data to spreadsheet, populating CC cell and that result shows up as message box while the VBA data form is still open.
This result message box could have two buttons, CONFIRM ENTRY or CANCEL.
If cancel is clicked then the data entered in the spreadsheet is deleted. If confirm is clicked, then entered data stays.
Kind of urgent if someone can suggest a solution.
I have added image of my VBA form.
In the most basic manner to answer your sub question:
"Is it possible that while I am entering the data in the form, a field in the VBA form itself keeps showing me running result of the inputs made based on the formula in the worksheet column CC?"
Yes it's possible.
Asumming have a working formula anywhere in the sheet. Which you do according to your question.
Assuming your Sheet doesn't have a code like Application.Calculation = xlCalculationManual , to say your Sheet is in automatic calculation mode it will show you the running results.
You use key press or enter events in the Form to update the Sheet column CC
Your approach with buttons and message box seems fine. Why don't you give it a try and let us know with any issues you face.
Some hints:
UserForm1.Show vbModeless property
Allow worksheet access while user form is open
Editing spreadsheet while user form is open
How to show messagebox from cell changed event while user form is open?