Select from where contains - ms-access

I have a database where i can add a full name of a person, and i am trying to implement a search function using a textBox and a button but i only want to search for the first or last name not necessarily entering the full name.
I tried using SELECT FROM WHERE CONTAINS like this:
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Table WHERE CONTAINS (column, '"+textBox.Text+"')";
But i keep getting this error:
Syntax error (missing operator) in query expression 'CONTAINS (column,'the text i tried to search')'.
I also tried changing the + to % or * or & but still it didn’t work.

Contains is not valid Access SQL. Use Like:
cmd.CommandText = "SELECT * FROM Table WHERE [YourNameField] Like '*" + textBox.Text + "*')";

Here is an example of a search such as you want:
Private Sub cmdFind_DisplayName_Click()
Dim dbs As Database, rstPatient As Recordset
Dim txtDisplayName, strQuote As String
strQuote = Chr$(34)
On Error GoTo ErrorHandler
Me.OrderBy = "DISPLAYNAME"
Me.OrderByOn = True
Set dbs = CurrentDb
Set rstPatient = Me.RecordsetClone
txtDisplayName = Trim(InputBox("Please Enter Patient Name ", "Patient Find By Name"))
txtDisplayName = UCase(txtDisplayName) & "*"
If IsNull(txtDisplayName) Then
MsgBox ("No Patient Name Entered - Please Enter a Valid Patient Name")
Else
rstPatient.FindFirst "[DISPLAYNAME] Like " & strQuote & txtDisplayName & strQuote
If Not (rstPatient.NoMatch) Then
Me.Bookmark = rstPatient.Bookmark
Me.Refresh
Else
MsgBox ("Patient Not Found - Please Enter a New Patient Name")
End If
End If
GoTo Exit_cmdFind_Click
ErrorHandler:
MsgBox LTrim(RTrim(Me.NAME)) + "." + "Patient Find By Display Name - " + "Error: " + AccessError(Err.Number)
Exit_cmdFind_Click:
rstPatient.Close
Set dbs = Nothing
Set rstPatient = Nothing
End Sub

Create 1 textbox (txtMain) and search command button(btnSearch) to execute SQL. Then add a listbox (listResult) to display results.
Private Sub btnSearch_Click()
Dim mainSQL As String
mainSQL = " SELECT YOUR_FIELD_NAME " & _
" FROM MasterReg " & _
" WHERE Left(,InStr(YOUR_FULL_NAME_FIELD,' ')-1) LIKE '" & me.txtMain & "*'" & _ ' Firstname Search
" OR RIGHT( YOUR_FULL_NAME_FIELD,Len( YOUR_FULL_NAME_FIELD )-InStr( YOUR_FULL_NAME_FIELD,' ')) LIKE '" & me.txtMain & "*'" 'Surname Search
Me.listResult.SetFocus
Me.listResult.RowSource = mainSQL
Me.listResult.Requery
End Sub

Related

Creating a form to search for records based on multiple criteria

