How can i reflect/copy changes from one form to another in MS Access - vba

I have a Main Form named "GSAPAccounts" referred to a query with the same name and another form (not a subform) named "Failures" referred to query also named as failures.
I have a condition in my GSAPAccounts where, when a condition is met, Failures (form) will pop out. In my Failure forms I have 2 fields to be populated "General Reason" and "Comments" (which is also in my GSAPAccounts Form) and a command button to close the form, and I also have included the "ID" as the common field between the 2 forms.
My question now is: Is it possible that when I make changes in my "Failures" form to reflect it in my "GSAP Accounts" form?
I am quite new to MS Access, and what I've done so far is just the code below. When I make changes in the Failures form, it is not updating in my other form.
I already tried everything that I can find in Google but nothing is working for me.
If IsNull(Me.General_Reason) Or IsNull(Me.Comments) Then
MsgBox "Provide M15 Failure Reason"
Else
DoCmd.Save
DoCmd.Close
End If

You could refresh or requery the other form in the AfterUpdate event of form "GSAP Accounts":
Forms!Failures.Refresh
'or:
' Forms!Failures.Requery

Related

Button to check for missing values in a MainForm and Subforms in MS Access

New to Access (still), have only basic VBA skills.
I've got 3 subforms (subfrm_PackingSteps1 , subfrm_MetalDetection and subfrm_Weights - the first 2 are continuous and the other one is single form) within a main form (frm_daily_packing_record) that users go through and input data. The user should be able to input data in no particular order, and only at the end there would be a button to confirm that the user is ready to save this form.
I'd like to have this button on the main form that checks each control (in main form and subforms) for empty values. I found and adjusted a code to check the recordset of one of the continuous forms (see below), but I can't figure out:
how to include a code that checks each control instead of manually adding all of them (I've used a function before that utilises the Tag property, but can't add it to this)
how to keep the button in the main form while checking the controls/recordsets in the other subforms.
Thanks in advance.
Private Sub ConfirmBtn_Click()
Dim blnSuccess As Boolean
blnSuccess = True
Me.Recordset.MoveFirst
Do While Not Me.Recordset.EOF
If IsNull(Me.pc) Or IsNull(Me.InnerP) Then
blnSuccess = False
Exit Do
End If
Me.Recordset.MoveNext
Loop
If blnSuccess = True Then
MsgBox "You may proceed to save this record"
Else
MsgBox "You still have some empty fields to fill in!", vbCritical + vbOKOnly, "Empty Fields!"
End If
End Sub
I personally don't like this because it is too code-heavy and can easily break when the form is modified.
Instead may I suggest doing it slightly different, for each field have vba code .ondirty or .onupdate and do the validation checking right as the user is actually on that field.
This has 2 benefits, it is creating the validation when you are creating each form field and it STOPS the user right when their first mistake or bad data is entered. The last thing I want is to enter 50 fields, scroll to the bottom, submit fails then scroll back and try to find where the mistake was. If validation is done while the user it doing the actual data entry, when you get to the bottom you should have valid data and the submit should succeed without further testing.
Less code to debug and timely messages to the user if an error is caught!

MS Access one form to select table values and use to populate multiple fields on other form?

