Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Posts
    20

    Question Unanswered: OpenForm action cancelled with error message

    Good day!

    I'm doing a little VBA programming for Access.

    I have two forms Form1 and Form2. By clicking a command button "cBut" on Form1 I would like Form2 to open but only if there is at least one record in it.

    The code below works fine if there are records to be displayed but when there is no records raises an error message "Run-time error '2501': The OpenForm action was canceled.".

    Code for Form1:

    Option Compare Database

    Public I As Integer

    Private Sub cBut_Click()
    DoCmd.OpenForm "Form2" 'debugger marks this line
    ... 'other statements
    End Sub

    Code for Form2:

    Option Compare Database

    Private Sub Form_Open(Cancel As Integer)
    A=DCount("*", "AgentsV")
    If A=0 Then
    MsgBox "No records to be retrieved! Going further.", vbOKOnly
    Cancel=True
    End If
    End Sub

    I have already lost too much time. Please help!

    Nice regards!
    BoHr

  2. #2
    Join Date
    Sep 2004
    Posts
    36

    Open form

    I would do the control before opening Form2 something like this..

    Dim db As DAO.Database 'maby remove DAO.
    Dim rs As DAO.Recordset 'maby remove DAO.
    Dim str As String

    Set db = CurrentDb

    str = "" 'Copy the text from a query and past between "" where you got a referance to form1 ex:
    'srt = "SELECT tbltest.Id, tbltest.text1, tbltest.text2 FROM tbltest WHERE (((tbltest.Id)=" & form_form1.txtidbox & "));

    Set rs = db.OpenRecordset(str, dbOpenSnapshot)


    If rs.EOF <> True Then
    DoCmd.OpenForm ("Form2")
    End If

    rs.Close
    db.Close

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    441
    Access wants to tell us what we alredy now, the 2501 - Cancel thingie, so we can trap it for instance like this.

    Private Sub cBut_Click()
    on error goto myerr
    DoCmd.OpenForm "Form2"
    ... 'other statements
    myexit:
    exit sub
    myerr:
    if err.number<>2501 then
    msgbox err.description
    resume myexit
    End Sub
    Roy-Vidar

  4. #4
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    I agree with theJedi, that way is probably the best way.

    But you could also just put an error check in cBut_Click

    Private Sub cBut_Click()
    On Error Goto FormError
    DoCmd.OpenForm "Form2" 'debugger marks this line
    ... 'other statements

    FORMERROR:
    If Err = 2051 Then
    'handle the error
    Resume Next 'move to the next line after the openform statement
    End If
    End Sub
    Inspiration Through Fermentation

  5. #5
    Join Date
    Dec 2004
    Posts
    20

    Smile

    Big thanks to all of you for your replies as well as merry Christmas and happy New Year 2005.!

    Nice regards!
    BoHr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •