473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What if DoCmd.FindRecord can't find a record?

KirkT
5
Hello everyone,

I am using the DoCmd.FindRecord to search a table and pull up a record. The way it is working right now, if it doesn't find the record, it still pulls of the form.

What I would like to do is put the DoCmd.FindRecord into an If statement and if it doesn't find a record, display a error message telling them to try again.

I have tried to put the Find Record into an If statement but it just gives me an error.

Here is the code that I am working wth:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOKButtton_Click()
  2. On Error GoTo Err_cmdOKButtton_Click
  3.  
  4.     ' Open the Edit FQT Test Log form
  5.     DoCmd.OpenForm "frm_Edit_FQT_Test_Log"
  6.  
  7.     Forms!frm_FQT_Test_Log!ID.SetFocus
  8.  
  9.     DoCmd.FindRecord Forms!frm_Test_Log_Record_Selection!Record_ID
  10.  
  11.     If DoCmd.FindRecord = True Then
  12.  
  13.     ' Set the focus on the System_ID field
  14.     Forms!frm_FQT_Test_Log!System_ID.SetFocus
  15.     ' Enable the bottom portion of the form
  16.     Forms!frm_FQT_Test_Log!TabCtl132.Enabled = True
  17.  
  18.     Else
  19.     DoCmd.Close
  20.     MsgBox "That record number does not exist, plase try again.", vbOKOnly + vbCritical, "An error has occured"
  21.  
  22. Exit_cmdOKButtton_Click:
  23.     Exit Sub
  24.  
  25. Err_cmdOKButtton_Click:
  26.     MsgBox Err.Description
  27.     Resume Exit_cmdOKButtton_Click
  28.  
  29. End Sub
  30.  
Any help would be appreciated.

Kirk
Jun 23 '17 #1

✓ answered by zmbd

Hello KirkT
You've hit on one of the limitations of the DoCmd.FindRecord (link to Syntax)
This function does NOT return any "user feedback" if no record is found.

One way to do test if the record is present is to open a recordset on your data.
If you are using a form, then a snapshot of the recordset may already available for your use provided the form is bound.

Another method is the Dlookup function (link to syntax) as it will at least return a null() value that you can check for.

-
From your code:

+It is not clear on what form "cmdOKButtton" event is launching.

+ "if it doesn't find the record, it still pulls of the form."
isn't clear; however, line 11 will error, see the link above for the command syntax, the remaining conditional will not execute.
> a debug/compile will catch this type of error.

+ Line 9 isn't clear; however, it appears that you are attempting to search against
"frm_FQT_Test_Log" in field "ID" for the value in the current record in "frm_Test_Log_Record_Selection" field "Record_ID"

line 19 needs to be more explicit in what it is closing.

>> I'm going to PM you a copy of a boilerplate that has some tutorial and other information, please check your
Bytes.com\inbox

So from the above information it may be possible to open a recordset on the underlying datasource for "frm_FQT_Test_Log" then use the findfirst method of the recordset to find the record, if it exists, and then move to that record in the "frm_FQT_Test_Log" form.

+++ a little hard to determine exactly what you are after here, there may be some better methods than what you are currently trying such as a parent/child form or other methods of searching.

Here's a very crude example against my test database using the recordset method.

