filter continuous form using textbox - ms-access

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

Related

Using textboxes to search entries based on a keyword (There are multiple textboxes for separate entities) and implementing a combo box

I want to be able to enter in a keyword into a textbox that would search records for that keyword and present reports based on that word that are found.
Please Note: There are multiple textboxes for this to separate specific information so that I need this to work for each individual textbox. There is one text box that will search for entries based on the date (month/day/year) and I have been having issues with it not displaying certain date ranges.
I am also looking to add in a combo box as well.
I have tried code that another individual has given me, but the issue that I have run into with the normal textboxes is that it requires me to put in everything that is in the entry as opposed to search based on a keyword to which nothing pops up when other criteria have been put in place (I know that there is an entry that fits, but for some reason when I add in the textbox element it does not work unless I put in the exact entry that is in the textbox that I am search the specific information from).
I have been trying to find information for combo boxes, but I have not found anything that really works with what I want to do.
Function SelectedItems(objBox As ListBox) As String
Dim strRtn As String, varItm
For Each varItm In objBox.ItemsSelected
strRtn = strRtn & ",'" & objBox.ItemData(varItm) & "'"
Next varItm
If strRtn <> vbNullString Then SelectedItems = Mid(strRtn, 2)
End Function
Private Sub Command62_Click()
Dim strSQL As String
Dim strArr As String
Dim varItm
For Each varItm In Array("District", "Circumstance", "Location", "Method", "Point", "Rank", "Description", "Missing", "IDNumber", "Dateto", "Datefrom", "Address")
strArr = vbNullString
Select Case Me.Controls(varItm).ControlType
Case acListBox
strArr = SelectedItems(Me.Controls(varItm))
Case acTextBox
If Not IsNull(Me.Controls(varItm).Value) Then
strArr = "'" & Me.Controls(varItm).Value & "'"
End If
End Select
If strArr <> vbNullString Then
strSQL = strSQL & "t." & varItm & " in (" & strArr & ") and "
End If
Next varItm
If strSQL <> vbNullString Then strSQL = "where " & Left(strSQL, Len(strSQL) - 5)
With CurrentDb.QueryDefs("qryMultiselect")
.SQL = "select * from tblDataEntry t " & strSQL
End With
DoCmd.OpenQuery "qryMultiselect"
End Sub
What I want is to be able to use my multi-select listboxes in conjunction with my textboxes as well as my combo box that I have as well. Using all of that or at least a combination of them, I want to be able to sort through my data to be able to create reports based on what is being looked for. Along with making sure that the user has the freedom to not having to enter in information for all fields and only the ones that they feel they need to enter in information on.
With the textboxes: I want to be able to search based on a keywords that corresponds with a specific section of the data that I am looking for and to be able to leave others blank and be able to pull up results.
The same goes for the date/time textboxes and my combo box.

Using OpenArgs to pass multiple textboxs into a different form

I am trying to pass 3 textboxes into a different form via parsing the string. I am getting a run time error 13.
Private Sub txtFullName_Click()
Const cstrForm As String = "frmInputInfo"
DoCmd.OpenForm "frmInputInfo", acFormAdd, , , acDialog, _
Me.txtFullName & "|" & Me.PATS_Job_Opening_ID & "|" & Me.NYCAPS_JobID
End Sub
Private Sub Form_Load()
varSplitString = Split(Me.OpenArgs, "|")
Me.[FullName].Value = varSplitString(0)
Me.[PATS Job Opening ID].Value = varSplitString(1)
Me.[NYCAPS_JobID].Value = varSplitString(2)
End Sub
and them on the form load I typed
Any help will be appreciated
You have to be extremely attentive with all those commas in the DoCmd.OpenForm options list. It's just way too darn easy to cause a misalignment between what you and Access think about which values apply to which options.
In your case you intend to pass a string, Me.txtFullName & "|" & Me.PATS_Job_Opening_ID & "|" & Me.NYCAPS_JobID, to OpenArgs. Unfortunately you omitted a comma, so Access thinks you're feeding it a value for WindowMode, which is supposed to be a number. Therefore, error 13: "type mismatch"!
Do it this way and you eliminate any confusion about which value goes with which option.
Dim strArgs As String
strArgs = Me.txtFullName & "|" & Me.PATS_Job_Opening_ID & "|" & Me.NYCAPS_JobID
Debug.Print strArgs ' make sure you got what you expect '
DoCmd.OpenForm FormName:="frmInputInfo", _
DataMode:=acFormAdd, _
WindowMode:=acDialog, _
OpenArgs:=strArgs
Also in the form event, make sure you got something for OpenArgs before you attempt to Split it. As it stands now, if the form is ever opened without supplying OpenArgs, your code will essentially attempt Split(Null, "|") and that will trigger a different error.
You can test before split like this:
If Len(Me.OpenArgs) > 0 Then
' do your split thing here '
End If

Warning for Dynamic Combobox list

