Microsoft Access - combo box dropdown errors - ms-access

I am new to VBA/Access so I have been using queries/VBA code I find online to help me build my current form. I experiencing weird behavior with my combo box and i think it may be due to the VBA/queries I have attached to it.
The code below is used to make the combo box a dynamic search tool: as you type each letter, it reruns the query to update the list with only the last names that match the letters typed.
However, pressing the arrow keys/tab/enter does nothing. The only way to navigate/select a value in the dropdown is a mouse click. And when I click the value I want, the form populates the data from the that record (yay!) but the dropdown menu stays visible until i click the background of the form.
I want the dropdown to disappear as soon as I select the record I want, and if possible I want to be able to use the arrow keys to navigate.
The "On Change" event has this code:
Private Sub Combo1397_Change()
Dim strText, strFind
Combo1397.SetFocus
strText = Me.Combo1397.Text
If Len(Trim(strText)) > 0 Then
strFind = "[Last Name] Like '"
For i = 1 To Len(Trim(strText))
If (Right(strFind, 1) = "*") Then
' When adding another character, remove the
' previous "*," otherwise you end up with
' "*g**w*" instead of "*g*w*."
' This has no apparent impact on the user, but
' ensures that the SQL looks as intended.
strFind = Left(strFind, Len(strFind) - 1)
End If
strFind = strFind & "*" & Mid(strText, i, 1) & "*"
Next
strFind = strFind & "'"
strSQL = "SELECT tbl_RC_Main.pk_CandidateID, tbl_RC_Main.[Last Name],tbl_RC_Main.[First Name], tbl_RC_Main.Email FROM tbl_RC_Main Where " & _
strFind & " ORDER BY [Last Name];"
Me.Combo1397.RowSource = strSQL
Else
' Show the entire list.
strSQL = "SELECT tbl_RC_Main.pk_CandidateID, tbl_RC_Main.[Last Name],tbl_RC_Main.[First Name], tbl_RC_Main.Email FROM tbl_RC_Main ORDER BY tbl_RC_Main.[Last Name]; "
Me.Combo1397.RowSource = strSQL
End If
Me.Combo1397.Dropdown
End Sub
Also,
On the "After Update" event I have a "Search for Record" macro

Because you have Me.Combo1397.Dropdown in the Change event, the combobox will drop down again upon selecting a value. You should instead move Me.Combo1397.Dropdown to the Enter event.
If you also remove Combo1397.SetFocus (I can't see why it's needed) then you will be able to use the Enter/Tab/Arrow keys as well.

Related

Why does Access VBA dropdown method not work?

