Results 1 to 5 of 5
  1. #1
    DmytriE is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    2

    Open Form

    I am baffled by similarities in my code but the different outputs when run. This code simply checks whether a checkbox which is defined by the variable Administrator is checked. If it is then it will open the administrator form (frmCaseList_Admin) while the opposite would open the regular form. If the value of Administrator is true then it will open the form, frmCaseList_Admin, and then set the RecordSource property followed by setting the caption for a label. The regular form, frmCaseList, is supposed to do the exact same thing. However, it returns the following error:

    "Run-time error '2501':


    The OpenForm action was cancelled."

    Code:
    ElseIf (Administrator = False) Then
        DoCmd.OpenForm "frmCaseList"
        Forms("frmCaseList").RecordSource = CaseListRS
        Forms("frmCaseList").lblCaseListHeader.Caption = StaffName & "'s Case List"
    Else
        DoCmd.OpenForm "frmCaseList_Admin"
        Forms!frmCaseList_Admin.RecordSource = CaseListRS
        Forms("frmCaseList_Admin").lblCaseListHeader.Caption = StaffName & "'s Case List"
    End If
    Can anyone help me figure out why these two similar blocks of code return different outputs? Thanks.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Please show all of the code involved.
    What exactly is Administrator?

    Have you tried me.requery to assign the latest recordsource to your form?
    eg:

    Code:
    ....
    ElseIf (Administrator = False) Then
        DoCmd.OpenForm "frmCaseList"
        Forms("frmCaseList").RecordSource = CaseListRS
        Forms("frmCaseList").lblCaseListHeader.Caption = StaffName & "'s Case List"
        Me.requery
    Else
        DoCmd.OpenForm "frmCaseList_Admin"
        Forms!frmCaseList_Admin.RecordSource = CaseListRS
        Forms("frmCaseList_Admin").lblCaseListHeader.Caption = StaffName & "'s Case List"
        Me.Requery
    End If
    ....

  3. #3
    DmytriE is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    2
    Administrator: A checkbox that is able to be checked on the login page if certain conditions are met. It is used to define which form is opened since each form has different objects associated with them.
    CaseListRS: This holds the appropriate record source string value. This points to the query that is created which holds the desired information.
    StaffName: Name of the staff who is logging in.

    Code:
    Public Sub OpenCases(Administrator As Boolean, CaseListRS As String, StaffName As String)
    CaseListRS = Replace(CaseListRS, " ", "")
    
    
    If (DCount("[ChildFullName]", CaseListRS) <= 0) Then
        DoCmd.OpenForm "frmCaseListEmpty", acNormal
        Forms("frmCaseListEmpty").lblCaseListHeader.Caption = StaffName & "'s Case List"
        Forms("frmCaseListEmpty").lblEmptyList.Caption = "NO CASES FOR " & UCase(StaffName) & "."
        Forms("frmCaseListEmpty").lblEmptyList.FontSize = 28
        Forms("frmCaseListEmpty").lblEmptyList.TextAlign = Center
    ElseIf (Administrator = False) Then
        Forms("frmCaseList").RecordSource = CaseListRS <-- Breaks here
        DoCmd.OpenForm "frmCaseList"
        Forms("frmCaseList").lblCaseListHeader.Caption = StaffName & "'s Case List"
    Else
        DoCmd.OpenForm "frmCaseList_Admin" <-- Does not break here
        Forms!frmCaseList_Admin.RecordSource = CaseListRS
        Forms("frmCaseList_Admin").lblCaseListHeader.Caption = StaffName & "'s Case List"
    End If
    
    End Sub
    With the breaking locations, the Me.requery would not work.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Why do you have a checkbox that a user can check to indicate Administrator? (my understanding of what you are showing/saying)
    If you have a list of users, some of which are Administrators, then signal that in the userTable, and open the form based on that, not a checkbox that anyone can check.

  5. #5
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    A couple of observations:
    Usually when you create an If Else End If block, you don't test for one condition, then test for a different one in the same block. The logic should be similar to If something = True Then do this, but (Else) If something = False, then do the other. What you have is If A is black then do this but if B is wet do this. That makes no sense.

    It sounds like either the form is not being opened (thus the cancellation message when you try to set the recordsource) or the form open event is cancelling. If the DCount is zero, you go to here
    Code:
    ElseIf (Administrator = False) Then
        Forms("frmCaseList").RecordSource = CaseListRS
    but the form was not opened by this procedure if DCount > 0.

    I'd make a simple choice of which form to open
    Code:
    If Administrator = True Then
      DoCmd.OpenForm "frmCaseList_Admin"
    Else
      DoCmd.OpenForm "frmCaseList"  
    End If
    and set the record source in the form open event rather than do it remotely.

    If the execution logic I've pointed out is incorrect, then what's missing from your information at this point is the form open event code so we might tell if you're cancelling it. BTW, I am not making the assumption that this Administrator thing is a method of controlling permissions. It might just be a word that you're using to direct code execution. However, if the intent is to control permissions, then I also see no sense in having such a feature if anyone can invoke it.

    PS: Also, if these forms are virtually the same, when you progress in your Access knowledge, you should learn how to open the same form in different ways for different levels of users rather than make duplicates of forms (or anything else) to suit situations.
    Last edited by Micron; 07-27-2016 at 08:24 PM. Reason: PS
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-02-2014, 01:31 PM
  2. Replies: 2
    Last Post: 08-07-2013, 07:44 AM
  3. Help on report to open form to open query...
    By saseymour in forum Programming
    Replies: 13
    Last Post: 07-16-2013, 08:11 AM
  4. Replies: 1
    Last Post: 05-03-2012, 02:25 PM
  5. Replies: 2
    Last Post: 02-26-2010, 08:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums