Using VBA to set Form and SubForm Read only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GaryDublin
    New Member
    • Feb 2016
    • 1

    Using VBA to set Form and SubForm Read only

    I have a form containing a subform which is used to enter payment requisitions in. I have the form opening from a VB button on a menu form. I am trying to set the form and subform to editable if opened via a Button with onclick Event as shown.

    Code:
    Private Sub btnNewPayment_Click()
    DoCmd.OpenForm "frmPayments", , , , acFormAdd
    End Sub

    or Read only if entered through another button.


    Code:
    Private Sub btnAllPayments_Click()
    DoCmd.OpenForm "frmPayments", , , , acFormReadOnly, acDialog, "ReadOnly"
    End Sub
    I have some VBA in the OnLoad Event which seems to cause the acFormreadOnly to be disabled.

    When I click on the subform and then back to the main form, the ReadOnly is working.

    Some of the code that I have in the OnLoad event

    Code:
    Private Sub Form_Load()
    
    If UserLevel() >= 4 Then ' UseLevel is a custom Function
    
        Me.lblAuthorisedForPayment.Visible = True
        Me.lblAuthorisedBy.Visible = True
        Me.lblTimeDate.Visible = True
        Me.AuthorisedForPayment.Visible = True
        Me.AuthorisedBy.Visible = True
        Me.TimeDateAuthorised.Visible = True
    
    Else
    
        Me.lblAuthorisedForPayment.Visible = False
        Me.lblAuthorisedBy.Visible = False
        Me.lblTimeDate.Visible = False
        Me.AuthorisedForPayment.Visible = False
        Me.AuthorisedBy.Visible = False
        Me.TimeDateAuthorised.Visible = False
        
    
    End If
         
         
        Me.tboLastExRate = Format(CurrentDb.OpenRecordset("qryLastDateCurrencyImported").Fields(0), "dd mmm yyyy")
    
        DoCmd.GoToControl "PaymentID"
        
    End Sub
    I have tried adding to the OnActivate event using the "ReadOnly" Argument attached to the button.

    Code:
    Private Sub Form_Activate()
        If Me.OpenArgs = "ReadOnly" Then
        Me.AllowEdits = False
        Me.AllowDeletions = False
        Me.AllowAdditions = False
     
        'Name the control, containing your subform 
    
        Me.sub_frmPaymentDetails.Form.AllowEdits = False
        Me.sub_frmPaymentDetails.Form.AllowDeletions = False
        Me.sub_frmPaymentDetails.Form.AllowAdditions = False
      End If
    End Sub
    I have been wracking my brain and scouring the internet for a solution and have drawn a blank. I hope that I have missed something small and fundamental that I will learn from.....!

    Thanks for your help.

    Gary
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    What is the result of your On Activate event running? Have you stepped through the code to see if your IF statement is following the path that you expect? Note that opening the main form as read only doesn't affect the subforms editing properties.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32662

      #3
      I'd use the Load or Open event to handle this. Why are you using Activate?

      Your Activate code looks fine. As Seth says, you may need to check that it's running when you think it is ;-)

      Comment

      Working...