Hi All,
I've got a form for my users to enter customer call reports. The form has a combo box to allow the user to select an existing customer (from a customers table), or to enter a new customer (and presuambly have that customer and customer ID entered into the customer table).
If the user can't find the customer in the como box list (i.e., they will need to add a new customer), how do I have another form open for entering a new customer to the table (I have created a form for this purpose, but can't figure out how to have the combo box open it, if the customer does not already exist int the table.)
Thanks,
JTSC
4 8715 @JTSC
The typical approach to adding an Item to the RowSource of a Combo Box is to use the NotInList() Event in conjunction with either DAO or ADO. No Form interface is usually needed, and the Limit To List Property of the Combo Box must be set to ' Yes'. I'll post the code and should you have any questions, please feel free to ask. - Private Sub cboTest_NotInList(NewData As String, Response As Integer)
-
Dim strMsg As String
-
Dim MyDB As DAO.Database
-
Dim rstCustomers As DAO.Recordset
-
-
strMsg = "'" & NewData & "' is not in the list. "
-
strMsg = strMsg & "Would you like to add a New Customer?"
-
-
If vbNo = MsgBox(strMsg, vbYesNo + vbQuestion, "New Customer") Then
-
Response = acDataErrDisplay 'Access displays its standard Error Message
-
Else
-
'Decided to Add the New Customer
-
Set MyDB = CurrentDb
-
Set rstCustomers = MyDB.OpenRecordset("tblCustomers", dbOpenDynaset)
-
rstCustomers.AddNew
-
rstCustomers("Name") = NewData
-
rstCustomers.Update
-
-
Response = acDataErrAdded 'Item added to underlying Recordset and the Combo
-
'Box is Requeried, Item added to List
-
rstCustomers.Close
-
Set rstCustomers = Nothing
-
End If
-
End Sub
Hi ADezii,
Thanks. I put the code in as suggested. Now when i put an entry in the combo box that does not match the list, I do get the prompt "Would you like to add a new customer? If I answer yes, I get the "standard" message "The text you entered isn't an item in the list. Select an item from the list or enter the text that matches one of the listed items".
Here's the code that I entered: - Private Sub Customer_ID_NotInList(NewData As String, Response As Integer)
-
Dim strMsg As String
-
Dim MyDB As DAO.Database
-
Dim rstCustomers As DAO.Recordset
-
-
strMsg = "'" & NewData & "' is not in the list. "
-
strMsg = strMsg & "Would you like to add a New Customer?"
-
-
If vbNo = MsgBox(strMsg, vbYesNo + vbQuestion, "New Customer") Then
-
Response = acDataErrDisplay 'Access displays its standard Error Message Else
-
'Decided to Add the New Customer
-
Set MyDB = CurrentDb
-
Set rstCustomers = MyDB.OpenRecordset("Customers", dbOpenDynaset)
-
rstCustomers.AddNew
-
rstCustomers("CustomerName") = NewData
-
rstCustomers.Update
-
-
Response = acDataErrAdded 'Item added to underlying Recordset and the Combo
-
'Box is Requeried, Item added to List
-
rstCustomers.Close
-
Set rstCustomers = Nothing
-
End If
-
-
End Sub
I'm not sure what to do next....
Thanks
@JTSC
Looks like you forgot an 'Else' between Lines 10 and 11.
NeoPa 32,556
Expert Mod 16PB
While the concept (with provided code) that ADezii suggests can work well with small, uncomplicated tables where the whole record is included in a ComboBox, it is often necessary to provide a more complete facility from which to add new records. This would be done, as you originally suggested, using a separate form.
ADezii's concept is not so different from this. Essentially, when an item triggers the NotInList event, instead of adding the new record with code from data already entered into the ComboBox, you would open a data entry form from within your code passing any data already entered into the ComboBox. A .Requery would be necessary after the form closes (not after the call to open it).
** Aside **
I'm glad I started this as I have literally just implemented something like this and I put the .Requery after the call to open the form. Seeing this I realised I had to kick myself and fix the bug. Luckily I missed with the kick.
Anyway, moving on. I will include some of the code I used to ensure the Requery was done. - Option Compare Database
-
Option Explicit
-
-
'strFrom will contain the name of the form, and optionally, the name of the
-
'control (if required) separated by a comma (,).
-
Private strFrom As String
-
-
Private Sub Form_Open(Cancel As Integer)
-
strFrom = Nz(OpenArgs, "")
-
End Sub
-
-
...
-
-
Private Sub Form_Close()
-
'Counterintuitively, setting .Dirty to False saves the changes.
-
Me.Dirty = False
-
'Requery the calling form or control.
-
If strFrom = "" Then Exit Sub
-
On Error Resume Next
-
With Forms(Split(strFrom, ",")(0))
-
If UBound(Split(strFrom, ",")) = 0 Then
-
Call .Requery
-
Else
-
Call .Controls(Split(strFrom, ",")(1)).Requery
-
End If
-
End With
-
On Error GoTo 0
-
End Sub
Welcome to Bytes!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Apple |
last post by:
May any one can teach me the easy way to create a combo box in a form
to find record.
|
by: Ray |
last post by:
To select all, I leave the
combo box blank and press Enter. In my query for my criteria I use
like Forms!! &"*"
It does not work when I open the form. I need to press the space bar or...
|
by: Mark Perona |
last post by:
I created an ASP.net form with an editable datagrid on it. I can
create new records, and update and delete existing records. The
problem I have is that I want a field in the grid to be editible...
|
by: smiler2505 |
last post by:
Sub cmbENum_AfterUpdate()
DoCmd.RunSQL "INSERT INTO tblExa(CCanEnt) SELECT tblCwS.CCanEnt FROM tblCwS WHERE tblCwS.ENum = tblExa.ENum"
End Sub
Tried that, without success.
When I change...
|
by: jyotig |
last post by:
hi,
i am using combo box for company list in ms-access
but if new company name will come that time i am going to table and add new company and again open form and select that company.
there is...
|
by: mbbum88 |
last post by:
Hi there,
I'm still relatively new to Access and am trying to build a database to track applicants for jobs.
The form I'm having a problem with is one for "Editing applications". I want to use...
|
by: ghjk |
last post by:
I'm new to java.I,m developing web application using jsp,struts,hibernet. I want to edit database record. This is my code and when I edit and save it it didn't update record but add a new one. Please...
|
by: prashantdixit |
last post by:
Hi,
I am developing a stock control software. Iam stuck somewhere.
I have a form "Add new stock" consisting of combobox, text boxes etc. which is used to add records in a table.
I have another...
|
by: lenniekuah |
last post by:
Hullo Friends,
I need your help. Please help me.
While trying to verify an existing record in the SQL SERVER table using the user input on TextBox it generate this error message:
Input string...
|
by: TheSmileyCoder |
last post by:
Im using an imagecombo in my form, and I have its Text property set to "Please Select". The text property of the imagecombo is what is displayed when you have not yet made a selection, kinda like...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |