Finding records using one form.

G

Guest

Hi,
Really need help, any suggestions on the following:
I am using a form to display data already stored in a table. I have text
boxes which use the field names of the table as the control source. Then i
have another unbound text box which i have named the same as the table field
for which i want to get the info from.
For example, if i want to search for 'order 123' i type '123' into my
unbound text box and click my 'find' button. The other text boxes on the
form then display the data from the other fields in the table associated with
'order 123'.
(hope im making sence here)!!

Now what i want to do is to replicat this operation on the same form so that
i can type in '123' in one unbound text box and eg '567' in another text box
and the relivant record information is displayed in the text boxes which are
controlled by the table field names.
I hope this makes sence, please help!!
Thanks in advance!
 
F

Fred Boer

Hi Nick:

What you describe in the first section seems reasonable, but it is unclear
what you are asking for in the second section of your post. Are you wanting
to implement "cascading" text boxes? I.e. Enter data in one unbound text box
which would filter the data behind the form, and then, by entering data in a
second unbound text box do a search using the filtered recordset?

If so, perhaps this will help:

http://www.mvps.org/access/forms/frm0028.htm
http://rogersaccesslibrary.com/download3.asp?SampleName=CascadingComboBoxes.mdb

If that isn't it, please post back with more details about what you are
trying to accomplish!

Cheers!
Fred Boer
 
G

Guest

Firstly don't name an unbound control with the name of a column (field) in
the form's underlying table; call it something like txtFindOrderNumber.

I'm not clear whether you want to search for multiple order numbers or on
the basis of separate columns in the table. If the latter do you want to
return rows which match both values or whre either values match? So I'll
deal with al three scenarios:

1. To find multiple order numbers I'd suggest using a multi-select list
box; you can then select as few or as many as you wish. Set its MultiSelect
property to either Simple or Extended; the former enables multiple selections
by simply clicking each in turn, the latter allows the use of Ctrl+Click or
Shift+Click in the usual way to select ranges. Set its RowSource so it lists
all the values from the column in question, sorting them either numerically
or alphabetically, e.g.

SELECT [OrderNum] FROM [Orders] ORDER BY [OrderNum];

To execute the search use your Find button, with code in its Click event
procedure which iterates through the selected items in the list box and sets
the forms Filter property, e.g.

Dim varItem As Variant
Dim strOrderNumbersList As String
Dim ctrl As Control

Set ctrl = Me.lstFindOrderNumber

If ctrl.ItemsSelected.Count > 0 Then
' build value list of selected items
For Each varItem In ctrl.ItemsSelected
strOrderNumbersList = stOrderNumbersList & "," &
ctrl.ItemData(varItem)
Next varItem

' remove leading comma from value list
strOrderNumbersList = Mid(strOrderNumbersList, 2)
' filter form
MeFilter = "[OrderNum] In(" & strOrderNumbersList & ")"
Me.FilterOn = True
Else
MsgBox "No orders selected.", vbExcalamation, "Invalid Operation"
End If

Where lstFindOrderNumber is the name of the list box. The above assumes
that the order numbers are a number data type, not text. With a text data
type you'd amend it slightly:

strOrderNumbersList = stOrderNumbersList & ",""" &
ctrl.ItemData(varItem) & """"

To clear the selections and show all records you can add another button to
the form with the following in its Click event procedure:

Dim n As Integer

' clear all selections from list box
For n = 0 To Me.lstFindOrderNumber .ListCount - 1
Me.lstStkNumbers.Selected(n) = False
Next n
' turn filter off
Me.FilterOn = False

2. To search on different columns in the table lets assume you want to
search by OrderNum and by Product and return rows which match *both*, the
first being a number data type the second text, and you want each of these to
be optional, i.e. the user can search on either or both of them. Say the
text boxes are called txtFindOrderNumber and txtFindProduct, the button's
code would go like this:

Dim strFilter As String

' has user entered an order number?
If Not IsNull(Me.txtFindOrderNumber) Then
strFilter = strFilter & " And [OrderNum] = " & MetxtFindOrderNumber
End If

' has user entered a product?
If Not IsNull(Me.txtFindProduct) Then
strFilter = strFilter & " And [Product] = """ & MetxtFindProduct &
""""
End If

' if neither text box has value do nothing
If Len(strFilter) > 0 Then
' remove leading ' And '
strFilter = Mid(strFilter,6)
' filter form
Me.Filter = strFilter
Me.FilterOn = True
End If

3. To search on different columns and return rows which match *either* the
button's code would go like this:

Dim strFilter As String

' has user entered an order number?
If Not IsNull(Me.txtFindOrderNumber) Then
strFilter = strFilter & " Or [OrderNum] = " & MetxtFindOrderNumber
End If

' has user entered a product?
If Not IsNull(Me.txtFindProduct) Then
strFilter = strFilter & " Or [Product] = """ & MetxtFindProduct &
""""
End If

' if neither text box has value do nothing
If Len(strFilter) > 0 Then
' remove leading ' Or '
strFilter = Mid(strFilter,5)
' filter form
Me.Filter = strFilter
Me.FilterOn = True
End If

As you'll see 2 is a Boolean AND operation, 3 is a Boolean OR operation.

For 2 and 3 to show all record s you can add another button with the
following in its Click event procedure:

' turn off filter
Me.FilterOn = False

Ken Sheridan
Stafford, England
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top