I am trying to create a form that allows you to return results based on multiple criteria.
I have FirstName field, LastName field, and State Field.
I also have an text boxes named searchFirst, searchLast, searchState where users can input criteria.
The search button will execute the following code once clicked.
Private Sub mySearchQuery_Click()
Dim filter As String
Dim rtFirstName As String
Dim rtLastName As String
Dim rtState As String
rtFirstName = Me.searchFirst.Value
rtLastName = Me.searchLast.Value
rtState = Me.searchState.Value
If Not IsNull(rtFirstName) Then
If Not IsNull(filter) Then filter = filter & " AND "
filter = filter & "(FirstName like""*" & rtFirstName & "*"")"
End If
If Not IsNull(rtLastName) Then
If Not IsNull(filter) Then filter = filter & " AND "
filter = filter & "(LastName like""*" & rtLastName & "*"")"
End If
If Not IsNull(rtState) Then
If Not IsNull(filter) Then filter = filter & " AND "
filter = filter & "(State LIKE""*" & rtState & "*"")"
End If
' Now re-construct the SQL query '
Dim sql As String
sql = "SELECT * FROM MainData"
If Not IsNull(filter) Then
sql = sql & " WHERE " & filter
End If
Me.RecordSource = sql
'SubForm.Form.RecordSource = sql
End Sub
I am getting the following error below.
Run-time error '3075': Syntax error (missing operator) in query
expression 'AND (FirstName like"*tracy*") AND (lastName like"*Smith*")
AND (State LIKE"*ga*")'.
I am not sure why AND was included at the beginning of the search query?
I am not sure why AND was included at the beginning of the search
query?
Since you have Dim filter As String, filter can never contain Null. That means these If conditions ... If Not IsNull(filter) ... will always be True.
Similarly, Not IsNull(rtFirstName), Not IsNull(rtLastName), and Not IsNull(rtState) will always be True.
The net result is the code adds another condition piece to your filter string regardless of whether or not the corresponding search text box contains anything, and each of those pieces is prefixed with " AND ".
With those points in mind, you could refactor your code to add a filter segment only when you have something in the corresponding search text box and decide when to include " AND ". However I find it simpler to include " AND " for each of them and then strip away the very first " AND " from filter before adding it to the WHERE clause.
Private Sub mySearchQuery_Click()
Dim strSelect As String
Dim strWhere As String
If Len(Trim(Me!searchFirst.Value) & vbNullString) > 0 Then
strWhere = strWhere & " AND FirstName Like ""*" & Me!searchFirst.Value & "*"""
End If
If Len(Trim(Me!searchLast.Value) & vbNullString) > 0 Then
strWhere = strWhere & " AND LastName Like ""*" & Me!searchLast.Value & "*"""
End If
If Len(Trim(Me!searchState.Value) & vbNullString) > 0 Then
strWhere = strWhere & " AND State Like ""*" & Me!searchState.Value & "*"""
End If
' Now re-construct the SQL query
strSelect = "SELECT * FROM MainData"
' only add WHERE clause if we have something in strWhere
If Len(strWhere) > 0 Then
' use Mid() to ignore leading " AND "
strSelect = strSelect & " WHERE " & Mid(strWhere, 6)
End If
Debug.Print strSelect ' <- inspect this in Immediate window; Ctrl+g will take you there
' enable one of these RecordSource lines after confirming Debug.Print shows you what you need
'Me.RecordSource = sql
'SubForm.Form.RecordSource = sql
End Sub

Linking a form to multiple list boxes