I have a combobox that builds it's list upon first usage. I know that the way I want "NotInList" to behave isn't conventional - I don't want to waste adding the item to a table separate from the needed entry, but I'd like to still warn about an item that hasn't been used yet, so that the user has to think twice before accepting the entry.
Once the user adds the item, it will automatically appear in the list next time because the data source for the combo box is as follows:
SELECT tbl_SP.PROGRAM
FROM tbl_SP
GROUP BY tbl_SP.PROGRAM
HAVING (((tbl_SP.PROGRAM) Is Not Null And (tbl_SP.PROGRAM)<>""));
I tried this:
Private Sub cmbPROGRAM_NotInList(NewData As String, Response As Integer)
If MsgBox("'" & Chr(34) & NewData & Chr(34) & " hasn't been used yet. Add to list? ", vbQuestion + vbYesNo, "Add - " & NewData & "?") = vbYes Then
Response = acDataErrAdded
End If
End Sub
but of course, Access wants the item to actually exist before it will release the error. And...if I set LimitToList to "No" then the user doesn't get a warning.
How can I achieve this behavior?
Ok, I tried this which works fine if the user selects YES, but becomes more complicated when the user selects "NO"
Public Function ReturnsRecords(strSQL As String) As Boolean
Dim d As DAO.Database
Dim arr(1 To 3) As DAO.Recordset
'Dim rs As DAO.Recordset
'assume 3 items in array above
Set d = CurrentDb
Set arr(1) = d.OpenRecordset(strSQL)
' MsgBox "Record Count is " & arr(1).RecordCount
If arr(1).RecordCount > 0 Then
ReturnsRecords = True
Else
ReturnsRecords = False
End If
Set d = Nothing
End Function
Private Sub cmbPROGRAM_BeforeUpdate(Cancel As Integer)
Dim strSQL As String
strSQL = "Select * from LU_PROGRAM where PROGRAM ='" & Me.cmbPROGRAM & "'"
If ReturnsRecords(strSQL) = False Then
If MsgBox("'" & Chr(34) & Me.cmbPROGRAM & Chr(34) & " hasn't been used yet. Add to list? ", vbQuestion + vbYesNo, "Add - " & Me.cmbPROGRAM & "?") = vbNo Then
Cancel = True
' how do I reset this? Me.cmbPROGRAM.Text = Null
End If
End If
End Sub
How do I clear the combobox if the user selects NO? If I select me.undo, that will undo all of the entries, but I just want to clear the combobox.
Incidentally, the form is totally unbound and doesn't accept an entry until the user selects "Save"
First, I'm not quite sure what you wish to achieve ...
Then, educate the users to press Escape to cancel. This is mandatory wisdom when operating an Access application.
For your code to work, you can't change the content of a control in the BeforeUpdate event. So try the AfterUpdate event with either:
Me!cmbPROGRAM.Text = ""
or:
Me!cmbPROGRAM.Value = Null

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.

Search form in Access

I want to implement a search form to operate on a table on access.
But when I choose the table then create a From, I get all the table data in the form instead of search field. And when I change any value, the table values change. I want to have a text field where user can enter search criteria and hit a search btn and the search result appears as a table in the form or as a message box.
You could add the data to the form after the search, but to keep things tidy, you might like to consider an unbound form with search box(es) and a subform for results. Let us say you have two boxes, txtName and txtDate and a search button, then a very rough idea would run:
strSQL = "SELECT aName, aDate FROM aTable WHERE 1=1 "
If Not IsNull(txtName) Then
strWHERE = " AND aName Like '*" & Replace(txtName,"'","''") & "*'"
End If
If Not IsNull(txtDate) Then
strWHERE = strWhere " AND aDate =#" & Format(txtdate,"yyyy/mm/dd") & "#"
End If
Me.SubformControlName.Form.RecordSource = strSQL & strWhere
You should, of course, make sure that the data in the controls in sensible and clean and that records are returned.
You can use something like this to put your form in Search mode when opened:
Private Sub Form_Open(Cancel As Integer)
'DoCmd.DoMenuItem acFormBar, acRecordsMenu, acFilterByForm, , acMenuVer70
DoCmd.RunCommand acCmdFilterByForm
End Sub
I have a search form created that I use that is very handy. In order to setup a form to search you need to create a search query. For example my form has the fields: Keywords and Source. So you need to link the query to the table that houses the data. The search query I have is
SELECT KWTable.KW AS Expr1, KWTable.Code, KWTable.Source
FROM KWTable
WHERE (((KWTable.KW) Like "*" & [Forms]![Search_Form]![KW_Text] & "*") AND ((KWTable.Source) Like "*" & [Forms]![Search_Form]![Source_Text] & "*"));
I type the words into the boxes and click a button to execute. The button code looks like
Private Sub Command8_Click()
On Error GoTo Err_Command8_Click
Dim stDocName As String
stDocName = "Search_Query"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Exit_Command8_Click:
Exit Sub
Err_Command8_Click:
MsgBox Err.Description
Resume Exit_Command8_Click
End Sub
I hope this helps.