Editable, Appendable ComboBox(?) in MS Access - ms-access

My Goal:
A form field (in MS Access) that is has some drop down choices. If the wanted value isn't in the lookup table, the user should be able to add it by typing it in.
Let's suppose the lookup table has rows: A, B, C, D. The user wants "E" which doesn't exist yet. Ideally they'd "override" and type in "E", which would then be added to the lookup table for future entries.
My google-fu has failed on this. Is there a term for this that I should be using? What are some good approaches? (I've been playing with combo-box and its wizard so far).
Thank you for any suggestions!

Aha, solved my own here:
http://allenbrowne.com/ser-27-01.html
Access 2007
To use the new properties in Access
2007:
Open your form in design view.
Right-click the combo, and choose Properties.
On the Data tab of the Properties box, set Allow Value List
Edits to Yes, and List Items Edit
Form to the name of the form to use
for adding items to the list.
When you are using this form, you can
now right-click the combo, and choose
Edit List Items.
There is also advice for older versions of Access.

You can try the following code:
Private Sub Combo33_NotInList(NewData As String, Response As Integer)
Dim strSql As String
If MsgBox(NewData & " not in list, add?", _
vbYesNo + vbQuestion) = vbYes Then
strSql = "insert into tblStudents (name) values(" & NewData & ")"
CurrentDb.Execute strSql
Response = acDataErrAdded
End If
End Sub
Note I used a table name of Students, and field name of Sname. So, just
change the table name, and the field to whatever you used.

Related

Coding a button on a form in MS Access

I'm trying to build a database with MS Access. I have two tables- StockFrames and Projects, and I have a form- FrameCheckOut. On the form I have a FrameID field (where we will type in a frame id number or scan its barcode) and a ProjectName field, with a drop down of project names from the Projects table. I also have a button- Assign Frame. I want the button to update the StockFrames table with the projectID number so that I can know whether or not a frame is currently in use (or "checked out") to a project.
I have tried assigning this code to the button On Click:
UPDATE StockFrames
SET StockFrames.projectID = [SELECT Projects.projectID
FROM Projects WHERE Projects.projectName LIKE projectName]
WHERE frameID = frameID;
.. but that code contains invalid syntax. I am very new to Access and coding and I would really appreciate some help if anyone is willing.
Include key field in combobox (column can be hidden) RowSource. Value of combobox will be ID but users see and type name. Combobox properties:
RowSource: SELECT ProjectID, ProjectName FROM Projects ORDER BY ProjectName;
BoundColumn: 1
ColumnCount: 2
ColumnWidths: 0";2"
ControlSource: leave blank if control is used to enter search criteria, otherwise field you want to save into
If form is bound to StockFrames, an UPDATE action is not needed. Find record for specific FrameID and simply select project from combobox.
If you prefer to use UPDATE, then have UNBOUND comboboxes for Frames and Projects designed as described above. Example VBA:
Private Sub AssignFrame_Click()
CurrentDb.Execute "UPDATE StockFrames SET ProjectID = " & Me.cbxProject & _
" WHERE FrameID = " & Me.cbxFrame
End Sub

Customize auto-complete functionality on Microsoft Access datasheet

I have a main form tied to a User record, with a subform tied to a number of Client objects the user "owns." So, there is a one-to-many relationship between User and Client.
As the subform exists, the user can add, remove, and edit entries in the Clients subform. When a user adds an entry to the subform datasheet, there is an autocomplete functionality that kicks in the user types part of a Client name that matches any names in the Client database, thus saving the user a few keystrokes and ensuring that the user enters a name that exactly matches one in the Client database.
One thing to note with the Clients table is that in addition to each Client having a unique numerical ID, each Client has a full company name (Test Agency, Inc.), a colloquial name (Test Agency) and an abbreviated name (TA).
I am trying to edit the subform so that the autocomplete functionality will match against any of the three fields listed above (full name, colloquial name, and abbreviated name). Right now, the autocomplete only works against the full name, as that is the field linked to the subform. I would like the user to be able to type in a part of a string, the subform to try and match it to any of the three fields (full name, colloquial name, abbreviated name) and return a list of potential matches to any of the three fields. When the user selects the correct potential match for the Client they are trying to search for, then then full company name would be entered into the datsheet. Basically, these additional fields just make it easier for the user to find the Client they are looking for (imagine typing in AMD instead of Advanced Micro Devices, Inc.)
My first question--is this possible to do with a simple datasheet? I've looked into using lookup fields and multi-value lookup fields, but I'm not sure this is the right method. Or will I need to build myself a custom control that does this matching on multiple fields?
Made a query like this
SELECT *
FROM Company
WHERE fullName LIKE '*' & pCompany & '*'
OR Colloquial LIKE '*' & pCompany & '*'
OR Abbr LIKE '*' & pCompany & '*'
and on my form I did this
Private Sub cboCompany_KeyUp(KeyCode As Integer, Shift As Integer)
ClearCombo cboCompany
Dim sql As String
Dim rs As DAO.Recordset
Dim companySearch As DAO.QueryDef
Set companySearch = CurrentDb.QueryDefs("CompanySearch")
companySearch.Parameters("pCompany") = cboCompany.Text
Set rs = companySearch.OpenRecordset
Do While Not rs.EOF
cboCompany.AddItem rs("ID") & ";" & rs("FullName") & ";" & rs("Colloquial") & ";" & rs("Abbr")
rs.MoveNext
Loop
End Sub
Private Sub ClearCombo(cbo)
For i = cbo.ListCount - 1 To 0 Step -1
cbo.RemoveItem i
Next i
End Sub
It's not super fast at all but it works. I think what would make it faster is not cuing off the KeyUp event and instead on a timer once users start typing in that field. Then turn the timer off when they stop typing or focus leaves the combobox.
So you are already able to search with partial string - probably by means of a query with a like condition.
The next step is very easy. Do the search for every field, combine them by UNION, and remove duplicates by means of a SELECT DISTINCT.
Hope this succinct answer will suffice?

MS access add item to combo box if non in list

I have an access form, i want to know how to add item in combo box if there is not in there.
my combo box is in value mode.
Unfortunately you cannot change the rowsource permanently without changing the form to design mode and adding the new value. It is possible to do this with code, but it is not a good idea when people are working. The easiest thing is to create a small table and add the values to that. New values will then be saved when you close the form.
Allen Browne has a description of how to do this : http://allenbrowne.com/ser-27.html
This is one of the ideas he shows:
Private Sub CategoryID_NotInList(NewData As String, Response As Integer)
Dim strTmp As String
'Get confirmation that this is not just a spelling error.
strTmp = "Add '" & NewData & "' as a new product category?"
If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
'Append the NewData as a record in the Categories table.
strTmp = "INSERT INTO Categories ( CategoryName ) " & _
"SELECT """ & NewData & """ AS CategoryName;"
DBEngine(0)(0).Execute strTmp, dbFailOnError
'Notify Access about the new record, so it requeries the combo.
Response = acDataErrAdded
End If
End Sub
You'll need to set the property limit to list to true.
Then add some code in the On Not In List event that potentially adds the value to the combo box. Here is a tutorial. Or you can view the other answer.
Please note that it is usually better to utilize a table that stores the values for your combo box. With a value list, unless you disable the shortcut menus a user can right click on the combo box select Edit List Items... and modify the list even if it is set to limit to list... which effectively defeats any limitation you are trying to place on the field.

MS Access 2007 OpenForm method, can't get the where clause to produce the correct result

I can not get the OpenForm method to open a form with the correct record loaded. I'm going to do my best here to provide the details:
The 'source' form is based off Table A, and it is read-only. A button on the source form is being used to open a 'target' form that is used for editing records, also from Table A.
I have tried using the wizard to create the button that opens the form based on primary key equality. The result from the wizard is that no matter what record is in context on the source form, only the first record in Table A is ever loaded by the target form.
I have tried using a procedure with the following variations:
Dim frm As String, whr As String
frm = "Target Form"
whr = "Forms![Source Form]!ID = Forms![Target Form]!ID"
'whr = "[ID] = [ID]"
'whr = "[ID] = Forms![Target Form]!ID"
DoCmd.OpenForm frm, acNormal, , whr, , , 1
I could either get only the first record to load in the target form or get a new record to load in the target form. But I can not get the source form's loaded record to determine the record loaded into the target form.
Thanks for any help
I think you got close. You want the WhereCondition to be the same as a WHERE clause for a query of Target Form's record source, but without the word WHERE.
Dim frm As String, whr As String
frm = "Target Form"
whr = "[ID] = " & Me.ID
Debug.Print "whr: " & whr
DoCmd.OpenForm frm, acNormal, , whr
I intended Me.ID as the value of a control whose name is ID and is bound to a field in the current record of Source Form. Me is shorthand for "this form". One reason it's useful is because you wouldn't have to revise your code if you later decide to give the form a different name.
I added the Debug.Print statement so you can switch to the Immediate Window (Ctrl+g), and copy the whr string, then paste it into a new query based on the same record source which Target Form uses. That could be helpful in case you still don't get the correct record displayed when Target Form opens.
Your version also included 1 as OpenArgs to OpenForm. I didn't see how you were using that, so left it off. If Target Form includes an event procedure which does something with OpenArgs, make sure it doesn't override your WhereCondition.

MS Access search for record by textbox instead of dropdown

I'm pretty new to MS Access. I'm trying to create a simple form that will basically search for a particular record using a textbox, rather than a drop down box. Essentially a user would be able to enter an ID number and retrieve some other related Info. However, I do not want the user to be able to add any new records to the database. I've been able to get the forms to look the way I want them, but I'm not sure where to place the code (do I create a macro, insert the code into the properties of the button?) Any help is greatly appreciated!
I assume that you have bound your form to a table or a query and that you want to be able to enter the ID manually in a textbox, then press ENTER and load that record's data or display an error message if there is no such record.
As dsteele said, make sure that the form's Data property Allow Addtions is set to No to disallow users from adding records.
Then, from the AfterUpdate event of the textbox, add the following code (assuming that your textbox is named txtGoTo):
Private Sub txtGoTo_AfterUpdate()
If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.RecordSet
Set rs = Me.RecordsetClone
rs.FindFirst "[ID]=" & txtGoTo
If rs.NoMatch Then
MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
vbOKOnly + vbInformation
Else
Me.RecordSet.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null
End Sub
Note that you will have to change the line rs.FindFirst "[ID]=" & txtGoTo to something that is adequate for your data:
"[ID]=" should be replaced by the field you want to search (it could be "[POReference]=" or something else.
if you are searching by a numeric ID, for instance because the field is an autonumber column, then the code is fine.
Otherwise, if the field you are searching on is a string (say PN12-G) then you have to change the code to:
rs.FindFirst "[ID]=""" & txtGoTo & """"
Failing to use the proper quoting (or quoting where not necessary) will result in errors of the kind Data type mismatch....
As a new user, I would recommend that you have a look at the sample NorthWind project database that is either shiped with older versions of Access or available as a template for download from Access 2007.
There a lots of techniques to learn from as a new Access developer, including other ways to implement record navigation.
Set the form property Data/'Allow Additions' to No.
Either in the AfterUpdate event of the textbox, or in the Click event of a button, you can write code or assign a macro to look up and display the record you want.