Remember Me
forgot your password?

Ms Access Tip: Self Learning Combo Box

Database data entry can be tedious at times. You can make your databases more user friendly if they have a way to select data to enter rather than typing in the same data over and over. Access provides a form control called a combo box to help with this task but it can lead to trouble. Also, if you find that the data is redundant, you will probably want to be able to sort on that field in the future for reporting. The combo box allows the user to select inputs from a preset location like a table, a list of values, or a preset query. This works well unless the option you want to enter is not in the preset data. Access will not let you put in a new value unless you set the Limit to List option to No. Then, users are allowed to put in any data without checks. This can be troublesome. The value of having the combo box can be quickly diminished if users are misspelling data as they enter it or using different data to mean the same thing. This is very easily seen as you can define the United States as US, USA, United States, United States of America all meaning the same thing. Subsequent reporting would be inaccurate with misspelled input and dissimilar input for the same field.

To over come that problem, it would be handy to have code that asks the user to confirm a new entry into the table before they go ahead, just to make sure they entered what they meant to. This example shows you how to accomplish this with what I call a self learning combo box. Meaning, it learns new entries and adds them to the list of preset values, but requests a confirmation from the user if the entry is not already in the table. Use the following to create a simple form to demonstrate the self learning combo box and make it a feature you give your users in the future.

Create a new database and do the following steps:
Set references by adding the Microsoft DAO 3.6 Object Library to the current checked list. Do this by opening the VBA editor and clicking on Tools/References then checking the library from the list.

Create a table named contacts with these fields.
ConId as autonumber set as the primary key field
ConName as text
ConType as text
Fill in a line or two of test data before going on to the next step.

Design a very simple form for the example with one text box and two combo box controls on it.
Set the form’s data source to contacts
Set the control source for the text box as ConName and name it ConName on the Other Tab

Set the control source for the first combo box as ConType and name it xType
Set the Row Source to SELECT Contacts.ConType FROM Contacts GROUP BY Contacts.ConType;

Set the control source for the second combo box to ConType and name it Type
You can do this later if you want, but you can set the Format control for Visible to No for this combo box.

In Design View of the Form cut and copy in the following Form Code

Option Compare Database
Private Sub Type_NotInList(NewData As String, Response As Integer)
Me.Type.LimitToList = False
End Sub

Private Sub xType_AfterUpdate()
Dim Resp
MiSQL = "SELECT ConTypes.ConTypes FROM ConTypes WHERE (((ConTypes.ConTypes)='" & xType & "'));"
GetMiRSet ‘ calls the function GetMiRSet from a module
If MiRec.RecordCount = 0 Then
Resp = MsgBox("The Type entered '" & xType & "' is not in list. Do you wish to add? Press OK or CANCEL..", vbOKCancel, xType & " Not On List")
If Resp = 1 Then
MiSQL = "INSERT INTO ConTypes ( ConTypes ) SELECT '" & xType & "' AS x1;"
PutMiRSet ‘calls the function PutMiRSet from a module
MiSQL = "SELECT ConTypes.ConTypes FROM ConTypes WHERE (((ConTypes.ConTypes)='" & xType & "'));"
GetMiRSet
Me.Type.Requery
Me.Type = MiRec!ConTypes
Form.Refresh
Else
xType = Null
End If
Else
Me.Type = MiRec!ConTypes
Form.Refresh
End If
End Sub

Now close the form and save it.

Next you will create a handy new module you can use in any database to save a little work.
This code is handy in Access because it makes it a little quicker to work with Recordsets by predefining command options ahead of time. Using this module you can simply call GetMiRSet when you want data from a table and PutMiRSet when you want to put data into a table with VBA. The required input for both functions is the SQL string defined as MiSQL. It is needed to define the data set you want to use in the format like the following:

MiSQL = "INSERT INTO ConTacts( ConType ) SELECT '" & xType & "' AS x1;"

You should see a line similar to this in the above list of commands.

Hint: Use the New Query function to create your SQL statements. After you have designed them in Design View, change to SQL view and you will see the SQL string. Copy this and place it into the VBA editor to save some time. You may have to make some modification to the SQL string you copy after you put it into your VBA code on the form. Like in our example, you can see where we are using the xType control name like a variable in the string. You can’t do this inside the string quotes so you have to concatenate two strings together with the & symbol. Also, in this sql string, xType is a string, so if you want to put it into the statement, like all strings, it has to be inside quotes. But, since the string is in a string, use single quotes. It will help to put a debug.print MiSQL statement after this line to see how Access is going to interpret the string.

Now create a New Module and put the following lines into it and save it under any name.

Option Compare Database
Option Explicit
Public MiSQL As String, Midb As Database, MiRec As DAO.Recordset, MiRec2 As DAO.Recordset

Public Function GetMiRSet()
Set Midb = CurrentDb()
Set MiRec = Midb.OpenRecordset(MiSQL, dbOpenDynaset)
End Function

Public Function GetMiRSet2()
Set Midb = CurrentDb()
Set MiRec2 = Midb.OpenRecordset(MiSQL, dbOpenDynaset)
End Function