I have a table called GL_Account and IM_Productline
The table IM_Productline has various fields that need to be populated with a value from the field GL_Account.AccountKey (i.e. IM_ProductLine.InventoryAcctkey and IM_ProductLine.CostOfGoodsSoldAcctKey)
To populate the IM_ProductLine table I made a form "Product Line Maintenance" with all the fields. To populate the field IM_ProductLine.InventoryAcctkey I put a (magnifying glass) button behind the field with the following code:
Private Sub CMD_Select_GL_Account_Click()
Me.Refresh
If IsNull(Select_ProductType) Then
'do nothing
Else
Forms![Product Line Maintenance].InventoryAcctkey = Me.SelectGLAccountKey.Column(0)
Forms![Product Line Maintenance].Refresh
End If
DoCmd.Close
End Sub
So the button opens a form Called "Select GL Account" with a combo box that enable to SELECT GL_Account.AccountKey, GL_Account.Account, GL_Account.AccountDesc
FROM GL_Account; and when the OK button is clicked it writes the value from GL_Account.AccountKey to IM_ProductLine.InventoryAcctkey, closes the form "Select GL Account" and then refreshes the form "Product Line Maintenance" so the account number and description become visible for the user.
This all work fine but here's my question:
Now rather than creating a new form for every account field I need to populate (i.e. "Select Inventory GL Account" select "Cost Of Goods Sold GL Account" etc) I'd prefer to use the form "Select GL Account" to select and populate the 11 different account fields. So behind each xxxAcctkeyfield on form "Product Line Maintenance" is a (magnifying glass) button that when clicked pulls up the form "Select GL Account" and when "OK" is clicked it writes the selected AccountKey to the correct field on form "Product Line Maintenance"?
I'd greatly appreciate anyone's efforts to understand what I am trying to explain and point me in the right direction.
Ok, there is the issue that all 11 fields should not require to be "copied" since you have a relational database (you would ONLY store the row PK ID of that selection in the current report. (a so called FK (foreign key) value). That way, say you want to change the choice? Well then you could pop up that form - search + select the one record with all that information, and then upon return ONLY store the one value.
So, I would give some thoughts to the above - you want to leverage the relational database features. And as a result, you don't need to "copy" all that data. This is not much different then say creating a invoice. I can create the new invoice, but all of the address information, and the customer that this ONE invoice belongs to? Well, that is one column with a FK value that points to the customer. Once I select that one customer, then display of the customer name + address can be say a sub form or some such - but no need exists to "copy" that information. It would also means with near zero code, you could move a invoice between customers!!! - (just change the one fk column with to the new/different customer ID (PK) value.
Now, back to the question at a hand?
You can certainly pop up a form, let the user select, enter, pick and do whatever. And THEN you can have the calling code grab + pick out the values from that form.
The way you do this? It involves a not too wide known trick.
The code that calls the form can simply open that form as a dialog form. This will HALT the calling code, the user does whatever, and when done the calling code will THEN continue. Not only does the calling code continue, but it can get/grab/pull/take any values from that pop up form WIHOUT having to use global vars.
The approach is thus thus:
dim strF as string
strF = "frmPopAskInfo"
docmd.OpenForm strF,,,,,,acDialog
' above code waits for user input
if application.AllForms(strF).IsLoaded = true then
' user did not cancel, get values from form
me!AccountNo = forms(strf)!AccountNumber
etc. etc. etc.
docmd.Close acForm,strF
end if
Now the only other issue? Well, the "ok" button on the popup for DOES NOT close the form, what it does is set visible = False. This will kick the form out of dialog mode.
me.Visible = False
So, if the user hits the cancel buttton (close form) or hits the X upprer right (close form), then the form will NOT be loaded when your calling code continues. But, if they hit OK button, then you don't close the form, but ONLY set visbile = false.
This allows the calling code to continue, you are free to get/grab/take values from that form, and then once done, you close the form.
So a form close in that popup = user canceled the form.
So, a popup form, and even a modal form? They do NOT halt the VBA calling code, but a acDialog form does!
You can thus place 2 or 5 little buttons that pops up this form, the user can pick/choose/select/enter values. When they hit ok, your calling code continues, and you are free to pull values from that form. So while all 3-4 buttons might pop up that form, each individual button launch of the form can have code that follows and updates the given control the pop button was placed beside.

I can't find the right way to reference "Parent"

On my Access main form I have a list box control (named "ActionsTaken") that shows a list of actions taken that are associated with the main form record. These are kept in a separate table linked to the main table. With a button I open a subform to add items to the list. After typing in the text I press a "Done" button which closes the subform with a macro. When the subform closes the deactivate event triggers an event procedure that validates the data, writes it to the Actions Table, and (hopefully) requeries the list control on the main form. Everything works if I use an explicit reference to the control on the main form for the requery, But this subform is called from several different main forms, so I want to refer to the control on the main form using "Parent." The syntax I think should work is:
Me.Parent!ActionsTaken.Requery
When the code executes I get a debug interrupt on the above line, and when I reset the code execution I get a pop up box with "There is no field named 'Me.Parent!ActionsTaken' in the current record." The control is definitely there (remember, an explicit reference to it works).
I suspect I don't understand how "Parent" should be referenced. I've found many other syntaxes with various combinations of dot and bang, and with the "Me" left off. Some give me different error messages, but none work.
I've found a few awkward work-arounds, but I'm really curious as to what's wrong.
A subform is a form sitting on another form (this is the 'parent') and opens when that other form (the 'parent') is opened and therefore is not a subform and does not have a parent.
Pass form name to second opened form via OpenArgs:
DoCmd.OpenArgs "secondformname", , , , , , Me.Name
Then the second form can reference the first:
Forms(Me.OpenArgs).ActionsTaken.Requery
Another approach is to open second form with acDialog which will suspend first form code execution until second form closes.
DoCmd.OpenArgs "secondformname", , , , , acDialog
Me.ActionsTaken.Requery
I have it working, thanks to June and others. I bound the "Child" form to a table even though it's not needed as this lets me indirectly pass the record number (ID) via the OpenForm command. I pass the "Parent" form name to the "Child" (Add New Action subform) with the OpenArgs argument of the OpenForm command:
strFormName = Me.Name
DoCmd.OpenForm "Add New Action subform", , _
"Comment Card ID Query", "[Comment Card].ID=[ID]" _
, , , Me.Name
When I am done with the "Child" I execute the following code:
Dim strParentName As String
strParentName = Me.OpenArgs
' Requery the Actions List Box on the Parent
[Forms](strParentName)!ActionsTaken.Requery
' Close the form
DoCmd.Close
I tried June's suggested syntax: Forms(Me.OpenArgs....) but I got errors from Access in Office 365. Using a string variable worked.
Thanks for all the help!

How to requery form opened by another user

this may seem like an odd question, but I am wanting to requery a form that is opened by another user. Basically I have two different main menus, that pertain to the role of the employee. On these main forms I display a menu with the counts of clients in each status. I then have a main form that holds the clients data. After I update the status field I am requery both forms. Now this requerys the menu that is open on the current user that changed the status, but it doesn't requery the other menu that is opened by the other user unless they close the menu and reopen. Is this possible to requery another subform of another user's form.
Here is my code below:
Private Sub status_ID_AfterUpdate()
On Error GoTo Problems
DoCmd.RunCommand acCmdSaveRecord
Forms!frmNursesMenu!frmStatusCount_Nurses.Form.Requery
Forms!frmNursesMenu!frmStatusCount_Nurses.Form.Repaint
Forms!frmNursesMenu.Requery
Forms!frmNursesMenu.Repaint
Forms!frmAdminMenu!frmStatusCount.Form.Requery
Forms!frmAdminMenu!frmStatusCount.Form.Repaint
Forms!frmAdminMenu.Requery
Forms!frmAdminMenu.Repaint
Exit Sub
Problems:
Err.Clear
Resume Next
End Sub
You will need to use the form's timer event to requery the form every so often or look to see if the form needs a requery.
If your tables include a column with the time of the last edit then you could use that to see if the form needs a requery.

MS Access will occasionally remove checkboxes from my Yes/No fields and replace them with 0 and -1

My new database is built using a variety of queries and tables. These tables and queries have two checkbox fields that are set for "Test" and "Approved". The way that these come into play is that I have buttons on one form called "ApprovalForm" that open up another form called "ViewForm". The goal was that "ViewForm would change its subform's source object every time one of these buttons is clicked. ApprovalForm closes itself first, then ViewForm opens, then Subform.SourceObject is updated to the query specified by whatever button was clicked.
Private Sub View_NonSize_APL_Click()
On Error GoTo View_NonSize_APL_Click_Err
DoCmd.Close acForm, ("ApprovalForm")
DoCmd.OpenForm ("ViewForm")
Forms!ViewForm!View_Subform.SourceObject = "Query.NonSize - APL View"
Forms!ViewForm!View_Subform.Form.Requery
Forms!ViewForm.Form.Requery
Forms.ViewForm.Caption = "NonSize - APL View"
View_NonSize_APL_Click_Exit:
Exit Sub
View_NonSize_APL_Click_Err:
MsgBox Error$
Resume View_NonSize_APL_Click_Exit
End Sub
With the process finished, the final result should be that the fields are populated by the query, which is looking at a table off on the side. The user has no access to the table itself, just that specific select query. However, in rare occasions, the checkboxes will not load correctly, instead displaying as 0s and -1s. What is peculiar about this is that when you click the button on "ViewForm" that leads back to "ApprovalForm" and click the same exact button AGAIN, the checkboxes load correctly.
My guess is that it has something to do with how "ViewForm" loads in, and due to it being a business project, the computer that it is working on is less than optimal. I tried to remedy the situation by using "Requery" and "Refresh" in the code to hopefully reload the process. I have also tried the cumbersome route of having the form close after being loaded and immediately reloading again. (Open the form, load its query, close the form, then open the form again.) That actually proved to ruin the entire process.
Is there anything I can do to counter this problem?