Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CmdBtn OnClick: Cancel Add New Record 1

Status
Not open for further replies.

Melagan

MIS
Nov 24, 2004
443
US
Greetings,

On a data entry form, I have a command button that opens another form that prompts the user to enter search criteria to pull up old records. The other form re-opens the main data entry form with the criteria as expected. This works great unless the main form is in add-mode. If so, I get the following error: "Runetime 2501. The OpenForm action was canceled."

So to get around that, I figured the best way to be to cancel the users current data entry of they hit the command button that opens the search form. Are there any properties or methods that would accomplish this? Here is the code I'm working with:

Data Entry form command button:
Code:
Private Sub cmdFind_Click()
'gstrFormName declarred globally
gstrFormName = Me.Form.Name
DoCmd.OpenForm "frmSearch", acNormal

End Sub


Search Form command button:
Code:
Private Sub cmdOK_Click()
'gstrFormName declared globally.
Dim blnFound As Boolean

blnFound = Nz(DLookup("[Escrow Number]", "Escrows", "[Escrow Number] = '" & Me.txtSearch.Value & "'"), False)
        
    If blnFound = False Then
        MsgBox "Escrow number " & Me.txtSearch.Value & " not found in database.", _
         vbInformation, "NATTrack"
    Else
        DoCmd.OpenForm gstrFormName, acNormal, , "[Escrow Number] = '" & Me.txtSearch.Value & "'"
         DoCmd.Close acForm, "frmSearch", acSaveNo
    End If

End Sub


Thanks in advance for the help!

~Melagan
______
"It's never too late to become what you might have been.
 
Melagan,
The [tt]Undo[/tt] in conjunction with [tt]Close[/tt] should cancel the users current data entry and reset the form.

Code:
Private Sub cmdFind_Click()
'gstrFormName declarred globally
gstrFormName = Me.Form.Name
[b]Me.Undo
DoCmd.Close[/b]
DoCmd.OpenForm "frmSearch", acNormal

End Sub

If you want to allow the users to keep both open you could use something like this in the search form. It will allow you to open multiple instances of a single form. These will stay open as long as the Search form is open.
Code:
[green]'Global delcaration[/green]
Dim colSpawnedForms As New Collection

Private Sub cmdOK_Click()
[b]Dim frmNew As New [i]Form_Form1[/i][/b]
...

    Else
[b]      With frmNew
        .Visible = True
        .Filter = "[Escrow Number] = '" & Me.txtSearch.Value & "'"
        .FilterOn = True
      End With
      colSpawnedForms.Add frmNew[/b]
      [s]DoCmd.Close acForm, "frmSearch", acSaveNo[/s]
...
End Sub

[tt]colSpawnedForms[/tt] could also be moved to a global location, allowing you keep multiple copies of the form open independent of the search form.

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Caution,

Thank you for pointing out both the Undo method and some extra tips on how to open multiple instances of a single form. Nice!

~Melagan
______
"It's never too late to become what you might have been.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top