Searching Records Using a (Global?) Module

L

Lucky Man Cree

I have designed a database with three tables. The corresponding forms each
have a search function. The search function works as follows:

I click on the field which I want to search in.
I enter the criteria in an unbound box.
I click on one of 4 buttons: Find First, Find Last, Find Next, Find
Previous.

Because each form has the same search function, I want to take the code out
of the individual form modules and put it in a (global?) module utilizing
variables for "form name". I want the buttons on the forms to send strings
"strFormName" and "strFindDirection" to module "modFinding" and have
"modFinding" do the work.

I think that I have finally got the
"forms(strFormName).txtSelectCriteria.Value" syntax right, as I no longer
get errors when I click the Find buttons. But now, unfortunately, nothing
happens.

This code worked like a charm when it was in the form's own module. Can
anyone tell me why this doesn't work in the (global?) module? What is
involved in doing this? Is what I'm trying to do even possible? The code
follows this message.

Thanks in advance!
Shannon


----------------------------------------------------------------------
EXAMPLE OF CODE ON THE FORM:

Option Compare Database
Option Explicit

'Used for the Find feature - first, last, previous, next...
Public strFindDirection As String
Public strFormName As String

----------------------------------------------------------------------

Private Sub fldName_Enter()
strSelectField = "fldName"
End Sub
----------------------------------------------------------------------

Private Sub btnFindLast_Click()
strFindDirection = "FindLast"
strFormName = "frmAddressBook1"
Call Finding
End Sub

----------------------------------------------------------------------
CODE IN THE MODULE:

Option Compare Database

Public Sub Finding()
On Error GoTo Err_Finding

Dim strCriteria As String


DoCmd.RunCommand acCmdRefresh ' MAYBE THIS IS A PROBLEM?


'Determine the Criteria.
strCriteria = "[" & strSelectField & "] like " & Chr(34) & "*" &
Forms(strFormName).txtSelectCriteria.Value & "*" & Chr(34)

'Set the Properties.
If strFindDirection = "FindFirst" Then
Forms(strFormName).RecordsetClone.FindFirst strCriteria
Forms(strFormName).Bookmark =
Forms(strFormName).RecordsetClone.Bookmark

ElseIf strFindDirection = "FindPrevious" Then
Forms(strFormName).RecordsetClone.FindPrevious strCriteria
Forms(strFormName).Bookmark =
Forms(strFormName).RecordsetClone.Bookmark

ElseIf strFindDirection = "FindNext" Then
Forms(strFormName).RecordsetClone.FindNext strCriteria
Forms(strFormName).Bookmark =
Forms(strFormName).RecordsetClone.Bookmark

ElseIf strFindDirection = "FindLast" Then
Forms(strFormName).RecordsetClone.FindLast strCriteria
Forms(strFormName).Bookmark =
Forms(strFormName).RecordsetClone.Bookmark

End If


'If there is no match (or if no more matches are found):
'Play a sound and
'Select corresponding button to return to the first or last record. (cannot
be combined with other If statement below)

If Forms(strFormName).RecordsetClone.NoMatch Then
'Audio Cue
Dim wFlags As Long
wFlags = &H1 Or &H2 'Plays Sound Once.
Call sndPlaySound("C:\Windows\MEDIA\Chimes.wav", wFlags)

'Visual Cue
MsgBox "No Further Matches"


If strFindDirection = "FindPrevious" Then
Forms(strFormName).btnFindLast.SetFocus

ElseIf strFindDirection = "FindNext" Then
Forms(strFormName).btnFindFirst.SetFocus

End If
End If

'Select corresponding button to continue searching. (cannot be combined with
If statement above)
If strFindDirection = "FindFirst" Then
Forms(strFormName).btnFindNext.SetFocus

ElseIf strFindDirection = "FindLast" Then
Forms(strFormName).btnFindPrevious.SetFocus

End If

Exit_Finding:
Exit Sub

Err_Finding:
MsgBox Err.Description
Resume Exit_Finding

End Sub
 

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