I have a master form which contains three list boxes and one sub form. I would like to build a routine which allows me to switch links between the sub form and the three list boxes. Is this possible? Or do i have to create three copies of the same sub form and hide two while one the other is activated?
To be practical, my form will work like this: The sub form contains a list of records of people participating in a project, their specific role, and which internal team they come from. I would like to use three list boxes to allow the user to filter this form by either:
(1) All participants coming from a certain team
(2) All participants by roles (titles)
(3) Filter by name of particants
Where I am short is on how to re-link the filter on the sub form so that it changes from list box to list box as the user passes from filter to filter.
Using Krish's suggestion below as a simple test i am trying the following code but am getting a compilation error message on the recordsource line stating that it is impossible to find the method or the data member.. Not sure what that means:
Private Sub lstRoles_AfterUpdate()
Dim SQL_GET As String
SQL_GET = "SELECT * from tblProjectGovernanceResources where ((role like '" & lstRoles.Value & "')"
Me.frmProjectGovernanceResources.RecordSource = SQL_GET
End Sub
you can retrieve the selected value from a listbox simply byt listbox1.value.
As Wayne G pointed. you would add a code in your listbox_after_update event to update your subform's recordsource.
something like:
dim SQL_GET as string
sql_get = "SELECT * from tbl_myTAble where ((condition like '" & listbox1.value & "') OR (condition2 like '"& listbox2.value &"') OR (condition3_number like "& listbox3.value &"))
me.mysubform.recordsource = sql_Get
obviously you need to improve this as per your requirements.
Try this and for a better answer, produce what you have coded so far..
I created some code for the easiest version possible. This means all of your listboxes have the 'multi select' property set to 'None' (this means you can't select multiple items in the list and you can't 'deselect' an item by clicking on it again. I did add some code at the end so you can see how a different multi-select option may work.
My form has three listboxes, a subform, and two buttons. One button will clear all selections in all listboxes. The other button applies the filter to the subform.
Option Compare Database
Option Explicit
'*** NOTE!!! THIS CODE ASSUMES YOU HAVE SET YOUR LISTBOX PROPERTY to 'NONE'.
' IF YOU SET 'MULTI SELECT' To 'SIMPLE' or 'EXTENDED', you MUST use different code to find all selected items.
Dim strWhereTeam As String
Dim strWhereRole As String
Dim strWhereParticipant As String
Private Sub cmdClear_Click()
' Clear all selections in all listboxes
Dim i As Integer
For i = 0 To Me.lstParticipant.ListCount 'Deselect ALL rows in Listbox
lstParticipant.Selected(i) = False
Next i
For i = 0 To Me.lstRole.ListCount 'Deselect ALL rows in Listbox
lstRole.Selected(i) = False
Next i
For i = 0 To Me.lstTeam.ListCount 'Deselect ALL rows in Listbox
lstTeam.Selected(i) = False
Next i
strWhereTeam = ""
strWhereRole = ""
strWhereParticipant = ""
Me.MySubForm.Form.Filter = "" ' Reste filter to NONE
Me.MySubForm.Form.FilterOn = False
End Sub
Private Sub cmdFilter_Click()
'Build Filter (concatenate three selections)
Dim strFilter As String
strFilter = ""
If strWhereTeam & "" <> "" Then
strFilter = strWhereTeam
If strWhereRole & "" <> "" Then
strFilter = strFilter & " AND " & strWhereRole
If strWhereParticipant & "" <> "" Then
strFilter = strFilter & " AND " & strWhereParticipant
End If
Else
If strWhereParticipant & "" <> "" Then
strFilter = strFilter & " AND " & strWhereParticipant
End If
End If
ElseIf strWhereRole & "" <> "" Then
strFilter = strWhereRole
If strWhereParticipant & "" <> "" Then
strFilter = strFilter & " AND " & strWhereParticipant
End If
ElseIf strWhereParticipant & "" <> "" Then
strFilter = strWhereParticipant
End If
If strFilter = "" Then
Me.MySubForm.Form.Filter = ""
Me.MySubForm.Form.FilterOn = False
Else
Me.MySubForm.Form.Filter = strFilter
Me.MySubForm.Form.FilterOn = True
End If
End Sub
Private Sub lstParticipant_Click()
strWhereParticipant = "[Participant] = '" & Me.lstParticipant.ItemData(Me.lstParticipant.ListIndex) & "'"
Debug.Print strWhereParticipant
End Sub
Private Sub lstRole_Click()
strWhereRole = "[Role] = '" & Me.lstRole.ItemData(Me.lstRole.ListIndex) & "'"
Debug.Print strWhereRole
End Sub
Private Sub lstTeam_Click()
If Me.lstTeam.MultiSelect <> 0 Then
MsgBox "You have set the 'Multi Select' property to either Simple or Extended. This code may not work!", vbOKOnly + vbCritical, "ListBox MultiSelect not 'None'"
End If
strWhereTeam = "[Team] = '" & Me.lstTeam.ItemData(Me.lstTeam.ListIndex) & "'"
Debug.Print strWhereTeam
'Simple_Code
End Sub
'' Sample code if set 'Multi Select' to 'Simple' or 'Extended'
'Sub Simple_Code()
' Dim var As Variant
' strWhereTeam = ""
' For Each var In Me.lstTeam.ItemsSelected
' strWhereTeam = strWhereTeam & "[Team] = '" & Me.lstTeam.ItemData(var) & "' OR "
' Next var
' strWhereTeam = "(" & left(strWhereTeam, Len(strWhereTeam) - 4) & ")"
' Debug.Print strWhereTeam
'End Sub
Thanks a lot! This did it all!
Private Sub lstRoles_AfterUpdate()
Dim SQL_GET As String
SQL_GET = "SELECT * from tblProjectGovernanceResources where ([role] = '" & lstRoles.Value & "')"
Me.frmProjectGovernanceResources.Form.RecordSource = SQL_GET
End Sub

Show saved date in label

I´m saving a date in a table like this:
Me!lastchangedate.Caption = Now
Set db = CurrentDb
Set rs = db.OpenRecordset("background", dbOpenTable)
rs.AddNew
rs![date] = Me!lastchangedate.Caption
rs.Update
rs.Close
Later I want to read this date out of the database and show it in a Label:
sqlstrdate = "SELECT date FROM background " _
& " WHERE SAP_ID = '" _
& Me!sapidtxt.Value & "'"
retvaldate = CurrentDb.OpenRecordset(sqlstrdate)
Until here it´s working but if I now try to show "retvaldate" as MsgBox or in a label I always get the error message: Error 13 type mismatch.
Im trying to use this to show the saved date in a label.
Me!lastchangedate.Caption = (retvaldate)
Is there an option to change the label type or do I have to change the "retvaldate" to a date type (which also gives me the same error).
You are trying to set 'retvaldate' like opening a recordset. The following should provide the correct result (BTW, why do you use reserved words like 'date' as a field name?)
Dim rs As Recordset
Dim sqlstrdate As String
sqlstrdate = "SELECT date FROM background " _
& " WHERE SAP_ID = '" _
& Me!sapidtxt.Value & "'"
Set rs = CurrentDb.OpenRecordset(sqlstrdate)
If Not rs.EOF Then
retvaldate = rs.Fields("Date")
Else
retvaldate = "No Records"
End If
rs.Close
Set rs = Nothing
'Then later... but I hope the variable is in scope (Global, form, subroutine)
Me!lastchangedate.Caption = retvaldate

Cascading Combobox

Copy from: https://softwareengineering.stackexchange.com/questions/158330/cascading-comboboxes
ok so i have a form, in Access 2010, with 1 Textbox and 3 ComboBoxes (1 Enabled & 2 Disabled).
the first ComboBox is not tied to the datasource but is subjective to the other 2 comboboxes. So i handled the Click event for the first Combobox to then make the other 2 enabled, and preload the 2nd ComboBox with a custom RowSource SQL Script dynamically built based on the 1st ComboBox Value.
This all works great for New information but when i goto review the information, via Form, its back to the New mode on the controls.
Question:
What event do i need to handle to check if the current Form Data contains data for the Control Source of the Controls?
As i would express it in Logic (its a mix between C & VB, i know but should get the pt acrossed):
DataSet ds = Form.RowSet
if (ds = Null) then
cbo2.enabled = false
cbo3.enabled = false
else
cbo2.rowsource = "select id, nm from table"
cbo2.value = ds(3)
cbo3.value = ds(4)
end if
... do some other logic ...
Updated Logic - Still problem, cant catch for RecordStatus for some reason (gives 3251 Run-Time Error)
Private Sub Form_Current()
Dim boolnm As Boolean: boolnm = (IsNull(txtName.Value) Or IsEmpty(txtName.Value))
Dim booltype As Boolean: booltype = IsNull(cboType.Value)
Dim boolfamily As Boolean: boolfamily = IsNull(cboType.Value)
Dim boolsize As Boolean: boolsize = IsNull(cboType.Value)
Dim rs As DAO.Recordset: Set rs = Me.Recordset
MsgBox rs.AbsolutePosition
' If rs.RecordStatus = dbRecordNew Then
' MsgBox "New Record being inserted, but not committed yet!", vbOKOnly
' Else
' MsgBox rs(0).Name & " - " & rs(0).Value & vbCrLf & _
' rs(1).Name & " - " & rs(1).Value & vbCrLf & _
' rs(2).Name & " - " & rs(2).Value & vbCrLf & _
' rs(3).Name & " - " & rs(3).Value
' End If
'MsgBox "Name: " & CStr(boolnm) & vbCrLf & _
"Type: " & CStr(booltype) & vbCrLf & _
"Family: " & CStr(boolfamily) & vbCrLf & _
"Size: " & CStr(boolsize), vbOKOnly
End Sub
Here is the final result, with Remou's assistance, and this is only a precursor to the end result (which is out of the context of the question).
Private Sub Form_Current()
If Me.NewRecord Then <=======================
cboType.Value = 0
cboType.Enabled = True
cboFamily.Enabled = False
cboSize.Enabled = False
Else
Dim rs As DAO.Recordset: Set rs = Me.Recordset
'get Family ID
Dim fid As String: fid = rs(2).Value
'Build SQL Query to obtain Type ID
Dim sql As String
sql = "select tid from tblFamily where id = " & fid
'Create Recordset
Dim frs As DAO.Recordset
'Load SQL Script and Execute to obtain Type ID
Set frs = CurrentDb.OpenRecordset(sql, dbOpenDynaset, dbReadOnly)
'Set Type ComboBox Value to Type ID
cboType.Value = frs(0)
cboType_Click 'Simulate Click Event since the Value has changed
'Make sure all 3 Comboboxes are enabled and useable
cboType.Enabled = True
End If
End Sub

Reference active table in split form without using actual table name

I'm writing an Access database. I have a number of forms that are identical. These are used to edit look up lists for different fields in my main contacts table.
e.g. there is a company field and a country field. The forms that open for each editable list are identical with repeat vba code in each becasue I cannot work out how to reference the active table from the active form.
The code I currently have for clearing all the yes/no boxes in the table is:
Private Sub cmdClearTicks_Click()
Dim db As Database
' Dim sel As Control
Set db = CurrentDb
' Clear all ticks of selected records
db.Execute "UPDATE ContactCompany " _
& "SET Selected = null "
' Update Selected Field
Me.Requery
End Sub
ContactCompany is the name of the table. I would like to be able to set this sub globally but cannot work out what I should replace ContactCompany with to reference the table in the currently open form. I've already tried Me.RecordSource which does not work.
I'm very grateful for what I assume is a very easy fix!
Sean posted a great fix below. I'm now stumped with including a filter too and defining it globaly.
Sub SelectFiltered(RS As String)
Dim strFilter As String
Dim strSQl As String
If InStr(RS, "FROM") Then
RS = Mid(RS, InStr(RS, "FROM") + 5)
If InStr(RS, " ") Then RS = Left(RS, InStr(RS, " ") - 1)
End If
strFilter = Me.Filter
If Me.FilterOn = False Then
'Select Case MsgBox("No search or filter applied.", vbCritical + vbOKOnly, "Warning")
'End Select
strSQl = "UPDATE " & RS & " " & _
"SET Selected = 1 "
Else
strSQl = "UPDATE " & RS & " " & _
"SET Selected = 1 " & _
"WHERE " & strFilter
End If
DoCmd.SetWarnings False
DoCmd.RunSQL strSQl
DoCmd.SetWarnings True
End Sub
Me.filter doesn't work in the global sub. Sean - I'm sure you'll have an answer for this in a sec. Thanks again!
you are close with using Me.Recordsource
db.Execute "UPDATE " & Me.Recordsource & " SET Selected = null "
or, if you want it as a global function, pass Me.Recordsource to it
Sub ClearTicks(RS as string,StrFilter as string)
Dim db As Database
Set db = CurrentDb
If InStr(RS, "FROM") Then
RS = Mid(RS, InStr(RS, "FROM") + 5)
If InStr(RS, " ") Then RS = Left(RS, InStr(RS, " ") - 1)
End If
' Clear all ticks of selected records
db.Execute "UPDATE " & RS & " SET Selected = null "
If StrFilter="" then 'no filter
Else 'filter
End If
End Sub
and your calling function would be:
Private Sub cmdClearTicks_Click()
ClearTicks Me.Recordsource,Me.Filter
Me.Refresh
End Sub
If you want a more reusable function method that could be expanded more easily, then call the sub/function with Me.Name as the parameter (e.g. MySub Me.Name) and then in your reusable function:
sub MySub(FrmName as string)
Forms(FrmName).Filter
Forms(FrmName).Recordsource
Forms(FrmName).AnyOtherParamater