Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms / May 2008

Tip: Looking for answers? Try searching our database.

Search Feature

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rohn - 08 May 2008 15:50 GMT
I know this is already problematic...... but,
I am trying to tweak an existing DB developed by another user (here, fix
it).  They used a Search Button code from "Graham Thorpe."   Works great for
searching the subform for an existing record and giving the user three
different prompts when the record exists, does not exist or errors.

Where I need help, when the search returns "Record does not exist," and the
focus returns to the search box, the form/subform gets focus on the frist
record of the database when I want it to remain with no focus!  Any advise
on how to only get focus when a record is found?  I can post the code behind
the search button!   Maybe start over with a different type of search? but
being fairly new to Access development I would need help with starting over.

Thanks, Rohn
Klatuu - 08 May 2008 17:17 GMT
Probably not necessary to start over.  Post the code, please.
Signature

Dave Hargis, Microsoft Access MVP

> I know this is already problematic...... but,
> I am trying to tweak an existing DB developed by another user (here, fix
[quoted text clipped - 10 lines]
>
> Thanks, Rohn
Rohn - 08 May 2008 17:37 GMT
Thanks Dave,

Here is the "On Click" event:
'--------------------------------------------------------------
'Seach field concept by Graham Thorpe
'--------------------------------------------------------------
Private Sub cmdSearch_Click()
   Dim Unit_IDRef As String
   Dim strSearch As String

'Check txtSearch for Null value or Nill Entry first.

   If IsNull(Me![txtSearch]) Or (Me![txtSearch]) = "" Then
       MsgBox "Please enter a value!", vbOKOnly, "Invalid Search
Criterion!"
       Me![txtSearch].SetFocus
   Exit Sub
End If
'---------------------------------------------------------------

'Performs the search using value entered into txtSearch
'and evaluates this against values in UNIT_ID field

   DoCmd.ShowAllRecords
   DoCmd.GoToControl ("Unit_ID")
   DoCmd.FindRecord Me!txtSearch

   Unit_ID.SetFocus
   Unit_IDRef = Unit_ID.Text
   txtSearch.SetFocus
   strSearch = txtSearch.Text

'If matching record found sets focus in UNIT_ID and shows msgbox
'and clears search control

   If Unit_IDRef = strSearch Then
       MsgBox "Match Found For: " & strSearch, , "Congratulations!"
       Unit_ID.SetFocus
       txtSearch = ""

'If value not found sets focus back to txtSearch and shows msgbox
       Else
         MsgBox "Match Not Found For: " & strSearch & " - Please Try
Again.", _
           , "Invalid Search Criterion!"
           txtSearch.SetFocus
   End If
End Sub
Klatuu - 08 May 2008 18:19 GMT
All you need to do is remove this line of code:

       Me![txtSearch].SetFocus

I would write it a little differently:

Private Sub cmdSearch_Click()
   Dim Unit_IDRef As String
   Dim strSearch As String

'Check txtSearch for Null value or Nill Entry first.

   If Nz(Me.txtSearch, vbNullstring) = vbNullString Then
       MsgBox "Please enter a value!", vbOKOnly, "Invalid Search
Criterion!"
   Exit Sub
End If

Signature

Dave Hargis, Microsoft Access MVP

> Thanks Dave,
>
[quoted text clipped - 44 lines]
>     End If
> End Sub
Rohn - 08 May 2008 19:08 GMT
Dave,

I tried both approaches without success.  When I removed
"Me![txtSearch].SetFocus"  from the current code,  no noticable difference?
When I replaced the code it gives me a VB Compile Error:  Syntax Error

Did I do something wrong?

more information:
Previously, I had been trying to modify the last section of this Code
because it seem like everything works OK until, you do not find an existing
record and you want to continue to search.  But the form & subform get
populated with the first record in the db while attempting your second
search in the search field.

> All you need to do is remove this line of code:
>
[quoted text clipped - 62 lines]
>>     End If
>> End Sub
Klatuu - 08 May 2008 19:40 GMT
Where in my code did you get a compile error.  It was, of course, air code
written in the message editor, but from practical experience.

I don't understand what you mean about continuing the search.  In most
cases, you either find a record or you don't.  Perhaps if you could describe
what you are trying to accomplish and post all the existing code, we can
offer a how to.
Signature

Dave Hargis, Microsoft Access MVP

> Dave,
>
[quoted text clipped - 77 lines]
> >>     End If
> >> End Sub
Rohn - 08 May 2008 20:19 GMT
Our Customer Service people SEARCH the database (main form / parent record)
to make sure there is not an existing complaint on the particular CUSTOMER
ORDER, if there is, they record the new issue in the subform details which
make a new child record.  If the SEARCH turns up, no record found, they know
to start a new / parent record for the CUSTOMER ORDER that has no previous
complaints.

But after running the SEARCH and not finding a record, the form/subform is
now displaying the first record of the database and not leaving the form set
to DataEntry so the user can easily start a new record!

The current code:

'--------------------------------------------------------------
'Seach field concept by Graham Thorpe
'--------------------------------------------------------------
Private Sub cmdSearch_Click()
   Dim Unit_IDRef As String
   Dim strSearch As String

'Check txtSearch for Null value or Nill Entry first.

   If IsNull(Me![txtSearch]) Or (Me![txtSearch]) = "" Then
       MsgBox "Please enter a value!", vbOKOnly, "Invalid Search
Criterion!"
       Me![txtSearch].SetFocus
   Exit Sub
End If
'---------------------------------------------------------------

'Performs the search using value entered into txtSearch
'and evaluates this against values in UNIT_ID field

   DoCmd.ShowAllRecords
   DoCmd.GoToControl ("Unit_ID")
   DoCmd.FindRecord Me!txtSearch

   Unit_ID.SetFocus
   Unit_IDRef = Unit_ID.Text
   txtSearch.SetFocus
   strSearch = txtSearch.Text

'If matching record found sets focus in UNIT_ID and shows msgbox
'and clears search control

   If Unit_IDRef = strSearch Then
       MsgBox "Match Found For: " & strSearch, , "Congratulations!"
       Unit_ID.SetFocus
       txtSearch = ""

'If value not found sets focus back to txtSearch and shows msgbox
       Else
         MsgBox "Match Not Found For: " & strSearch & " - Please Try
Again.", _
           , "Invalid Search Criterion!"
           txtSearch.SetFocus
   End If
End Sub

> Where in my code did you get a compile error.  It was, of course, air code
> written in the message editor, but from practical experience.
[quoted text clipped - 21 lines]
>> populated with the first record in the db while attempting your second
>> search in the search field.
Klatuu - 08 May 2008 20:52 GMT
The reason it is going to the first record in the recordset when no match is
found is because of this line:

   DoCmd.ShowAllRecords

The ShowAllRecords method does a couple of things, some of which you don't
really want to do. It removes any filtering on the form's recordsetand is
requeies the recordset.  A requery always takes you back to the first record
of a form recordset.

So, the question is is there any filtering on the form or the form's
recordset?  If not, maybe just removing that line will resovle the issue.  If
that doesn't do it, I would suggest rewriting the sub using more specific
code.  The code, as written, is one step better than macros, but is still a
long shot from precisely written code.

If you need help rewriting how the search is done, I would need to know the
name of the field in the form's recordset you want to search on.  But, as a
general way of doing a search, this may help.

This assumes the code is in the form module of the form you are searching:

'Performs the search using value entered into txtSearch
'and evaluates this against values in UNIT_ID field

Private Sub cmdSearch_Click()

'Check txtSearch for Null value or Nill Entry first.

   If Nz(Me.txtSearch), vbNullString = vbNullString Then
       MsgBox "Please enter a value!", vbOKOnly, "Invalid Search
Criterion!"
       Me.txtSearch.SetFocus
   End If
   Else
       With Me.RecordsetClone
'Use this if the table field Unit_ID is numberic
           .FindFirst "[Unit_ID] = " & .txtSearch                          
                   

'Use this if the table field Unit_ID is text
           .FindFirst "[Unit_ID] = """ & .txtSearch & """"

           If .NoMatch Then
             MsgBox "Match Not Found For: " & strSearch & " - Please Try
Again.", _
           , "Invalid Search Criterion!"
               txtSearch.SetFocus
           Else
               MsgBox "Match Found For: " & strSearch, , "Congratulations!"
               Me.Unit_ID.SetFocus
               Me.txtSearch = vbNullString
           End If
       End With
   End With
End Sub

Signature

Dave Hargis, Microsoft Access MVP

> Our Customer Service people SEARCH the database (main form / parent record)
> to make sure there is not an existing complaint on the particular CUSTOMER
[quoted text clipped - 80 lines]
> >> populated with the first record in the db while attempting your second
> >> search in the search field.
Rohn - 08 May 2008 22:12 GMT
I removed the   DoCmd.ShowAllRecords
and it gets an error on   DoCmd.FindRecord Me!txtSearch

This maybe a dumb question but, I can't just say DoCmd.HideAllRecords or
Form.NewRecord or something?

Maybe I should try your method, so that I am not handcuffed to code I can
not make work right!

So, I create a Module place this code in it and save as Search!  Already
done!  Then what?

Again, thanks for your help on this,  Rohn

> The reason it is going to the first record in the recordset when no match
> is
[quoted text clipped - 152 lines]
>> >> populated with the first record in the db while attempting your second
>> >> search in the search field.
Klatuu - 08 May 2008 22:21 GMT
No, you don't create a module. You just replace the code you have in the sub
with my code.  Be aware it was written in this message box, so it may have
errors we will have to work out.
Signature

Dave Hargis, Microsoft Access MVP

> I removed the   DoCmd.ShowAllRecords
> and it gets an error on   DoCmd.FindRecord Me!txtSearch
[quoted text clipped - 166 lines]
> >> >> populated with the first record in the db while attempting your second
> >> >> search in the search field.
Rohn - 08 May 2008 22:33 GMT
I replaced the code and I get a Compile Error on:

If Nz(Me.txtSearch), vbNullString = vbNullString Then

It is the only thing in the VB window that is red.

> No, you don't create a module. You just replace the code you have in the
> sub
[quoted text clipped - 14 lines]
>>
>> Again, thanks for your help on this,  Rohn
Klatuu - 08 May 2008 22:41 GMT
Told you this might happen <g>
Should be:
If Nz(Me.txtSearch, vbNullString) = vbNullString Then
Signature

Dave Hargis, Microsoft Access MVP

> I replaced the code and I get a Compile Error on:
>
[quoted text clipped - 20 lines]
> >>
> >> Again, thanks for your help on this,  Rohn
Rohn - 09 May 2008 11:36 GMT
Made a few tweaks; had errors with Criterion, end with, missing end if.  Now
have an error: Run time error - Object doesn't support this property or
method, on:

.FindFirst "[Unit_ID] = " & .txtSearch

BTW, the Unit_ID field has a TEXT field property and the data is always
Alphanumeric.
I think it is getting closer to working!!!  This is awesome!

> Told you this might happen <g>
> Should be:
[quoted text clipped - 29 lines]
>> >>
>> >> Again, thanks for your help on this,  Rohn
Klatuu - 09 May 2008 14:46 GMT
Another whoopsy, sorry, but it is hard to get it right without VBE correcting
me as I go.  This line:
          .FindFirst "[Unit_ID] = " & .txtSearch                          
Should be
          .FindFirst "[Unit_ID] = " & Me.txtSearch
Signature

Dave Hargis, Microsoft Access MVP

> Made a few tweaks; had errors with Criterion, end with, missing end if.  Now
> have an error: Run time error - Object doesn't support this property or
[quoted text clipped - 39 lines]
> >> >>
> >> >> Again, thanks for your help on this,  Rohn
Rohn - 09 May 2008 15:08 GMT
great, I added the revised wording.  No errors but the search button is not
finding existing values!  Here is the revised code.

'Performs the search using value entered into txtSearch
'and evaluates this against values in UNIT_ID field

Private Sub cmdSearch_Click()

'Check txtSearch for Null value or Nill Entry first.

   If Nz(Me.txtSearch, vbNullString) = vbNullString Then
       MsgBox "Please enter a value!", vbOKOnly, "Invalid Search
Criterion!"
       Me.txtSearch.SetFocus
   Else
       With Me.RecordsetClone
'Use this if the table field Unit_ID is numberic
           .FindFirst "[Unit_ID] = " & Me.txtSearch

'Use this if the table field Unit_ID is text
           .FindFirst "[Unit_ID] = """ & Me.txtSearch & """"

           If .NoMatch Then
             MsgBox "Match Not Found For: " & strSearch & " - Please Try
Again.", _
           , "Invalid Search Criterion!"
               txtSearch.SetFocus
           Else
               MsgBox "Match Found For: " & strSearch, , "Congratulations!"
               Me.Unit_ID.SetFocus
               Me.txtSearch = vbNullString
           End If
       End With
   End If
End Sub

> Another whoopsy, sorry, but it is hard to get it right without VBE
> correcting
> me as I go.  This line:
>           .FindFirst "[Unit_ID] = " & .txtSearch
> Should be
>           .FindFirst "[Unit_ID] = " & Me.txtSearch
Klatuu - 09 May 2008 15:19 GMT
You are using both versions of what should be one line.

'Use this if the table field Unit_ID is numberic
           .FindFirst "[Unit_ID] = " & Me.txtSearch

'Use this if the table field Unit_ID is text
           .FindFirst "[Unit_ID] = """ & Me.txtSearch & """"

As the comments say, if Unit_ID ia a numeric field in the form's record set,
use the first version and delete the second version.  If it is a text field,
delete the first version and use the second version.  Note the text version
encloses the search value in quotes which is correct syntax when seaching for
text.  You do not use any delimiters when seaching for a numeric value.  And,
FYI, if you are searching a date field, the delimiters are the # sign:
           .FindFirst "[Unit_ID] = #" & Me.txtSearch & "#"

Signature

Dave Hargis, Microsoft Access MVP

> great, I added the revised wording.  No errors but the search button is not
> finding existing values!  Here is the revised code.
[quoted text clipped - 38 lines]
> > Should be
> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 15:40 GMT
Thanks for the explaination of the two fields.  I took out the first field
since we are searching for Alphanumeric data (like Z004 or Y142K).

The new error is a Compile Error: Invalid or Unqualified Reference on:
.txtSearch    in this peice of the code.

'Use this if the table field Unit_ID is text
           .FindFirst "[Unit_ID] = """ & Me.txtSearch & """"

Thanks for sticking with this issue!

> You are using both versions of what should be one line.
>
[quoted text clipped - 61 lines]
>> > Should be
>> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Klatuu - 09 May 2008 15:48 GMT
Is txtSearch the name of a control on the form the code is in or it is on a
subform or different form?

Signature

Dave Hargis, Microsoft Access MVP

> Thanks for the explaination of the two fields.  I took out the first field
> since we are searching for Alphanumeric data (like Z004 or Y142K).
[quoted text clipped - 72 lines]
> >> > Should be
> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 16:12 GMT
txtSearch is the text box on the same form as the button with this VB
control.

> Is txtSearch the name of a control on the form the code is in or it is on
> a
[quoted text clipped - 80 lines]
>> >> > Should be
>> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 16:28 GMT
Just found that I had a space between the Me and the period.  So the error
went away but it still does not see the current records?  It is trying to
work

The correct message box comes up when the txtSearch field is blank but
existing data is not being found?

thanks, Rohn

> txtSearch is the text box on the same form as the button with this VB
> control.
[quoted text clipped - 16 lines]
>>>
>>> > You are using both versions of what should be one line.
Klatuu - 09 May 2008 17:24 GMT
That doesn't make any sense, then.
Try going into the VB Editor and commenting out that line.  Then type it in
again and when you type Me.  The intellisense dropdown should have txtSearch
in it.  If it doesn't then there is a naming problem.  If it does, then
select it and see what happens.
Signature

Dave Hargis, Microsoft Access MVP

> Thanks for the explaination of the two fields.  I took out the first field
> since we are searching for Alphanumeric data (like Z004 or Y142K).
[quoted text clipped - 72 lines]
> >> > Should be
> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 18:01 GMT
Intellisense has txtSearch in the list!   I selected it and it doesn't work
as it is or with the added  & """"    at the end?

Any other ideas?

> That doesn't make any sense, then.
> Try going into the VB Editor and commenting out that line.  Then type it
[quoted text clipped - 84 lines]
>> >> > Should be
>> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Klatuu - 09 May 2008 18:19 GMT
Save the mdb and do a compact and repair.
There is something wrong with the form.
Signature

Dave Hargis, Microsoft Access MVP

> Intellisense has txtSearch in the list!   I selected it and it doesn't work
> as it is or with the added  & """"    at the end?
[quoted text clipped - 89 lines]
> >> >> > Should be
> >> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 18:34 GMT
I am hosed!!!!!!   Compact and Repair did nothing!

An I stream line the Search to just do the search and eliminate the popup
boxes if that is adding some complications to the functionality!

Thanks for your help so far!
Rohn

> Save the mdb and do a compact and repair.
> There is something wrong with the form.
[quoted text clipped - 101 lines]
>> >> >> > Should be
>> >> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Klatuu - 09 May 2008 18:44 GMT
I Think I got the number of quotes correct, but just as a text, change this
line:
.FindFirst "[Unit_ID] = """ & Me.txtSearch & """"

To:
.FindFirst "[Unit_ID] = '" & Me.txtSearch & "'"
Expanded for clarity:
.FindFirst "[Unit_ID] = ' " & Me.txtSearch & " ' "

It shouldn't make a difference, but just in case I got the quotes wrong.

Signature

Dave Hargis, Microsoft Access MVP

> I am hosed!!!!!!   Compact and Repair did nothing!
>
[quoted text clipped - 109 lines]
> >> >> >> > Should be
> >> >> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 19:32 GMT
Dave,
I removed the entire line and put in a stripped down version:  .FindFirst
[Unit_ID] = Me.txtSearch   with no luck but

my error is: Run-time error 3421: Data type conversion error.    I think it
is trying to work!

I even tried the form.control wording:   .FindFirst
"[(F)FRDF_CONTACT.Unit_ID] = ' " & Me.txtSearch & " ' "

but didn't have any luck.

>I Think I got the number of quotes correct, but just as a text, change this
> line:
[quoted text clipped - 126 lines]
>> >> >> >> > Should be
>> >> >> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 20:55 GMT
Here is a question!    The form is set to Data Entry on initial open will
that affect this type of code?

>I Think I got the number of quotes correct, but just as a text, change this
> line:
[quoted text clipped - 126 lines]
>> >> >> >> > Should be
>> >> >> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 21:10 GMT
I set both the Main form and Sub Form to No DataEntry to allow all records
to be visible via the form navigation and ran the new code, the popup
actually says "record found" but it does not change the current record set
to that found record?   This is very close, I just can't tell what the
problem is.

I really appreicate all the help you have given on this one.  I have learned
alot from you about how these code actually work.

Thanks again for the assistance.
Rohn

> Here is a question!    The form is set to Data Entry on initial open will
> that affect this type of code?
[quoted text clipped - 133 lines]
>>> >> >> >> > Should be
>>> >> >> >> >           .FindFirst "[Unit_ID] = " & Me.txtSearch
Rohn - 09 May 2008 13:52 GMT
Did some tweaks when some minor errors came up, like: End If missing, End
With, quote on Criterion but then had a Run-time error '438': Object doesn't
support this property or method, on:
.FindFirst "[Unit_ID] = " & .txtSearch & ""

BTW, the UNIT_ID field is a text property and populated with alphanumeric
data.

Thanks, Rohn

> Told you this might happen <g>
> Should be:
[quoted text clipped - 29 lines]
>> >>
>> >> Again, thanks for your help on this,  Rohn
Rohn - 08 May 2008 20:20 GMT
Thanks Dave, for spending some time on this issue, its a major pain for the
users and I just can't figure out how to modify it.

Rohn

> Where in my code did you get a compile error.  It was, of course, air code
> written in the message editor, but from practical experience.
[quoted text clipped - 4 lines]
> what you are trying to accomplish and post all the existing code, we can
> offer a how to.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.