Allow Edit on cmd Button Click (1 Viewer)

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
Hi,

I want to lock down my database to prevent accidental data changes...

I have a command button (cmdEdit) which on click should enable edit to both my main form and sub-form and then on a second click should return the forms to read only...

It works for the subform but not for the main form... initailly it is read only and then with the click I can edit but when i try to disable edits again it still allows me to change things... :confused:

Code:
Private Sub cmdEdit_Click()

    If Me.AllowEdits = True Then
        Me.AllowEdits = False
        Me.AllowAdditions = False
        Me!Invoice.Form.AllowEdits = False
        Me!Invoice.Form.AllowAdditions = False
        Me.cmdEdit.Caption = "Edit is Disabled"
    Else
        Me.AllowEdits = True
        Me.AllowAdditions = True
        Me!Invoice.Form.AllowEdits = True
        Me!Invoice.Form.AllowAdditions = True
        Me.cmdEdit.Caption = "Edit is Enabled"
    End If

End Sub

What's wrong with my code???

If it helps this is code that launches my main form (frmProject)

Code:
Private Sub cmdData_Click()
If Me.cboClient > 0 Or Me.cboProjectNo > 0 Then
DoCmd.OpenForm "frmProject", acNormal, , , acFormReadOnly
Else
DoCmd.OpenForm "frmerror3"
End If
End Sub
 

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
Hi,

I've not managed to alter my code at all and am still a bit stuck... if anyone does have a suggestion for me to try... I'd be grateful!!
 

Colin@Toyota

What's an Access?
Local time
Today, 14:44
Joined
May 2, 2006
Messages
203
Maybe try with a toggle button, instead of a cmd button
 

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
never ever used a toggle button before but on doing a bit of research it does sound like it could be useful... my code for my toggle button is:

Code:
Private Sub tglEdit_Click()
    If Me.tglEdit = False Then
        Me.AllowEdits = False
        Me.AllowAdditions = False
        Me!Invoice.Form.AllowEdits = False
        Me!Invoice.Form.AllowAdditions = False
        Me.tglEdit.Caption = "Edit is Disabled"
    Else
        Me.AllowEdits = True
        Me.AllowAdditions = True
        Me!Invoice.Form.AllowEdits = True
        Me!Invoice.Form.AllowAdditions = True
        Me.tglEdit.Caption = "Edit is Enabled"
    End If
End Sub

This doesn't allow me to edit anything no matter how many times I click it...

Any ideas what i'm doing wrong??
 

Colin@Toyota

What's an Access?
Local time
Today, 14:44
Joined
May 2, 2006
Messages
203
Hmm... I am by no means an Access or VBA expert, so nothing immediately jumps out to me as being wrong...

Could you try using the toggle button with the locked property?
 

Colin@Toyota

What's an Access?
Local time
Today, 14:44
Joined
May 2, 2006
Messages
203
Or maybe have two cmd buttons... one containing:

Code:
Dim cntrl as Control

For each cntrl in me.Controls
     .cntrl.locked = true
Next cntrl

and the other...

Code:
Dim cntrl as Control

For each cntrl in me.Controls
     .cntrl.locked = false