In this instance what I have done here is created a very simple form with a text box control and a command button for the search and then I search against the form named "frm_people"

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Command2_Click()
  5.   Dim zrs As DAO.Recordset
  6.   Dim zrs_clone As DAO.Recordset
  7.   Dim zsql As String
  8.   '
  9.   On Error GoTo zerrtrap
  10.   '
  11.   'trap for missing or null entries
  12.   If Me.zctrl_SearchTextBox & "" = "" Then Err.Raise Number:=(vbObjectError + 1), Source:="Command2_Click", Description:="An entry is required for this search to work"
  13.   '
  14.   'First make sure our form we're searching against is loaded and open or the code will error
  15.   If Not CurrentProject.AllForms("frm_people").IsLoaded Then DoCmd.OpenForm FormName:="frm_people"
  16.   '
  17.   'set a pointers to the recordset of form to be searched
  18.   'I only want to set the main recordset if it's not already opened... shouldn't be but I've seen strange things happen
  19.   If zrs Is Nothing Then Set zrs = Forms!frm_people.Recordset
  20.   Set zrs_clone = Forms!frm_people.RecordsetClone
  21.   '
  22.   ' create the SQL string used to find the record of interest
  23.   '>> very importain here to note that if we were using a string value in the field of interest
  24.   '>> we would have to have some additional quotes!
  25.   zsql = "data_pk=" & Me.zctrl_SearchTextBox
  26.   '
  27.   'using the cloned record set, zrs_clone, we're going to look up the record and if found then move
  28.   'form's record cursor to that position.
  29.   zrs_clone.FindFirst zsql
  30.   '
  31.   If zrs_clone.NoMatch Then
  32.     'if there's no matching record found then user feedback and close the form
  33.     MsgBox prompt:="There doesn't appear to be a record with that ID in the current records.", title:="No Match"
  34.     DoCmd.Close objecttype:=acForm, objectname:="frm_people", Save:=acSaveNo
  35.   Else
  36.     'if there is a matching record then move the form record cursor to that record and set the focus
  37.     zrs.Bookmark = zrs_clone.Bookmark
  38.     '
  39.     'note here, I rename the form controls, the default control name is the field name; however, this can create issues
  40.     With Forms!frm_people
  41.       .SetFocus
  42.       !zctrl_data_pk.SetFocus
  43.     End With
  44.   End If
  45.   '
  46. zcleanup:
  47. Stop
  48. 'cleanup step, ignore errors so-as to avoid a loop
  49. On Error Resume Next
  50.   'if the form is not loaded then we should close the pointers to the main recordset
  51.   'otherwise if we close the main recordset we will close that recordset on the form too
  52.   If Not CurrentProject.AllForms("frm_people").IsLoaded Then
  53.     If Not zrs Is Nothing Then
  54.       zrs.Close
  55.       Set zrs = Nothing
  56.     End If
  57.   End If
  58.   'it doesn't matter if we close the clone of the record set if the form is open so lets release the memory
  59.   If Not zrs_clone Is Nothing Then
  60.     zrs_clone.Close
  61.     Set zrs_clone = Nothing
  62.   End If
  63. Exit Sub
  64. zerrtrap:
  65.   MsgBox prompt:="Err Number " & Err.Number & vbCrLf & Err.Description, title:=Err.Source
  66.   Resume zcleanup
  67. End Sub
.

Clear as mud (@_@)
Never fear - we can help with that!
-Z

2 5421
zmbd
5,501 Expert Mod 4TB
Hello KirkT
You've hit on one of the limitations of the DoCmd.FindRecord (link to Syntax)
This function does NOT return any "user feedback" if no record is found.

One way to do test if the record is present is to open a recordset on your data.
If you are using a form, then a snapshot of the recordset may already available for your use provided the form is bound.

Another method is the Dlookup function (link to syntax) as it will at least return a null() value that you can check for.

-
From your code:

+It is not clear on what form "cmdOKButtton" event is launching.

+ "if it doesn't find the record, it still pulls of the form."
isn't clear; however, line 11 will error, see the link above for the command syntax, the remaining conditional will not execute.
> a debug/compile will catch this type of error.

+ Line 9 isn't clear; however, it appears that you are attempting to search against
"frm_FQT_Test_Log" in field "ID" for the value in the current record in "frm_Test_Log_Record_Selection" field "Record_ID"

line 19 needs to be more explicit in what it is closing.

>> I'm going to PM you a copy of a boilerplate that has some tutorial and other information, please check your
Bytes.com\inbox

So from the above information it may be possible to open a recordset on the underlying datasource for "frm_FQT_Test_Log" then use the findfirst method of the recordset to find the record, if it exists, and then move to that record in the "frm_FQT_Test_Log" form.

+++ a little hard to determine exactly what you are after here, there may be some better methods than what you are currently trying such as a parent/child form or other methods of searching.

Here's a very crude example against my test database using the recordset method.

