Customize auto-complete functionality on Microsoft Access datasheet - ms-access

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?

Related

Store filtered employees in training record

Good afternoon,
I have a form used to create a "toolbox talk". A toolbox talk is not always applicable to every employee but might be limited to a specific department, gender, or competency (or any combination of the three). I have created a subform which using unbound text boxes allows me to filter my list of employees based on the toolbox talk criteria - great!. See screenshot below:
However what I cant work out now is how I can then store the filtered results (list of employees) as recipients against the toolbox talk record.
I know that I could include a control to select employee ID/Names line by line as per the filtered results, however I was hoping that a quicker option could be created.
Can someone please suggest a possible solution?
Thank you in advance.
If you want to 'batch' create records, one approach is an INSERT SELECT action SQL that uses the same criteria selected by the form controls. If your code applies this criteria to the form Filter property, that property can be referenced to incorporate filter criteria for SQL. I prefer to use CurrentDb.Execute because then don't have to deal with RunSQL popup warnings.
Private Sub Command16_Click()
Dim mySql As String
mySql = "INSERT INTO tblEmployeeToolboxTalk (EmployeeID, ToolboxTalkID) " & _
"SELECT EmployeeID," & Me.ToolboxTalkID & " AS TTID FROM tblEmployees WHERE " & Me.Filter
CurrentDb.Execute mySql
End Sub
If you need guidance on conditionally building filter criteria in VBA, review http://allenbrowne.com/ser-62.html

Blank subform: Unable to find a record, based off multiple fields

I've looked around for an answer to this issue, but have fond none. Thank you in advance to anyone who's able to help. I'm trying to look up records and alter them, based off multiple fields. However, my form shows up blank.
I have a database with one to many links for the following tables:
Sample->Set->Catch->Length->Diet (Key fields: SampleID, SetID, etc.)
Preliminary data is entered. I have additional data for some individuals to be entered into the Length and Diet tables. So, I created a form with combo boxes that allow the user to navigate to the correct fish by selecting: Date, Station, Set, Species, and Length. So, when I select a date, I'm restricted to stations sampled on that day and so on. I have a query string set up to restrict results to those matching the criteria entered into the combo boxes. My subform is based off the final query in this string (Query 5). It's linked on the primary key field for the length table (LengthID). All fine so far.
The issue: When I open my form and select values for each combobox, the subform remains blank. However, I can run Query 5 from the sidebar at this point and it runs successfully. I could just enter data directly into the query, but it would be less streamlined and vulnerable to human error.
I've also tried opening my subform directly from the sidebar. When I do this, Access prompts me for the Date, Station, Set, Species, and Length. Twice. The form then shows up and all are fields blank including the LengthID field, which should be filled in (since I'm looking up an existing record). I don't know why it prompts me twice, but I think that the subform isn't showing up in regular form view because the database sees the LengthID field as blank.
My combo boxes appear to navigate correctly to a given record. The query string my combo boxes and subform are based on all work when run directly. But I can't enter data into my subform, presumably because the subform can't find the correct record even though the query it's based off of can find it just fine. I've run out of troubleshooting ideas, any advice is greatly appreciated. Thanks!
If I understand correctly, you're trying to achieve a query WHERE clause utilizing combo-boxes. There are many SO questions out there on this but this should get you going. Let me know if you run into trouble and I'll help you out.
I assume you have a linked parent/subform combo. This code would be on the parent form search button:
Dim strSQL As String
strSQL = " 1=1 "
If Not IsNull(cmbComboBox1) Then
strSQL = strSQL & " AND Field1 = " & cmbComboBox1
End If
If Not IsNull(cmbComboBox2) Then
strSQL = strSQL & " AND Field2 = " & cmbComboBox2
End If
If Not IsNull(cmbComboBox3) Then
strSQL = strSQL & " AND Field3 = " & cmbComboBox3
End If
Me.Filter = strSQL
Me.FilterOn = True

