MsAccess Free text in dropdown - ms-access

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

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

need to refresh subform based list box selection in access 2007

Preface: I have a form which has tabs. In one of the tabs, I have a list box, a button, and a sub-form. The list box is populated from two tables and has a bound column.
Needed: I need the sub-form to edit existing records of one of the tables the list box is built on and append records to the same table on button click. The sub form is to be linked to the non-bound column of the list box. Please help.. I have tried a few things in Vb but could not complete..
--Chegu.
I am not sure if I understand you correctly, but if you want to select a line in the listbox, edit the respective record in the underlying table and then save the record and update the listbox, you could work along those lines:
The listbox should not be linked, neither should the subform.
Create a procedure
Sub UpdateSubform()
subform![id]=listbox!changetableID]
End sub
In the OnLoad event of the form and in the OnChange event of the listbox call this procedure
In the afterUpdate event of the subform:
Private Sub Form_AfterUpdate()
me.parent.requery
End Sub
I didn't check this out myself, because I am away from my access - but it should work along those lines.
If thats not what you are looking for, please edit your post and at least post the information whats in the listbox and where your subform should link to.

How to get rid of adding new record at the end of a subform?

I use a subform to show the result of a query, but at the end of record there is a *(New) for adding new records. I don't want the user to be able to add new records through this subform. How can I get rid of this?
With the subform in Design View, open its property sheet. Then select the Data tab on the property sheet, find the property named "Allow Additions" and set it to No.
The "Allow Additions" property will not display if you open the design view of the parent form and click on the subform. The subform must be opened in Design View independent of the parent form.
Had exactly the same problem.
My DB is to keep track of basketball box scores. Every new main form created a new blank subform to enter the quarter scores into. The problem was as I Entered through the fields once I hit enter on the last quarter score value a new record was created for the table that Quarter Scores was based on.
I could not use Allow Additions = no. If I did not allow additions there was no quarter score input created when a new main form (for a new game) was created.
I used the code below for a key down event of the Enter key to set the focus on another subform before a new quarter score record was created.The commented lines were to help trouble shoot when creating the code. Key code 13 is the Enter key.
Hope this helps someone, took a while to get this right.
Den
Private Sub HOT2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> 13 Then Exit Sub
'MsgBox "Enter Pressed"
KeyCode = 0
'MsgBox "KeyCode=0"
Forms!FRM_BoxScores.Scrimmage.SetFocus
Forms!FRM_BoxScores!subform_qryReturnVisitingPlayers_BosScores.Form!subform_tblPlayerPoints_BoxScores.Form!PlayerPoints.SetFocus
End Sub
Change Record-set Type to Screenshot in Data Form Properties > Data Tab. Please note user cannot change any Data in form you once you change this
in your subform design grid, open the properties.
Where is says recordset type, set it to snapshot. That will remove that line.
The recordset will NOT be updateable at that time. So if you want to edit records, you have to change it back

MS access Not In List event and Subform

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