MS access Not In List event and Subform - ms-access

I have created a survey form in ms access 2003 which have one main form and two subforms from 3 different tables. On the main form there is a combo box for patient ID and date field for date registered. when the patient ID is not in the list a the event fires and a pop up form appears where i can completer details of new ID and date and that new id will create a record in all 3 tables. My problem is after closing the popup form the combo box does show new data -ID but the subform which relates to the new ID does not update or refresh and does not show new ID. I need to close the main form and re-open and then i can select the new patient ID which will be shown on the subform as well. Can this be programmed so that i do not have to close the form? Help will be appreciated.

You can manually re-populate all controls from code by executing the following in VBA code of you form:
Me.Requery
As long as your form is opened you can access the form via AllForms collection. So, in your popping-up form you can just write in the OnClose-event the following code
Forms("NameOfYourMainForm").Requery

Related

Access VBA - Selecting newest option in combobox

I have a database with three tables: Employees, Consultations, and Customers. Here are the relationships:
Employees<--->Consultations
Consultations<--->Customers
I have a form for entering consultations (called "Consultations"), and the user can select the Customer from a combo box. This then displays the customer information on the form in a read-only format. If the user needs to update the customer information (name, type, department, etc.) they can click a button that opens another form to that customer's record. They can update the info, then close that form, and the "Consultations" form is updated with the new customer information. This all works fine.
I also want users to be able to enter a new customer record if they don't exist in the customers table. Currently, the user can click on a button, and a form is opened where they can enter all of the new customer information, called "Add-Customer". Once they close the form, the "Consultations" form is displayed again. Here is where I am having issues.
What I want to happen is that after the user enters the new customer, the new customer should be selected in the combo box. The combo box is holding the customer's "LastName, FirstName". I was able to get the new record to appear in the combo box, but the user still needs to manually select it. I want this to happen automatically.
Here is my code that runs when the user clicks OK on the "Add-Customer" form:
Private Sub Command1_Click()
'save customer record
DoCmd.RunCommand acCmdSaveRecord
'make add-customer form invisible
Me.Visible = False
'requery the customerlastname field on consultations form
DoCmd.Requery "CustomerLastName"
'close add-customer form
DoCmd.Close acForm, "Add-Customer"
End Sub
I tried adding a variable that stores the ID of the new record, then tried to have the combo box select that record, but couldn't get it to work. I've removed that from the above code.
Thanks!
UPDATED: Realizing you are working with a second form - which is not a subform, here is one possible solution.
You will need to pass the new value back to the original form. You can do this in several ways: (a) a global variable; (b) a hidden textbox; (c) create public function in main form. I chose to use a hidden field.
I assume you are opening the ‘Add’ form as modal. If not, then you need to do that.
(You will need to change the following code to use your combobox name and form name)
Add a hidden textbox on the main form i.e. named txtNameHidden
In the close code for the 'Add' form, add the following:
Forms!frmMyMainForm!txtNameHidden = me.txtName ' The value as it will appear in the combobox
In the Main form, after the line of code that opened the form, add the following code:
Me.cboNames.Value = Me. txtNameHidden
I am curious about your code 'DoCmd.Requery "CustomerLastName"' - does this work? How does it know to reference the control on the main form?

New record in subform based on ID of main form

First I want to say that I'm a beginner in Access, and I will appreciate every bit of help I can get.
I have created a form showing records from one of my tables with three subforms. The record and the subforms has a one-to-many relationship. I am now creating buttons for each of the subforms for creating new records in the subforms.
I've managed to make the buttons and used the Macro builder to open the forms in a dialog mode with Add as data mode.
Openform (New recordSub1; Form; ; ; Add; Dialog)
But the users has to manually add the ID of the parent in order to keep the relationship between the parent and the child record. How can I bring the ID of the parent record over to the form which opens when the users click on the "Add new record" button for the subforms?
Thanks!
With Me.MySubForm.Form.RecordsetClone
.AddNew
!data = Your data
!ID_MySubForm = Me.ID
.Update
End With

