One piece of code conflicting with another

BlueJacket

Registered User.
Local time
Today, 16:51
Joined
Jan 11, 2017
Messages
90
I just noticed that when I open frmServiceChart from another form, frmMainSearch, that the search box in frmServiceChart no longer works. When I open up frmServiceChart from the Navigation Pane, it works as intended.

The code for opening up frmServiceChart from frmMainSearch is as follows:

Code:
Private Sub lboMainSearchResults_DblClick(Cancel As Integer)

'Opens frmServiceChart for the selected property
    
    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "frmServiceChart"
    
    stLinkCriteria = "[PropertyID]=" & Me![lboMainSearchResults]
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    
End Sub

The code for the combobox search is:

Code:
Private Sub cboGoToRecord_AfterUpdate()

'Searches properties based off the info entered in the GoToRecord searchbox

    On Error Resume Next
    Dim rst As Object
    Set rst = Me.RecordsetClone
    rst.FindFirst "[PropertyID]=" & Me.cboGoToRecord.Value
    Me.Bookmark = rst.Bookmark
    
End Sub

At least, these are the two pieces of code that I think are at hand. What is conflicting here and how do I fix it?

Thank you in advance.
 
The first bit of your code opens the form with a where condition applied ie the form will only show those records which satisfy the condition in stLinkedCriteria

If you subsequently search the form for a record not included in the opening where criteria, nothing will show.

You could use filters when you open the form intially
stLinkCriteria = "[PropertyID]=" & Me![lboMainSearchResults] DoCmd.OpenForm stDocName
Forms!stDocName.filter= stLinkCriteria
forms!stdocname.filteron = true
 
Well, looking at this code...
Code:
Private Sub lboMainSearchResults_DblClick(Cancel As Integer)
'Opens frmServiceChart for the selected property
    
    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "frmServiceChart"
    
    stLinkCriteria = "[PropertyID]=" & Me![lboMainSearchResults]
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    
End Sub

[COLOR="Green"]'which we can simplify as follows...[/COLOR]

Private Sub lboMainSearchResults_DblClick(Cancel As Integer)
    DoCmd.OpenForm "frmServiceChart", , , "PropertyID = " & Me.lboMainSearchResults
End Sub
...it looks like it opens the indicated form with a filter that displays a single record. In that case, when you try an search in that form with this code....
Code:
Private Sub cboGoToRecord_AfterUpdate()
   'Searches properties based off the info entered in the GoToRecord searchbox
    On Error Resume Next[COLOR="Green"] 'this hides any errors that may occur[/COLOR]
    Dim rst As Object
    Set rst = Me.RecordsetClone
    rst.FindFirst "[PropertyID]=" & Me.cboGoToRecord.Value
    Me.Bookmark = rst.Bookmark
End Sub
The item is simply not found. But there is no message box or indication that the FindFirst completes successfully with no result. So you might--after running FindFirst--check the .NoMatch property to see if the search succeeded, and educate the user, like...
Code:
Private Sub cboGoToRecord_AfterUpdate()
    with Me.RecordsetClone
       .FindFirst "PropertyID = " & Me.cboGoToRecord
       if not .nomatch then 
          Me.Bookmark = .Bookmark
       else
          if not .eof then .movelast
          msgbox "Property ID " & me.cboGoToRecord & " does not exist.  Checked " & .recordcount & " records.
       end if
    end with
End Sub
See what happens there? We run the FindFirst, and then we check NoMatch. If something is found, we nav to that record using bookmarks, else count the records in the recordset, and inform the user what we looked for, and how many records we searched.

hth
 

Users who are viewing this thread

Back
Top Bottom