FIND RECORD IN ACCESS - Please Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi hope someone can help me.

I have a form, on the form i want a create a box, when i click on the box a
find Dialog box appears just like the one when you click Edit and select
FIND. Every time i want to search for a name in all records i have to
deselect Search fields as formatted and click on for it to search all
records. I know all this, but a new user would not.

Can you please explain this in detail and in a simple mannor, i.e codes in
Visual Basic, etc.

This is the third time i have put thread in without success! So its a box
that appears and you type a text, number and it will find in all fields for
that. So if i type Ricky it will find Ricky in every Record..

Thanks, i wait in anticipation.


Ricky Samras
 
Samrasr said:
Hi hope someone can help me.

I have a form, on the form i want a create a box, when i click on the
box a find Dialog box appears just like the one when you click Edit
and select FIND. Every time i want to search for a name in all
records i have to deselect Search fields as formatted and click on
for it to search all records. I know all this, but a new user would
not.

Can you please explain this in detail and in a simple mannor, i.e
codes in Visual Basic, etc.

This is the third time i have put thread in without success! So its
a box that appears and you type a text, number and it will find in
all fields for that. So if i type Ricky it will find Ricky in every
Record..

Thanks, i wait in anticipation.


Ricky Samras

Do you mean that whatever is entered to search for should be found in
any field where it may appear, not just in the field that had the focus
before "find" button was clicked?

Do you want to match (a) the entire field, or (b) the start of the
field, or (c) any part of the field?
 
Samrasr said:
Dirk,

Any part of the fieid, it does not matter where. (C)

And my first question:

?

I think I can give you something that will work, if you can answer that
question, too.
 
Yes that is correct.

So if i click a button the search dialog comes and like i said it will
search anything in
any field where it may appear, not just in the field that had the
focus before "find" button was clicked.

Please help, with simple detail.

Ricky
 
Samrasr said:
Yes that is correct.

So if i click a button the search dialog comes and like i said it will
search anything in
any field where it may appear, not just in the field that had the
focus before "find" button was clicked.

Please help, with simple detail.

Okay, here's what I have done to make something that seems to do what
you're asking. The idea is to have a simple popup search form that
operates very similarly to the way the built-in Find dialog would work
if the proper options were selected, but without displaying anything
more than the bare minimum.

Create a new form in design view. Set the following properties for the
form itself:

Format tab
-----------
Default View: Single Form
Scroll Bars: Neither
Record Selectors: No
Navigation Buttons: No
Dividing Lines: No
Border Style: Dialog

Other tab
-----------
Pop Up: Yes

Turn off the control wizards and add the following controls:

(1) a text box with its associated label. Name the text box
"txtFindWhat". Set the caption of the label to something like "Enter
what you want to find:".

(2) a command button. Name the button "cmdFind" and set its caption
to "Find".

(3) optionally, a label to go over or beside the button, with a
caption along the lines of "Then click this button:".

Adjust the sizes, shapes, and positions of these controls to suit you.

Save the form now (without closing it), giving it the name
"frmFindRecord".