MS Access table subform refresh

I'd like to refresh the table located under the History tab whenever I enter new data under Payments. I tried adding an onclick macro when History tab is clicked but the new macro editor in Access 2010 confuses me.
As can be seen in the picture below, I've already added a fourth entry in Payments subform and the new entry does not show up in the table under History Tab.
I think Refresh or requery is the way to go but I don't know how to set it up to get it to work. Any help would be appreciated.
The best place to run the requery would be the After Update event for the payments subform.
Private Sub Form_AfterUpdate()
Me.Parent.HistorySubformConrolName.Form.Requery
End Sub
Make sure you use the name of the subform control, not the name of the form contained. See http://access.mvps.org/access/forms/frm0031.htm
Creating events: http://office.microsoft.com/en-us/access-help/about-events-and-event-procedures-HP005186744.aspx
EDIT in this particular case:
Me.Parent.Payment_Info.Form.Requery
Me.qryListExpertsHistory_subform.Requery
' qryListExpertsHistory_subform (is the embedded form)

MsAccess Free text in dropdown

I have two tables tied by a foreign key. Example:
Customer -> CustomerType
I've created a form where I have a dropdown for each customer. Dropdown contains all customer types. Now, sometimes person who enters data wants to add new customer type to that customer without leaving the form. is there a way to have a free text input in dropdown and automatically creating a record in the db if it does not exist? or is there a way of having "new..." option which will expose textbox to enter new group name?
Thanks!
Assuming your dropdown is a combo box, use its On Not In List event. Allen Browne has sample code you can adapt: Option 1: Not In List event
If your dropdown is a list box, I don't know how to do it without opening another form.
If the user types-in the new categorie in the combobox it will save this new customer type in the customer table but not in the customerType table.
What you could do is:
have a onchange event that check for the "add a new customerType" row being selected
have a form open in modal view to add the new cutomerType (form properties PopUp = true and Modal = true)
\
Private Sub province_Change()
If Me.comboBoxCustomerType.SelText = "Add a new customerType" Then
DoCmd.OpenForm "frmAddNewCustomerType"
End If
End Sub

Forms and Subforms in Access 2003

I have only one table and I would like to create a form for the users to populate the table easily. There are at least 5 fields in the table that only needs to be completed if a certain type of inspection (Fire) is selected from a drop down list and is left blank otherwise.
I would like to create a subform that will only pop up when inspection type "Fire" is selected from the drop down list in the main form. How can I do this?
I used the wizard to create the form and I am stuck because I really don't know VBA. So far, I went to the inspection type field on the form, clicked on "Properties", then clicked on "After Update", then selected the Macro that I created to open the subform when the inspection type = "Fire", but it isn't working.
What happens is the subform gets opened up no matter what type of inspection I select, then the ID number on the subform doesn't correspond to the main form (the subform ID will remain on id#1). Also, when I do input data in the subform, the information ends in the next record.
I was wondering if that is happening because I am using both the form and the subform to enter data into the same table. I hope this is a clear explanation of what I want to do.
Just to try to improve a bit on Kovags excellent proposition:
Sub InspectionType_AfterUpdate
Subform1.Visible=(InspectionType.Text="Fire")
End Sub
Sub Form_Current
InspectionType_AfterUpdate
End Sub
Just change this code to adapt your needs and add it to the Change Event of the Inspection Type textbox:
Sub InspectionType_Change
if InspectionType.Text="Fire" Then
Subform1.Visible=True
else
Subform1.Visible=False
endif
End Sub
If the fields are on the same table then I'd suggest placing the fields on the subform onto the main form. Then making them visible or not depending on the state of the combo box.
Using a subform gets a bit more complex as, among other things, you will need to save the record in the main form before opening the subform. Otherwise the subform won't be able to update the record and will give you a locking message.