I'm using a text box to filter a combo box list in Access 2013. I put the filter code in the Form_Timer sub so as to give users time to type the entire filter string before applying the filter, and I invoke the timer from the text box Change sub. It works great except for one thing: I want the combo box list to drop down and display results, and it just won't work. However I put the exact same line of code in the GotFocus sub for the combo box, and that line works perfectly.
I also tried executing the filter code within the Change sub, just in case there was some weirdness regarding Form_Timer execution. Same result. Here is the code:
Private Sub cboCENamesMain_GotFocus()
Me.cboCENamesMain.Dropdown '<---This line works perfectly.
End Sub
Private Sub Form_Timer()
Dim strSQL As String
Me.TimerInterval = 0
Me.txtFilter.Value = Me.txtFilter.Text
Me.cboCENamesMain.SetFocus
strSQL = ""
strSQL = strSQL & "Select DISTINCT [CE ID] "
strSQL = strSQL & "From [tblMyTable] "
If Len(Me.txtFilter) > 0 Then
strSQL = strSQL & "Where [CE ID] Like ""*" & Me.txtFilter & "*"" "
End If
strSQL = strSQL & "Order By [CE ID];"
Me.cboCENamesMain.RowSource = strSQL
Me.cboCENamesMain.Dropdown '<---This line doesn't do what it's supposed to.
Me.txtFilter.SetFocus
Me.txtFilter.SelStart = Len(Me.txtFilter.Text)
Me.txtFilter.SelLength = 0
End Sub
Private Sub txtFilter_Change()
If Len(Me.txtFilter.Text) = 0 _
Or Len(Me.txtFilter.Text) > 2 Then
Me.TimerInterval = 500
End If
End Sub
I could use a list box instead of a combo box to allow users to see the results of their filter typing, but that would seriously detract from my form design. I have searched on Google and on StackOverflow, and have not found anybody else discussing this issue. Any ideas?
Here is my final code. This works just the way I want. The user types a few characters, and the timer automatically filters the combo box list down to items containing the typed string.
StackOverflow is a great place to file stuff like this - going from job to job it will save some time reinventing the wheel.
The extensive comments in the code are for the analysts who will need to maintain the applications after my contract ends. Neither of them has ever written VBA.
Private Sub cboCENamesMain_Change()
' Ordinarily the AfterUpdate event would automatically fire off the Change event.
' We set a flag to avoid that - otherwise the list would be filtered to only the selected record.
If booCancelChange = False Then
' Don't bother filtering the combo box list unless the filter text is null or longer than
' 1 character.
If Len(Me.cboCENamesMain.Text) <> 1 Then
' Set the Form_Timer event to fire off in 0.3 second to give them time to type a few characters.
Me.TimerInterval = 300
End If
Else
' Reset the flag, otherwise the Change code would stop working after the first record selection.
booCancelChange = False
End If
End Sub
Private Sub Form_Timer()
Dim strSQL As String
' Reset the timer to not fire off, so that it won't keep running without a change
' in the combo box.
Me.TimerInterval = 0
' If they have tabbed out of the combo box after selecting an item, we don't want to
' do this. It's unnecessary and it throws errors from references to the control's
' properties when focus is no longer on the control.
If Screen.ActiveControl.Name = Me.cboCENamesMain.Name Then
' Create a SQL filter for the combo box using the entered text.
strSQL = ""
strSQL = strSQL & "Select DISTINCT [CE ID] "
strSQL = strSQL & "From [Covered Entities] "
If Len(Me.cboCENamesMain.Text) > 0 Then
strSQL = strSQL & "Where [CE ID] Like ""*" & Me.cboCENamesMain.Text & "*"" "
End If
strSQL = strSQL & "Order By [CE ID];"
' Apply the filter.
Me.cboCENamesMain.RowSource = strSQL
Me.txtRowCount = Me.cboCENamesMain.ListCount
' Drop down the combo list so they can see the results of their filter text.
Me.cboCENamesMain.Dropdown
End If
End Sub

Hyperlink text disappears based on combo box value on continuous form

I'm having a bit of an unusual problem (or at least I think it's unusual). I have a continuous form that shows certain records based on a query that updates after I change the value in a combo box. I have a "View All" hyperlinked text box so that I can switch to seeing all of the records when I want to, but have discovered it only appears when I have selected a combo box value that has records. For example, if I want to find records assigned to Joe Shmo and there is one record assigned to him, the hyperlink appears. If I want to find records assigned to Susie Seashell and she has none assigned to her, it disappears. The catch is that if I click on the area of the screen where the hyperlink should appear, it still works as expected-- there is just nothing visible that would lead you to click there. The text field's visibility is on, and I even tried coding in a txtfield.visible=true and it did nothing. Any help would be appreciated!
Note: The hyperlink control source is ="View All". Not sure if that matters or not.
All of the VBA code used on this form is below:
Option Compare Database
Private Sub cboFindNotice_AfterUpdate()
getSearchResults
End Sub
Private Function getSearchResults()
Dim sql As String
Dim errMsg As String
'== Sets value of string 'sql'
sql = "SELECT tblNotice.ID, tblNotice.noticeSEIFnoticeNumber, tblNotice.noticeJurisdiction, tblNotice.noticeDueDate, tblNotice.noticeTitle, " _
& "tblAnalysts.[analyst_lName] & "", "" & [analyst_fName] AS Expr1, tblNotice.noticeStatus" _
& " FROM tblAnalysts INNER JOIN tblNotice ON tblAnalysts.ID = tblNotice.noticeAnalyst" _
& " WHERE tblNotice.noticeAnalyst LIKE '*" & Me.cboFindNotice & "*'" _
& " ORDER BY tblNotice.noticeDueDate"
'== Displays records based on query
Me.Form.RecordSource = sql
Me.Form.Requery
End Function
Private Sub noticeSEIFnoticeNumber_Click()
DoCmd.OpenForm "frmNoticeDetails"
[Forms]![frmNoticeDetails].Controls("txtControlField").Value = Me.Controls("ID").Value
Call Forms.frmNoticeDetails.txtControlField_AfterUpdate
End Sub
Private Sub txtViewAll_Click()
Me.cboFindNotice.Value = ""
Call cboFindNotice_AfterUpdate
End Sub