Click the "Code" button on the toolbar, or else click menu items View ->
Code. That will put you in the VB Editor looking at the newly created
code module for this form. Copy the code below (between the "'-----
start of code -----" and "'----- end of code -----" lines) and paste it
over whatever may currently be displayed in the forms's module:

'----- start of code -----
Option Compare Database
Option Explicit

Dim mstrFormToSearch As String

Private Sub cmdFind_Click()

Static strLastFind As String
Dim ctlFocus As Access.Control
Dim strTemp As String
Dim I As Integer

If IsNull(Me.txtFindWhat) Then Exit Sub

With Forms(mstrFormToSearch)

.SetFocus

Set ctlFocus = .ActiveControl

On Error Resume Next
strTemp = ctlFocus.ControlSource
If Err.Number <> 0 Then
For I = 0 To .Controls.Count - 1
Set ctlFocus = .Controls(I)
If ctlFocus.Enabled = True Then
Err.Clear
strTemp = ctlFocus.ControlSource
If Err.Number = 0 Then
Exit For
End If
End If
Next I
End If
ctlFocus.SetFocus
On Error GoTo 0

End With

If Me.txtFindWhat = strLastFind Then
DoCmd.FindNext
Else

DoCmd.FindRecord _
Me.txtFindWhat.Value, _
acAnywhere, _
False, _
acSearchAll, _
False, _
acAll, _
True

strLastFind = Me.txtFindWhat

End If

End Sub

Private Sub Form_Open(Cancel As Integer)

On Error Resume Next
mstrFormToSearch = Screen.ActiveForm.Name

If Len(mstrFormToSearch) = 0 Then
MsgBox "There's no active form to search!"
DoCmd.Close acForm, Me.Name, acSaveNo
End If

End Sub
'----- end of code -----

Click menu items Debug -> Compile (your project). If any compile errors
were found, you'll hjave to fix them and recompile. When you get a
clean compile, click the Save button on the toolbar.

Switch back to the database application window. Save and close
frmFindRecord.

Now open the form you want to to use this search function on. Put a
command button on the form, name it "cmdSearch" (for example), and
create this event procedure for its Click event:

'----- start of search-button code -----
Private Sub cmdSearch_Click()

DoCmd.OpenForm "frmFindRecord"

End Sub
'----- end of search-button code -----

Compile and save that form.

That ought to do it. Clicking the search button should open your find
dialog form, on which you can enter what you want to search for and
click the Find button to find the first record and field that contains
it. If you then click the Find button again -- without modifying the
search text -- it should find the next occurrence of that text, and so
on.

Please bear in mind that this is fairly rough, hasn't been tested in a
wide variety of circumstances, and has no error-handling. Any polish is
up to you. Let me know how it works out.
 
Hi,

It does not work: a messege appears which says Methood or data member not
found. And it points to [Me.txtFindWhat]

Ricky Samras
 
Samrasr said:
Hi,

It does not work: a messege appears which says Methood or data
member not found. And it points to [Me.txtFindWhat]

Ricky Samras

Dirk Goldgar said:
Okay, here's what I have done to make something that seems to do what
you're asking. The idea is to have a simple popup search form that
operates very similarly to the way the built-in Find dialog would
work if the proper options were selected, but without displaying
anything more than the bare minimum.

Create a new form in design view. Set the following properties for
the form itself:

Format tab
-----------
Default View: Single Form
Scroll Bars: Neither
Record Selectors: No
Navigation Buttons: No
Dividing Lines: No
Border Style: Dialog

Other tab
-----------
Pop Up: Yes

Turn off the control wizards and add the following controls:

(1) a text box with its associated label. Name the text box
"txtFindWhat". Set the caption of the label to something like "Enter
what you want to find:".

(2) a command button. Name the button "cmdFind" and set its
caption to "Find".

(3) optionally, a label to go over or beside the button, with a
caption along the lines of "Then click this button:".

Adjust the sizes, shapes, and positions of these controls to suit
you.

Save the form now (without closing it), giving it the name
"frmFindRecord".

Click the "Code" button on the toolbar, or else click menu items
View -> Code. That will put you in the VB Editor looking at the
newly created code module for this form. Copy the code below
(between the "'----- start of code -----" and "'----- end of code
-----" lines) and paste it over whatever may currently be displayed
in the forms's module:

'----- start of code -----
Option Compare Database
Option Explicit

Dim mstrFormToSearch As String

Private Sub cmdFind_Click()

Static strLastFind As String
Dim ctlFocus As Access.Control
Dim strTemp As String
Dim I As Integer

If IsNull(Me.txtFindWhat) Then Exit Sub

With Forms(mstrFormToSearch)

.SetFocus

Set ctlFocus = .ActiveControl

On Error Resume Next
strTemp = ctlFocus.ControlSource
If Err.Number <> 0 Then
For I = 0 To .Controls.Count - 1
Set ctlFocus = .Controls(I)
If ctlFocus.Enabled = True Then
Err.Clear
strTemp = ctlFocus.ControlSource
If Err.Number = 0 Then
Exit For
End If
End If
Next I
End If
ctlFocus.SetFocus
On Error GoTo 0

End With

If Me.txtFindWhat = strLastFind Then
DoCmd.FindNext
Else

DoCmd.FindRecord _
Me.txtFindWhat.Value, _
acAnywhere, _
False, _
acSearchAll, _
False, _
acAll, _
True

strLastFind = Me.txtFindWhat

End If

End Sub

Private Sub Form_Open(Cancel As Integer)

On Error Resume Next
mstrFormToSearch = Screen.ActiveForm.Name

If Len(mstrFormToSearch) = 0 Then
MsgBox "There's no active form to search!"
DoCmd.Close acForm, Me.Name, acSaveNo
End If

End Sub
'----- end of code -----

Click menu items Debug -> Compile (your project). If any compile
errors were found, you'll hjave to fix them and recompile. When you
get a clean compile, click the Save button on the toolbar.

Switch back to the database application window. Save and close
frmFindRecord.

Now open the form you want to to use this search function on. Put a
command button on the form, name it "cmdSearch" (for example), and
create this event procedure for its Click event:

'----- start of search-button code -----
Private Sub cmdSearch_Click()

DoCmd.OpenForm "frmFindRecord"

End Sub
'----- end of search-button code -----

Compile and save that form.

That ought to do it. Clicking the search button should open your
find dialog form, on which you can enter what you want to search for
and click the Find button to find the first record and field that
contains it. If you then click the Find button again -- without
modifying the search text -- it should find the next occurrence of
that text, and so on.

Please bear in mind that this is fairly rough, hasn't been tested in
a wide variety of circumstances, and has no error-handling. Any
polish is up to you. Let me know how it works out.

You named the text box "txtFindWhat", as I instructed? Check the
control's Name property on its property sheet to make sure. If it's
correctly named, try changing the dot (.) to a bang (!) to see if that
makes a diffence; in other words, change all references to
"Me.txtFindWhat" to "Me!txtFindWhat". The code I posted works for me
using Access 2002, but I've heard some reports of Access 2003 being
pickier about references.

Also, make you posted the code into the module of the correct form.
 
Back
Top