473,387 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Look Up existing Record or Add New using Combo Box

2
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
Sep 27 '09 #1
4 8715
ADezii
8,834 Expert 8TB
@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.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboTest_NotInList(NewData As String, Response As Integer)
  2. Dim strMsg As String
  3. Dim MyDB As DAO.Database
  4. Dim rstCustomers As DAO.Recordset
  5.  
  6. strMsg = "'" & NewData & "' is not in the list.  "
  7. strMsg = strMsg & "Would you like to add a New Customer?"
  8.  
  9. If vbNo = MsgBox(strMsg, vbYesNo + vbQuestion, "New Customer") Then
  10.   Response = acDataErrDisplay 'Access displays its standard Error Message
  11. Else
  12.   'Decided to Add the New Customer
  13.   Set MyDB = CurrentDb
  14.   Set rstCustomers = MyDB.OpenRecordset("tblCustomers", dbOpenDynaset)
  15.     rstCustomers.AddNew
  16.       rstCustomers("Name") = NewData
  17.     rstCustomers.Update
  18.  
  19.     Response = acDataErrAdded   'Item added to underlying Recordset and the Combo
  20.                                 'Box is Requeried, Item added to List
  21.     rstCustomers.Close
  22.     Set rstCustomers = Nothing
  23. End If
  24. End Sub
Sep 27 '09 #2
JTSC
2
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Customer_ID_NotInList(NewData As String, Response As Integer)
  2. Dim strMsg As String
  3. Dim MyDB As DAO.Database
  4. Dim rstCustomers As DAO.Recordset
  5.  
  6. strMsg = "'" & NewData & "' is not in the list.  "
  7. strMsg = strMsg & "Would you like to add a New Customer?"
  8.  
  9. If vbNo = MsgBox(strMsg, vbYesNo + vbQuestion, "New Customer") Then
  10.   Response = acDataErrDisplay 'Access displays its standard Error Message Else
  11.   'Decided to Add the New Customer
  12.   Set MyDB = CurrentDb
  13.   Set rstCustomers = MyDB.OpenRecordset("Customers", dbOpenDynaset)
  14.     rstCustomers.AddNew
  15.       rstCustomers("CustomerName") = NewData
  16.     rstCustomers.Update
  17.  
  18.     Response = acDataErrAdded   'Item added to underlying Recordset and the Combo
  19.                                 'Box is Requeried, Item added to List
  20.     rstCustomers.Close
  21.     Set rstCustomers = Nothing
  22. End If
  23.  
  24. End Sub
I'm not sure what to do next....

Thanks
Sep 27 '09 #3
ADezii
8,834 Expert 8TB
@JTSC
Looks like you forgot an 'Else' between Lines 10 and 11.
Sep 27 '09 #4
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.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. 'strFrom will contain the name of the form, and optionally, the name of the
  5. 'control (if required) separated by a comma (,).
  6. Private strFrom As String
  7.  
  8. Private Sub Form_Open(Cancel As Integer)
  9.     strFrom = Nz(OpenArgs, "")
  10. End Sub
  11.  
  12. ...
  13.  
  14. Private Sub Form_Close()
  15.     'Counterintuitively, setting .Dirty to False saves the changes.
  16.     Me.Dirty = False
  17.     'Requery the calling form or control.
  18.     If strFrom = "" Then Exit Sub
  19.     On Error Resume Next
  20.     With Forms(Split(strFrom, ",")(0))
  21.         If UBound(Split(strFrom, ",")) = 0 Then
  22.             Call .Requery
  23.         Else
  24.             Call .Controls(Split(strFrom, ",")(1)).Requery
  25.         End If
  26.     End With
  27.     On Error GoTo 0
  28. End Sub
Welcome to Bytes!
Sep 27 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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.
1
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...
2
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...
5
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...
1
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...
1
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...
0
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...
11
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...
0
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...
3
TheSmileyCoder
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.