A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » New Users
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Searching Records Using a (Global?) Module



 
 
Thread Tools Display Modes
  #1  
Old June 15th, 2004, 09:52 PM
Lucky Man Cree
external usenet poster
 
Posts: n/a
Default Searching Records Using a (Global?) Module

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




 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 05:53 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.