Passing query result to a textbox control on an access form

I need help with a textbox field on an Access 2007 form. I'm trying to insert the result of a query into the text box control on the form. This is used soley as information for the user. The form supplies the query with parameters to get the value. The query works fine and returns the correct result. What I can't seem to figure out is how to pass the query result to the textbox. I’ve tried several different ways but with no luck.
(PS> I know a combo box can do a lookup, however I don’t want the user to have to click the dropdown just to select the value as there can only ever be one value result from the query.) I'm open to suggestions as I'm not a programmer or DB Admin, but I've taking a few classes on Access (enough to be dangerous).
Private Sub cbo3_Change()
Me.tbx2 = ("SELECT tbl_Billing.Savings_b FROM tbl_Billing GROUP BY tbl_Billing.UBI_b, tbl_Billing.TaxYr_b, tbl_Billing.TaxPrg_b, tbl_Billing.Savings_b HAVING (((tbl_Billing.UBI_b)=forms!f1_UpBilled!cbo1) And ((tbl_Billing.TaxYr_b)=forms!f1_Upbilled!cbo2) And ((tbl_Billing.TaxPrg_b)=forms!f1_UpBilled!cbo3));")
End Sub
If you wish to do this in run time, you need to do the following, I take the controls you are referring to in this is on the same form.
The very simple and straight forward way to get it done is as follows,
Private Sub cbo3_Change()
Dim tmpRS As DAO.Recordset
Set tmpRS = CurrentDb.OpenRecordset("SELECT tbl_Billing.Savings_b FROM tbl_Billing GROUP BY " & _
"tbl_Billing.UBI_b, tbl_Billing.TaxYr_b, tbl_Billing.TaxPrg_b, " & _
"tbl_Billing.Savings_b HAVING ((tbl_Billing.UBI_b = '" & Me.cbo1 & "') And (tbl_Billing.TaxYr_b = '" & Me.cbo2 & "') " & _
"And (tbl_Billing.TaxPrg_b = '" & Me.cbo3 & "'))")
If tmpRS.RecordCount > 0 Then
Me.tbx2 = tmpRS.Fields(0)
Else
Me.tbx2 = 0
End If
Set tmpRS = Nothing
End Sub
Just note, I have implied all your combo boxes are returning String and the field you are comparing against are Text type. If that is not the case, you need to make changes accordingly.

filter continuous form using textbox