In this instance what I have done here is created a very simple form with a text box control and a command button for the search and then I search against the form named "frm_people"

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Command2_Click()
  5.   Dim zrs As DAO.Recordset
  6.   Dim zrs_clone As DAO.Recordset
  7.   Dim zsql As String
  8.   '
  9.   On Error GoTo zerrtrap
  10.   '
  11.   'trap for missing or null entries
  12.   If Me.zctrl_SearchTextBox & "" = "" Then Err.Raise Number:=(vbObjectError + 1), Source:="Command2_Click", Description:="An entry is required for this search to work"
  13.   '
  14.   'First make sure our form we're searching against is loaded and open or the code will error
  15.   If Not CurrentProject.AllForms("frm_people").IsLoaded Then DoCmd.OpenForm FormName:="frm_people"
  16.   '
  17.   'set a pointers to the recordset of form to be searched
  18.   'I only want to set the main recordset if it's not already opened... shouldn't be but I've seen strange things happen
  19.   If zrs Is Nothing Then Set zrs = Forms!frm_people.Recordset
  20.   Set zrs_clone = Forms!frm_people.RecordsetClone
  21.   '
  22.   ' create the SQL string used to find the record of interest
  23.   '>> very importain here to note that if we were using a string value in the field of interest
  24.   '>> we would have to have some additional quotes!
  25.   zsql = "data_pk=" & Me.zctrl_SearchTextBox
  26.   '
  27.   'using the cloned record set, zrs_clone, we're going to look up the record and if found then move
  28.   'form's record cursor to that position.
  29.   zrs_clone.FindFirst zsql
  30.   '
  31.   If zrs_clone.NoMatch Then
  32.     'if there's no matching record found then user feedback and close the form
  33.     MsgBox prompt:="There doesn't appear to be a record with that ID in the current records.", title:="No Match"
  34.     DoCmd.Close objecttype:=acForm, objectname:="frm_people", Save:=acSaveNo
  35.   Else
  36.     'if there is a matching record then move the form record cursor to that record and set the focus
  37.     zrs.Bookmark = zrs_clone.Bookmark
  38.     '
  39.     'note here, I rename the form controls, the default control name is the field name; however, this can create issues
  40.     With Forms!frm_people
  41.       .SetFocus
  42.       !zctrl_data_pk.SetFocus
  43.     End With
  44.   End If
  45.   '
  46. zcleanup:
  47. Stop
  48. 'cleanup step, ignore errors so-as to avoid a loop
  49. On Error Resume Next
  50.   'if the form is not loaded then we should close the pointers to the main recordset
  51.   'otherwise if we close the main recordset we will close that recordset on the form too
  52.   If Not CurrentProject.AllForms("frm_people").IsLoaded Then
  53.     If Not zrs Is Nothing Then
  54.       zrs.Close
  55.       Set zrs = Nothing
  56.     End If
  57.   End If
  58.   'it doesn't matter if we close the clone of the record set if the form is open so lets release the memory
  59.   If Not zrs_clone Is Nothing Then
  60.     zrs_clone.Close
  61.     Set zrs_clone = Nothing
  62.   End If
  63. Exit Sub
  64. zerrtrap:
  65.   MsgBox prompt:="Err Number " & Err.Number & vbCrLf & Err.Description, title:=Err.Source
  66.   Resume zcleanup
  67. End Sub
.

Clear as mud (@_@)
Never fear - we can help with that!
-Z
Jun 24 '17 #2
NeoPa
32,556 Expert Mod 16PB
Hi Kirk.

Just a thought, but many people think first of searching through records, when a filter is more database-centric and more efficient. If you're interested in that alternative approach you'll find some pointers at Example Filtering on a Form.

Otherwise, you can do a lot worse than working with Zmbd.
Jun 24 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Sarah Smith via AccessMonster.com | last post by:
I am creating a database of documents that need to be worked on, are int eh proress of being worked on, and have been completed. Sometimes the same document (an updated version) comes back for more...
2
by: tehgreatmg | last post by:
Ok what I have is a list of boxes on a form that are connected to fields in my database. There is an extra box I want to be able to put the record number in and it automatically load the data...
0
by: BD | last post by:
I am in the process of developing new front end program for SQL database to replace MS Access front end. I have been unable to convert the following lines of VBA to C# to find a record in the...
1
by: RAG2007 | last post by:
Hi all: From a main form, after selecting a ID, I need pull up the appropriate record in a separate form. If there is no record for that ID I need to create a new record. Here's the code I"m...
4
by: RAG2007 | last post by:
Hi Having some problems with docmd.findrecord, getting runtime error 2162, in an adp, sql server back end. On my main form, I have a continuous view subform giving a list of subrecords within...
0
by: Ben Lahoy | last post by:
Hi! My problem is basically in a search modal window, where the user is allowed to make a selection on which option to take. After selecting an option and then giving the data to search, the...
8
by: internetintern | last post by:
hello, I have a code like: Private sub button1_Click() DoCmd.FindRecord Me.fieldname.Value, acStart, , acSearchAll, , , False end sub but the problem is that the command stops searching at...
6
by: QueenKirsty | last post by:
Hi. I am trying to move to a particular record on a form where the form has a composite PK. The PK is made of 3 fields: ModuleID, Precedence and RecordTypeID I have always used...
11
by: dowlingm815 | last post by:
the following code is generating an error; Mircosoft Access cant find the field '|" referred to in your expression" any suggestions would be appreciated. Private Sub...
1
by: neelsfer | last post by:
I want to add a Button to a Tab control called "TabCtl429" and the page index = 2. The button must open the find record dialog specifically in the "racenumber" field of the "RacetimingSF" subform on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.