How to make MS Access run a query on focus lose event and take actions accordingly

I am creating a small Access DB for our Data Entry guys. Our main DB is mysql but due to ease of use we are creating an Access DB to make it easier for them to enter the Data. I have done most of it but am stuck with this problem (which I know how to solve in mysql+php) Please pardon my ignorance, but I have just started using MS Access.
I have two tables - ClientPhones and sales. The ClientPhones table has phone, clientid fields. sales table has salesid, clientid, date, etc fields.
I also have a Form which has all relevant fields for the sales table. I want to add a new field, phone_no in that form. When a user inputs the number and on focus lose event, I was access to run a query on the clients table to see if the phone number exists in any of the 3 phone number fields. If it finds a client with that phone number, the client ID should be populated, else a new form to input the client details should be shown.
Is this possible with MS access or am I doing this incorrectly?
Use the text box's After Update event to retrieve the clientid which matches the phone number the user entered.
If a clientid is found, store it in the text box which is bound to clientid.
If no match is found, ask whether the user wants to add a new client, and open that form if they respond yes.
This code outline assumes txtSearchPhone is the name of the text box where the user enters the target phone number, and txtClientId is the name of the text box where you want to store clientid.
Private Sub txtSearchPhone_AfterUpdate()
Dim varClientId As Variant
Dim strCriteria As String
strCriteria = "[phone]='" & Me.txtSearchPhone.Value & "'"
Debug.Print strCriteria '<-- inspect this in Immediate window; Ctrl+g will take you there
varClientId = DLookup("clientid", "ClientPhones", strCriteria)
If IsNull(varClientId) Then
If MsgBox("Add new user?", vbYesNo) = vbYes Then
'DoCmd.OpenForm ... (see Access help topic)
End If
Else
Me.txtClientId.Value = varClientId
End If
End Sub
Make sure the text in txtSearchPhone does not include a single quote character (') because the DLookup will break if it does. You can use the text box's Validation Rule and/or Before Update event to make sure a single quote is not present.

Populating a form from a table

I'm not sure if I used the right title for this question but I'll try to ask it. Be patient with me I'm new to Microsoft Access.
I'm making an MS-Access database for employee reviews at my company and I made the form and all of the data from the form is going to two seperate table. The tables are linked through the same rating ID for each time the form is filled.
I have a seperate form that when you select the name of the stakeholder reviewed it fills a listbox with the name of the leader who reviewed them, the date, and the rating ID. I used VB to make it to where if you select the rating ID then it will open the form in which the data was put into using DoCmd.OpenForm "FRM".
Now for the question I hope that was all understandable. I would like for the form that is brought up from selecting the rating ID to be repopulated with all of the information that was entered into the table for that specific ID. What do I need to do to do this?
Another option is to base the form on a table or query (its RecordSource) and use the Where clause argument when opening the form:
DoCmd.OpenForm "frmName", acNormal, , "[SomeID]=" & frmYours!ctlControlName
This has the advantage that the form can be opened separately, as it will just show all the data from its RecordSource, not producing an unwanted parameter request.
If SomeID is text then its value needs to be quoted with apostrophes:
DoCmd.OpenForm "frmName", acNormal, , "[SomeID]='" & frmYours!ctlControlName & "'"
There are various possible solutions to this.
The first one (and simplest) is to write a query in the RowSource property of your form with the appropriate data source, and include a filter field in that query that corresponds to the Id you are filtering:
select a.*
from [your table] as a
where a.[id] = forms!frmYourForm![theControlName]
That way, every time you open the form, it will show the appropriate data.
The second solution (a little more sophisticated) is to edit the RowSource property on run-time. In the code that opens the form, you can do something like this:
strSQL = "select a.* from [your table] as a where a.Id = " & theControlName.Value
DoCmd.OpenForm "theDetailedForm"
form_theDetailedForm.RowSource = strSQL
form_theDetailedForm.Requery
Hope this helps you

Editable, Appendable ComboBox(?) in 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.