PDA

View Full Version : Solved: Is it possible to write Vba code on a command button to edit records?



wedd
09-26-2011, 05:40 AM
Hi experts, I've created a comman button were I would like to edit records...the records or text fields are set are set to prevent a user from accidentally over writing existing data through a typo etc...I've locked the text fields; however, I would like the user to click on the command button to edit the data. I've enclosed code I've written below, but it doesn't seem to work...Can this be done? If so, how can this be done? Would you have an any examples of how this can be done?





Private Sub EditRecords_Click()
Dim cap As String
cap = Me.CmdEditRecords
Select Case cap
Case "Edit"
With Me
.AllowEdits = True
.CmdEdit.Caption = "Lock"
.CmdEdit.ForeColor = 128
.CmdEdit.FontBold = True
.Refresh
End With
Case "Lock"
With Me
.AllowEdits = True
.CmdEdit.Caption = "Edit"
.CmdEdit.ForeColor = 0
.CmdEdit.FontBold = False
.Refresh

End Sub



Thanks for your contributions :friends:

Norie
09-27-2011, 04:29 AM
What's the name of the command button?

Is it EditRecords or CmdEditRecords?

wedd
09-27-2011, 05:05 AM
Hi Norie, it is called CmdEditRecords.

Norie
09-27-2011, 05:41 AM
Why is the sub called this then?



Private Sub EditRecords_Click()

Are you calling this sub from the click event of the command button CmdEditRecords?

wedd
09-27-2011, 06:04 AM
Yes, in the click event of my "edit" command button, I would like to set properties to Enabled = True and Locked = False

wedd
09-27-2011, 06:05 AM
Hi, Norie! Yes, in the click event of my "edit" command button, I would like to set properties to Enabled = True and Locked = False

wedd
09-27-2011, 06:07 AM
Those changes will allow the user to edit those text boxes bound to the current record. Once the user navigates to a different record, the On Current event will fire and make those text boxes uneditable again.

wedd
09-30-2011, 08:34 AM
Hi Norie, I solved it thanks! I used this code and it worked:

Private Sub CmdEdit_Click()
Dim cap As String
cap = Me.CmdEdit.Caption
Select Case cap
Case "Edit"
With Me
.AllowEdits = True
.CmdEdit.Caption = "Lock"
.CmdEdit.ForeColor = 128
.CmdEdit.FontBold = True
.Refresh
End With
Case "Lock"
With Me
.AllowEdits = False
.CmdEdit.Caption = "Edit"
.CmdEdit.ForeColor = 0
.CmdEdit.FontBold = False
.Refresh
End With
End Select
End Sub