Public Function PutMiRSet()
Dim Midb As Database ', MiRec As DAO.Recordset
Set Midb = CurrentDb()
Midb.Execute MiSQL
End Function

You are done. Go back to the form and give it a try. Enter a new value in the text box and then enter a new value in the first combo box and you should get a message box asking you to confirm the entry.

You can find a working example database at http://www.biomationsystems.com/AccessTips.htm

Jon Watson

Jon Watson is the founder of Biomation Systems, Inc. With 26 years experience helping Fortune 500 companies with process improvement he formed BioMation to bring the same expertise to smaller companies that need the same improvements at an affordable price.

Rate this Article: 0 / 5 stars - 0 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Technology Articles
  • More from Jon Watson

How to Create a Powerful Artificial Brain!

By: Said Benrida | 03/01/2010
I hear a lot about artificial intelligence in computer sciences and their power. But still we can’t trust that intelligence because it can’t understand and find solutions to any problem neither answer like a human does. What about creating a powerful artificial brain that we can trust, it can understand, find solutions to all problems, execute any order and answer like a human can do, even better? Then let’s try so.

How to Create an Infinite Source of Water!

By: Said Benrida | 02/01/2010
Water is so precious. It can be produced by many reactions but still we need more water. To create an infinite source of water we need to create water, not to produce it. Then how can we create water ?

Sustainable Energy

By: jerzy jagielski | 02/01/2010
In a world where resources are dwindling and everyone is cost sensitive, one solution remains. The world must turn to Sustainable Energy sources.

Pure Play Independent Software Testing Services Market in Europe

By: Bharat Book Bureau | 02/01/2010
Bharatbook.com added a new report on "Pure Play Independent Software Testing Services Market in Europe 2008-2012" is helping the companies improve the quality of software considerably by incorporating corrections during the development phase.

Unico Binocular Microscopes

By: Jeremy Linder | 02/01/2010
Binocular microscopes come with a wide variety of advanced features and specifications. They come with dual heads with 52 – 75 mm interpupillary distance adjustment.

Present & Future of Cell Phones

By: yanary | 02/01/2010
Cell Phones are experiencing the new trends in technology. Within few decades, the industry has created a boom in sales due to the emerging technologies.

polymer science

By: Dhar Kalyan Kumar | 02/01/2010
FUNDAMENTALS OF POLYMER SCIENCE Thermal Transitions in Polymers

StudentScholarships.org - How DNA Profiling has changed Policing

By: Drew Backley | 01/01/2010
Because of DNA profiling, we are able to determine who should be placed in prison for committing crimes as well as those placed there unjustly. With the development of DNA profiling, lives have been saved. Instead of prisoners being unjustly executed because of death penalty laws, they are once again able to rejoin their loved ones and continue leading productive lives.

Ms Access Tip – you Have Heard ofs, Now Learn to Use Them

By: Jon Watson | 11/01/2008 | Management
This example demonstrates using an array and SQL statements to create the similar records. It also has a function to replace records if you want to change the amount value and update the data table. The replace function assumes that the description and date are the same and replaces every occurrence of the existing record with the latest amount value. For safekeeping the replace function makes a copy of the data table before executing.

Ms Access Tutorial - How to Use Color Coding to Enhance your Applications

By: Jon Watson | 18/12/2007 | Management
Color coding adds value to a form as it easily directs the user's eye to critical data. This is a powerful technique that allows a user to quickly evaluate a pages of data for errors or warning information.

How to Get the Most From your Training Dollar

By: Jon Watson | 12/12/2007 | Management
There are so many people telling you that you can make such a huge return on your investment these days that you just can't believe any of them any longer. I know, I have received so many get rich emails that I should be earning about a billion a day now! How about investing in something that you really believe in. Yourself.

Ms Access Tip: Self Learning Combo Box

By: Jon Watson | 12/11/2007 | Technology
Database data entry can be tedious at times. You can make your databases more user friendly if they have a way to select data to enter rather than typing in the same data over and over. Access provides a form control called a combo box to help with this task but it can lead to trouble.

Access Databasetip: Create a User Defined Search Field for a List Box

By: Jon Watson | 02/11/2007 | Technology
Many times it is necessary to provide your users with an easy way to filter or search for data in large table of information. This tip will show you one quick and easy way to solve the problem by giving the user a field to enter a search string and updating the form with those fields that fit the search pattern. It's quick and easy, so give it a try. Read about how Access was a big success solving a license tracking issue in this article.

Access Database Tip: How to Multiselect in a Listbox

By: Jon Watson | 26/10/2007 | Technology
When you have a list box it is handy to be able to multiselect to perform some action on multiple items in the listbox. Here is a simple set of instructions to get you started. These instructions will create a quick way to multiselect and delete rows from a table.

Ms Access Tip: Keeping Up With User Activity

By: Jon Watson | 25/10/2007 | Technology
At times you will find it very handy to know what a user was doing in your application at a specific time. This is especially useful with multi-user database applications for debugging or security reasons. With this tool implemented in your applications you will be able to see what event happened and at what time.

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.06, 1, w3)