VBA - Changing subform field's property from mainform button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • opmandrake
    New Member
    • Feb 2010
    • 5

    #1

    VBA - Changing subform field's property from mainform button

    Evening guys.

    Here something that's been driving me nuts for the past couple of hours.

    I have a mainform and in it, two subforms. Normally I'd like the fields to be read-only, but if a user has the right password he'd be able to click on a button on the mainform and turn the "locked" property on all the fields to FALSE.

    Code:

    Code:
    Private Sub cmdEdit_Click()
    
    Dim strPasswd
    
        strPasswd = InputBox("Please insert the password to edit records", "Access restricted")
    
        If strPasswd = "" Or strPasswd = Empty Then
            MsgBox "Cancelled.", vbInformation, "Insert password."
            Exit Sub
        End If
    
        If strPasswd = "password1" Then
            
    	'unlocks fields in main forms
            Me!cmdNewRecord.Enabled = True
            Me!txtSubject.Locked = False
    
    	'unlocks fields in subform 1
            Me!subAquisitions.Form!txtPrice.Locked = False
    
    	'unlocks fields in subform 2
            Me!subResponsible.Form!txtName.Locked = False
    
            
        Else
            MsgBox "Sorry, wrong password.", _
                   vbOKOnly, "Try again"
            Exit Sub
        End If
    
    End Sub
    Pretty straightforward ..but no good so far...:(

    The button unlocks the MAIN form's fields okay, but then I get this: "Run-time error '2465': Mainform can't find the field 'txtPrice' referred to in your expression."

    An ideas? Thanks for the help!
  • mshmyob
    Recognized Expert Contributor
    • Jan 2008
    • 903

    #2
    Proper syntax would be:

    [code=vb]
    Me!MainformName !subAquisitions .Form.txtPrice. Locked = False
    [/code]

    I usually just allow or diasallow edits, deletions, etc. for the entire subform rather than the individual controls but either way will work.

    cheers,

    Comment

    • opmandrake
      New Member
      • Feb 2010
      • 5

      #3
      Thanks mshmyob, I tried it though I seem to get a slightly different error:

      "Run-time error '2465': Mainform can't find the field 'frmProductData ' referred to in your expression."

      Of course "frmProductData " is the name of the form...and it still can't find it! How weird is that?

      Oh, I also know that the subforms are each in their own controls, I checked their names and they match the ones in the first example, and it still can't find them :(

      Comment

      • mshmyob
        Recognized Expert Contributor
        • Jan 2008
        • 903

        #4
        Interesting... could you attach a stripped down version of your database so I can check the form?

        cheers,

        Comment

        • opmandrake
          New Member
          • Feb 2010
          • 5

          #5
          Alright, I'll try to as soon as possible...I think I'll have to start it from scratch as there's too much company information, even in the descriptions and code. Thanks for the help, though!

          Comment

          • mshmyob
            Recognized Expert Contributor
            • Jan 2008
            • 903

            #6
            Just copy the form and paste it into a new database ( this will have a database with only the form and no data). I just want to see your syntax.

            cheers,

            Comment

            Working...