Full Version: Search Setup
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
ginmarie
At present I have a form with a list from which I can choose a contact. Along with this I have several buttons which accomplish other tasks based on the list's selection. This is working very well, but in thinking about my end UI, I'd like to give the user an option to type in a last name in a search box which would open a form with all the button on it. So type in the contact last name, on enter or after update, up pops the options (form) for choosing to edit, email, etc. Ok, now that I have the first method down, how do I create the second? Best and simplest way possible, though now that I'm gaining confidence with VBA, having code to copy and tweak, or even write if I have step by step directions does not scare me. smile.gif


GroverParkGeorge
I'd use a combo box, not a free form text box, for that task. First, it does the matching as you type, so you get updated results as you go. Second, it displays ALL valid names, and ONLY valid names, avoiding types, etc.

ginmarie
I thought about that, but wouldn't that be the same as the listbox, as in asking the user to scroll through a list to find the contact, or am I missing something? I realize the inherent risk in having the user type in a name, but I think having this as an option along side the listbox would give a speedier option.

GroverParkGeorge
No, there is a difference between list and combo boxes that may not be so obvious. The combo box has an "Auto Expand" property, which CONTINUES to match as you type addtional characters, whereas the list box matches ONLY on the initial character.
Click to view attachment

However, if you want to be able to type characters that may appear ANYWHERE in the string ("I think her name has an "anna" somewhere in it..."), you can use a freeform text box to do that.

This is one that I have as a sample in a demo I'm trying to get ready to publish.
Click to view attachment
CODE
Private Sub cmdSearch_Click()
Dim strSQL As String
On Error GoTo errHandler

strSQL = cboSelectAPerson.RowSource
strSQL = Left(strSQL, InStr(strSQL, "WHERE") - 2) & _
    " Where SearchName Like  '*"  & Me.txtSearch &  "*'"
Me.cboSelectAPerson.RowSource = strSQL
Me.cboSelectAPerson.SetFocus

Cleanup:
    On Error Resume Next
exitProc:
    On Error GoTo 0 'Disables any enabled error handler in the current procedure.
    Exit Sub

errHandler:
    Call GlobalErrorMessage(iNum:=Err, iLn:=Erl, sfrm:="cboSelectAPerson_AfterUpdate of VBA Document Form_frmPerson_2010")

    Resume Cleanup
    Resume

End Sub


Note that the criteria in the SQL for the combo box is Where SearchName Like '*" & Me.txtSearch & "*'"
This allows a match on any portion of the name. The combo box repopulates with the filtered list of potentially matching names.

HTH
ginmarie
Ok, I have no problem setting up the combobox for this, but I'm confused about where to put the code.
GroverParkGeorge
In the example I posted, it's behind the button to which the green arrow points.
ginmarie
Ok, I made a button, put the code in it, changing it to reference my combo box where needed. I get this error now - compile error, sub or function not defined. This part of the code is highlighted in blue Call Global Error Message

GroverParkGeorge
Oh, darn. I meant to edit that out before posting. blush.gif So sorry. Here's a generic version.



CODE
Private Sub cmdSearch_Click()
Dim strSQL As String
On Error GoTo errHandler

strSQL = cboSelectAPerson.RowSource
strSQL = Left(strSQL, InStr(strSQL, "WHERE") - 2) & _
    " Where SearchName Like  '*"  & Me.txtSearch &  "*'"
Me.cboSelectAPerson.RowSource = strSQL
Me.cboSelectAPerson.SetFocus

Cleanup:
    On Error Resume Next
exitProc:
    On Error GoTo 0 'Disables any enabled error handler in the current procedure.
    Exit Sub

errHandler:
     MsgBox Err & ": " & Err.Description, vbOkOnly
     Resume Cleanup
     Resume

End Sub
ginmarie
Sigh...I really appreciate the help, but I just can't seem to get this to work. This is what I've done so far step by step

*made a textbox called txtsearch, unbound
*made a combo called cbosrch, unbound, and with the settings like your example
*made a button with the copied and adjusted code behind it:

Private Sub Command426_Click()
Dim strSQL As String
On Error GoTo errHandler

strSQL = cbosrch.RowSource
strSQL = Left(strSQL, InStr(strSQL, "WHERE") - 2) & _
" Where SearchName Like '*" & Me.txtsearch & "*'"
Me.cbosrch.RowSource = strSQL
Me.cbosrch.SetFocus

Cleanup:
On Error Resume Next
exitProc:
On Error GoTo 0 'Disables any enabled error handler in the current procedure.
Exit Sub

errHandler:
MsgBox Err & ": " & Err.Description, vbOKOnly
Resume Cleanup
Resume

End Sub

I keep getting this error message: 5: Invalid proceedure call or argument

One thing I am unsure of in your example is where to put this: " Where SearchName Like '*" & Me.txtSearch & "*'"
Does it go in the select query for the combo box, and in which field?





GroverParkGeorge
The combo box must START OUT with a valid SQL String. Could that be the problem?
ginmarie
I don't know - here is the string:
SELECT Search.AbleID, Search.FirstName, Search.LastName, [FirstName] & " " & [LastName] AS Name FROM Search;

I've tried both using the box wiz and doing it manually, both results the same

Whoops, I just fixed this: it's:

SELECT Search.AbleID, [FirstName] & " " & [LastName] AS FirstLast, [LastName] & " " & [FirstName] AS LastFirst FROM Search;

GroverParkGeorge
I'm glad to hear you fixed it. COntinued success with your project.
ginmarie
Oop's sorry - I didn't mean I fixed the problem, just the string.
ginmarie
Thanks again for your help. I just had an Occam's Razor moment. Found a very simple solution that had been staring at me all along. I think having just the combobox with the auto expand turned on will do the trick after all. smile.gif
GroverParkGeorge
"Auto Expand"? As I suggested back in post #4, with a screenshot?
ginmarie
Yeah - sometimes the simplest solution really IS the right one. LOL

One thing I'm wondering and hate to start a whole new thread to ask because it might not be a big deal either in terms of solving or even if I really need to do this.
When opening the form, my combo box's text field is blank which is perfect, but once I enter a name, it stays there until I either choose another name or close the form. I'd like to have it return to blank after I use either command button, so it's visibly ready for another entry if that makes sense.
Please excuse if I should start another thread with this question.

GroverParkGeorge
You can add a line of code to the Command button to set the value of the combo box to null, blanking it out.
ginmarie
Awesome!! That works beautifully!!! Thank you, thank you. smile.gif
GroverParkGeorge
Continued success with your project.

George
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.