I need to let users filter a continuous form using values the user enters into a textbox. And the continuous form is also nested within a couple levels of navigation subforms. This sounds easy enough, but all the examples I find on the web use macros instead of vba.
I set up the structure and wrote an AfterUpdate procedure for a textbox txtFilter as follows:
Private Sub txtFilter_AfterUpdate()
Dim filterval As String
filterval = txtFilter.Value
With Forms!Main!NavigationSubform.Form!NavigationSubform.Form
.Filter = "LastName Like " & filterval
.FilterOn = True
End With
End Sub
I have played with different syntax, but none of it seems to work properly. Here is a link to download the relevant parts of the database from a file sharing site: http://jmp.sh/v/HGctZ4Ru74vDAjzN43Wq
Can anyone show me how to alter this so that users can use the textbox to filter the continuous form?
I got it to work using this: .Filter = "LastName Like """ & filterval & """"
Need those annoying String Identifiers even for strings sometimes.
Okay, To get the form to open with no records and then pull up just the records you (or the user) specifies is easiest with a bit of re-work.
(I'd recommend you working with a copy and not your original)
1:On your Continuous Form, remove the Recordsource; we're going to use Late Binding (Kinda)
2:Then delete the code under the txtFilter box, then delete the box itself.
3:Add a comboBox with something like this as the recordsource:
SELECT DISTINCT myTable.LastName FROM myTable ORDER BY myTable.LastName; (This will get you a unique list of last names so knowing how to spell the name will not be necessary, plus it assures at least one match)
4:In the After Update event of that combobox, add code like this:
Dim strSource As String
strSource = "SELECT mt.IntakeNumber, mt.ClientNumber, " & _
"mt.LastName, mt.FirstName, mt.ConsultationDate " & _
" FROM myTable mt " & _
"WHERE (mt.LastName)= '" & Me.cboFilter.Value & "'"
Me.RecordSource = strSource
Me.Requery
Obviously you'll need to change the table and field names as necessary, but hopefully you get the idea.
Option Compare Database
Option Explicit '<- always include this!!!!!
Private Sub txtFilter_AfterUpdate()
Dim strFilter As String
' only set Filter when text box contains something
' to search for ==> don't filter Null, empty string,
' or spaces
If Len(Trim(Me.txtFilter.Value & vbNullString)) > 0 Then
strFilter = "LastName Like '*" & _
Replace(Me.txtFilter.Value, "'", "''") & _
"*'"
' that Replace() prevents the procedure from breaking
' when the user enters a name with an apostrophe
' into the text box (O'Malley)
Debug.Print strFilter ' see what we built, Ctrl+g
Me.Filter = strFilter
Me.FilterOn = True
Else
' what should happen here?
' maybe just switch off the filter ...
Me.FilterOn = False
End If
End Sub

Fire update event on keyup

I have textbox ("D_find" in its id) on my form witch i use to filter form's data using LIKE query.
I have following code:
Private Sub D_find_AfterUpdate()
Dim fil
fil = Me.D_find
If fil = Null Then
fil = ""
End If
Me.Filter = "DeloN Like '" + fil + "*'"
End Sub
It's working correctly if i press tab or focus some other control on form, but i need to apply filter immediatly after keyup event of the textbox, but i cant to it, because if i use this code in D_find_keyup i always have D_find is NULL, but in current scenario it's always is not null except if it's empty.
The second trouble present in current scenario: after AfterUpdate fired and filter is applied, text color in D_find textbox is going to be white and it rollbacks to black after i type somethin in this textbox (D_find) or cut some text.
--
I'm sorry for my bad English.
If the textbox still has focus, you can use:
NameOfControl.Text
This will contain the text just entered. However, why must you apply the filter immediately, it would be safer in Afterupdate, because you would be sure that all data was entered, and you can set focus back to the search control.
EDIT re Comments
Using a textbox, txtSearch, and a listbox, lstCompanies the following code illustrates the above:
Private Sub txtSearch_Change()
s = "SELECT Key, Company " _
& "FROM tblCompanies " _
& "WHERE Company LIKE '*" _
& Replace(Me.txtSearch.Text, "'", "''") & "*'"
Me.lstCompanies.RowSource = s
End Sub
The listbox returns a gradually diminishing list as letters are added to txtSearch.