Next cntrl
 

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
I have got no idea.... :(

I have found so many bits of how to do this using allowedits or locked and enabled using both command and toggle buttons and nothing appears to work for me...

I honestly can't understand why my original code worked for my subform but not for my main form when i was using the same code...

Code:
Private Sub cmdEdit_Click()

    If Me.AllowEdits = True Then
        Me.AllowEdits = False
        Me.AllowAdditions = False
        Me!Invoice.Form.AllowEdits = False
        Me!Invoice.Form.AllowAdditions = False
        Me.cmdEdit.Caption = "Edit is Disabled"
    Else
        Me.AllowEdits = True
        Me.AllowAdditions = True
        Me!Invoice.Form.AllowEdits = True
        Me!Invoice.Form.AllowAdditions = True
        Me.cmdEdit.Caption = "Edit is Enabled"
    End If

End Sub

Anyone please help!!!
 

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
Really am very stuck.... anyone to take pity on me???

:rolleyes:
 

Extra_Cover

Registered User.
Local time
Today, 19:44
Joined
Oct 21, 2008
Messages
71
Hi Andy
Try Screen.ActiveForm

You could try disabling the controls as the default and then enabling with the following.



Code:
Dim ctl As Control
For Each ctl In Screen.ActiveForm
    If TypeOf ctl Is TextBox Then
        With ctl
        .Enabled = True
        End With
    End If
Next
 

hiền muội

small girl in a big world
Local time
Tomorrow, 01:44
Joined
Jun 11, 2009
Messages
23
Hi Andy,
I try your case and have idea.
Code:
Private Sub cmdEdit_Click()

    If Me.AllowEdits = True Then
        Me.AllowEdits = False
        Me.AllowAdditions = False
        
        [i][color=red]Me!YourSubform.Locked = True[/color][/i]
        Me.cmdEdit.Caption = "Edit is Disabled"
    Else
        Me.AllowEdits = True
        Me.AllowAdditions = True
        [i][color=red]Me!YourSubform.Locked = False[/color][/i]
        Me.cmdEdit.Caption = "Edit is Enabled"
    End If

End Sub

I changed some code. Hopefully this code could solve your case.
I'm newbie.
 

CEH

Curtis
Local time
Today, 13:44
Joined
Oct 22, 2004
Messages
1,187
Andy,
I copied your code into a form and subform I had.... worked fine....
Could there be another event being triggered....a OnDirty...an update.... something that is causing the problem?
 

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
Andy,
I copied your code into a form and subform I had.... worked fine....
Could there be another event being triggered....a OnDirty...an update.... something that is causing the problem?

I can't see anything... I've posted pictures of my two forms properties... the only one being called in oncurrent and this just sets some fields to be visible or not...

This is sooooo frustrating... :confused:
 

Attachments

  • database properties.zip
    84.1 KB · Views: 189

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
hiền muội;853731 said:
Hi Andy,
I try your case and have idea.
Code:
Private Sub cmdEdit_Click()

    If Me.AllowEdits = True Then
        Me.AllowEdits = False
        Me.AllowAdditions = False
        
        [i][color=red]Me!YourSubform.Locked = True[/color][/i]
        Me.cmdEdit.Caption = "Edit is Disabled"
    Else
        Me.AllowEdits = True
        Me.AllowAdditions = True
        [i][color=red]Me!YourSubform.Locked = False[/color][/i]
        Me.cmdEdit.Caption = "Edit is Enabled"
    End If

End Sub

I changed some code. Hopefully this code could solve your case.
I'm newbie.

Hi hiền muội,

Thanks for your post - my problems are with locking the main form not the subform - the subform appears to respond to the code and locks and unlocks fine the main form is the one that unlocks and then will not lock again...

:(
 

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
Hi Andy
Try Screen.ActiveForm

You could try disabling the controls as the default and then enabling with the following.



Code:
Dim ctl As Control
For Each ctl In Screen.ActiveForm
    If TypeOf ctl Is TextBox Then
        With ctl
        .Enabled = True
        End With
    End If
Next

I've given this a go with the following code:

Code:
Private Sub cmdEdit_Click()
Dim ctl As Control
For Each ctl In Screen.ActiveForm
 
    If Me.AllowEdits = True Then
        'Me.AllowEdits = False
        'Me.AllowDeletions = False
        'Me.AllowAdditions = False
        'Me!Invoice.Form.AllowEdits = False
        'Me!Invoice.Form.AllowDeletions = False
        'Me!Invoice.Form.AllowAdditions = False
        If TypeOf ctl Is TextBox Or ComboBox Then
        With ctl
        .Enabled = False
        Me.cmdEdit.Caption = "Edit is Disabled"
        End With
        End If
        End If

    If Me.AllowEdits = False Then
        'Me.AllowEdits = True
        'Me.AllowDeletions = True
        'Me.AllowAdditions = True
        'Me!Invoice.Form.AllowEdits = True
        'Me!Invoice.Form.AllowDeletions = True
        'Me!Invoice.Form.AllowAdditions = True
        If TypeOf ctl Is TextBox Or ComboBox Then
        With ctl
        .Enabled = True
        Me.cmdEdit.Caption = "Edit is Enabled"
        End With
        End If

End If
Next
End Sub

It's not working with run-time error 91: object varaiable or with block variable not set

Any ideas what's wrong with this code so i can see if will work when it runs??
 

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
No progress over the weekend... can anyone help??

:(
 

andy_dyer

Registered User.
Local time
Today, 19:44
Joined
Jul 2, 2003
Messages
806
Still can't get this working - anyone willing to look at my database??

In summary...

It is the enable/disable edit function I'm struggling with...

If you open the database, select "Xyz" client and then "View Existing Project(s)/Invoice(s)"

The form and subform is opened so you are unable to edit...

If you then click "Edit is Disabled" - you can then edit both main form and subform...

If you then click the same button which is now named "Edit is Enabled" - the subform goes back to no edits but the mainform can still be edited...

Arrrrgggghhhhhh! :mad:

If someone can spot the setting(s) or code that is causing this then will be very happy and very grateful!!

:D
 

Attachments

  • Demo Work in Progress.zip
    173 KB · Views: 181

Users who are viewing this thread

Top Bottom