Edit and Locking fields/textboxes?

Happle
01-09-2004, 10:50 AM
Hi everyone,

This is what I am trying to achieve:

1. Allow users to edit a record when pressing the 'Edit' button AND

2. Not allow them to change the fields at any other time.

Right, I have found the 'Locked' option for the textboxes, which when are set to true do not allow the user to change the contents of the field. So this achieves No.2.

I have the following code for editing the textboxes and updating the database:

Private Sub cmdEditCustomer_Click()
Edit$ = "Edit the details and then press the left arrow button"
reply = MsgBox(Edit$, vbOKCancel, "Edit Record")
If reply = vbOK Then
datCustomers.Recordset.Edit
datCustomers.Recordset.Fields("Title").Value = txtTitle.Text
datCustomers.Recordset.Fields("ContactFirstName").Value = txtForename.Text
datCustomers.Recordset.Fields("Maiden").Value = txtMaiden.Text
datCustomers.Recordset.Fields("Married Name").Value = txtMarriedName.Text
datCustomers.Recordset.Fields("Wedding Date").Value = txtWeddingDate.Text
datCustomers.Recordset.Fields("Home Phone Number").Value = txtHomeTel.Text
datCustomers.Recordset.Fields("Work Number").Value = txtWorkTel.Text
datCustomers.Recordset.Fields("EmailAddress").Value = txtemail.Text
datCustomers.Recordset.Fields("StreetAddress").Value = txtStreet.Text
datCustomers.Recordset.Fields("Town").Value = txtTown.Text
datCustomers.Recordset.Fields("County").Value = txtCounty.Text
datCustomers.Recordset.Fields("PostalCode").Value = txtPostCode.Text
datCustomers.Recordset.Fields("Country").Value = txtCountry.Text
datCustomers.Recordset.Fields("Notes").Value = txtNotes.Text
datCustomers.Recordset.Update
End If
End Sub

When I run the program, I can't change any of the locked text boxes at anytime even when I press the edit button AND I can change the other unlocked textboxes at anytime and this will save to the database.

I don't want the users to be able to change the contents of the textboxes unless they have pressed the edit button..........sorry if I have confused you all!

Can anyone help, thanks

Happle

Dennis DVR
01-09-2004, 10:53 AM
Hi everyone,

This is what I am trying to achieve:

1. Allow users to edit a record when pressing the 'Edit' button AND

2. Not allow them to change the fields at any other time.

Right, I have found the 'Locked' option for the textboxes, which when are set to true do not allow the user to change the contents of the field. So this achieves No.2.

I have the following code for editing the textboxes and updating the database:

Private Sub cmdEditCustomer_Click()
Edit$ = "Edit the details and then press the left arrow button"
reply = MsgBox(Edit$, vbOKCancel, "Edit Record")
If reply = vbOK Then
datCustomers.Recordset.Edit
datCustomers.Recordset.Fields("Title").Value = txtTitle.Text
datCustomers.Recordset.Fields("ContactFirstName").Value = txtForename.Text
datCustomers.Recordset.Fields("Maiden").Value = txtMaiden.Text
datCustomers.Recordset.Fields("Married Name").Value = txtMarriedName.Text
datCustomers.Recordset.Fields("Wedding Date").Value = txtWeddingDate.Text
datCustomers.Recordset.Fields("Home Phone Number").Value = txtHomeTel.Text
datCustomers.Recordset.Fields("Work Number").Value = txtWorkTel.Text
datCustomers.Recordset.Fields("EmailAddress").Value = txtemail.Text
datCustomers.Recordset.Fields("StreetAddress").Value = txtStreet.Text
datCustomers.Recordset.Fields("Town").Value = txtTown.Text
datCustomers.Recordset.Fields("County").Value = txtCounty.Text
datCustomers.Recordset.Fields("PostalCode").Value = txtPostCode.Text
datCustomers.Recordset.Fields("Country").Value = txtCountry.Text
datCustomers.Recordset.Fields("Notes").Value = txtNotes.Text
datCustomers.Recordset.Update
End If
End Sub

When I run the program, I can't change any of the locked text boxes at anytime even when I press the edit button AND I can change the other unlocked textboxes at anytime and this will save to the database.

I don't want the users to be able to change the contents of the textboxes unless they have pressed the edit button..........sorry if I have confused you all!

Can anyone help, thanks

Happle

I don't see any unlocking on this code.
where the txtCountry.Locked = False?

Happle
01-09-2004, 11:02 AM
Sorry.....not sure if I explained it clearly.

The 'Locked' property I have set in the Properties window of the individual textboxes in VB - not in the code.

Should I lock all of the them in the Properties window and then in the code before each line of code 'unlock' each field?????

As you can tell I am new to all of this locking stuff!

Thanks
Happle

Dennis DVR
01-09-2004, 11:06 AM
Sorry.....not sure if I explained it clearly.

The 'Locked' property I have set in the Properties window of the individual textboxes in VB - not in the code.

Should I lock all of the them in the Properties window and then in the code before each line of code 'unlock' each field?????

As you can tell I am new to all of this locking stuff!

Thanks
Happle

you should lock and unlock the textbox on your code when you want to change the value locked property.

or why don't you just put all your textboxes on a container like frame and disable the frame if you don't want the user to edit the textboxes otherwise enable it.

Happle
01-09-2004, 11:07 AM
Sorry.....not sure if I explained it clearly.

The 'Locked' property I have set in the Properties window of the individual textboxes in VB - not in the code.

Should I lock all of the them in the Properties window and then in the code before each line of code 'unlock' each field?????

As you can tell I am new to all of this locking stuff!

Thanks
Happle

Maybe lock is the wrong word - I just don't want them to be able to change the data in the textboxes, only if the Edit button has been pressed............Sorry if this doesn't make sense!

Happle

00100b
01-09-2004, 11:08 AM
You could handle this in the code by setting the Locked property = False, like in the Edit button's Click event.

Private Sub cmdEdit_Click()

' Allow the user to modify the txtForeName text box.
Me.txtForeName.Locked = False
' etc for each text box that you want to unlock

' Do your other code.

End Sub

And then after the record has been saved, you can relock them by setting the Locked property = True.

Dennis DVR
01-09-2004, 11:09 AM
or if you want? read post number 4

Happle
01-09-2004, 05:32 PM
You could handle this in the code by setting the Locked property = False, like in the Edit button's Click event.

Private Sub cmdEdit_Click()

' Allow the user to modify the txtForeName text box.
Me.txtForeName.Locked = False
' etc for each text box that you want to unlock

' Do your other code.

End Sub

And then after the record has been saved, you can relock them by setting the Locked property = True.

Hi,
Thanks for the advice. I have set the txt.Locked = True like so:

Private Sub Form_Load()
txtTitle.Locked = True
txtForename.Locked = True
........
End Sub

which does indeed lock all the textboxes so that when the form loads no one can edit the textboxes.
I have then under the Edit button code, unlocked the textboxes, got the edit code and then have set the locked property to true again:

Private Sub cmdEditCustomer_Click()
Edit$ = "Edit the details and then press the left arrow button"
reply = MsgBox(Edit$, vbOKCancel, "Edit Record")
If reply = vbOK Then
txtTitle.Locked = False
txtForename.Locked = False
.
.
.
datCustomers.Recordset.Edit
datCustomers.Recordset.Fields("Title").Value = txtTitle.Text
datCustomers.Recordset.Fields("ContactFirstName").Value = txtForename.Text
.
.
datCustomers.Recordset.Update
txtTitle.Locked = True
txtForename.Locked = True
.
.
End If
End Sub


Now, the problem is that when I press the edit button the textboxes still stay locked! Is it just a problem with my coding? Have I got it the right way round? I have tried moving it about......but to no success!

Thanks
Happle

00100b
01-09-2004, 06:42 PM
Are you sure that you hadn't set the Enabled property for the text boxes in the form during design-time to False?

Happle
01-09-2004, 07:06 PM
Are you sure that you hadn't set the Enabled property for the text boxes in the form during design-time to False?

Hi,
I have checked these properties and they are all true???? Any other ideas?

Happle

00100b
01-09-2004, 07:15 PM
Since you aren't declaring any variables (at least that I can tell from your code snippet) then you probably don't have Option Explicit set. That means that you can have a variable or reference name mistyped and it might not generate an error.

Go to the very top of the form's code module and add

Option Explicit

Once this is set, then you will need to visit each procedure and declare all of the variables used. At this point, I would recommend that you use the appropriate data types.

For example, in your cmdEditCustomer_Click event procedure, you would need to declare the variables for Edit$ and reply.

Dim strEdit As String
Dim intReply As Integer


After you have explicitly declared all variables, run the project and fix any run-time errors that occur due to mistyped variable/reference names.

Happle
01-10-2004, 05:33 AM
Since you aren't declaring any variables (at least that I can tell from your code snippet) then you probably don't have Option Explicit set. That means that you can have a variable or reference name mistyped and it might not generate an error.

Go to the very top of the form's code module and add

Option Explicit

Once this is set, then you will need to visit each procedure and declare all of the variables used. At this point, I would recommend that you use the appropriate data types.

For example, in your cmdEditCustomer_Click event procedure, you would need to declare the variables for Edit$ and reply.

Dim strEdit As String
Dim intReply As Integer


Hi,
Thanks for the reply - I did what you said and they still just stay locked, even when I press the Edit button. I can't understand this as I have set the Locked property to False in the cmdEditCutsomer_Click event, for some reason the code under Private Sub Form_load() is overiding everything......... :confused:

Thanks for your suggestions so far - but anymore ideas?????

Happle
After you have explicitly declared all variables, run the project and fix any run-time errors that occur due to mistyped variable/reference names.

Happle
01-10-2004, 05:35 AM
Since you aren't declaring any variables (at least that I can tell from your code snippet) then you probably don't have Option Explicit set. That means that you can have a variable or reference name mistyped and it might not generate an error.

Go to the very top of the form's code module and add

Option Explicit

Once this is set, then you will need to visit each procedure and declare all of the variables used. At this point, I would recommend that you use the appropriate data types.

For example, in your cmdEditCustomer_Click event procedure, you would need to declare the variables for Edit$ and reply.

Dim strEdit As String
Dim intReply As Integer


After you have explicitly declared all variables, run the project and fix any run-time errors that occur due to mistyped variable/reference names.

Hi,
Thanks for the reply - I did what you said and they still just stay locked, even when I press the Edit button. I can't understand this as I have set the Locked property to False in the cmdEditCutsomer_Click event, for some reason the code under Private Sub Form_load() is overiding everything.........

Thanks for your suggestions so far - but anymore ideas?????

Happle

Dennis DVR
01-10-2004, 05:52 AM
can you show us the code of you form_load event and the cmdEditClick?

Happle
01-10-2004, 06:03 AM
can you show us the code of you form_load event and the cmdEditClick?

Yeah sure here it is: (Its probably not efficient or absolutely the correct way of doing things but I am new to all of this) ;)

Option Explicit
Private Sub Form_Load()
txtTitle.Locked = True
txtForename.Locked = True
txtMaiden.Locked = True
txtMarriedName.Locked = True
txtWeddingDate.Locked = True
txtHomeTel.Locked = True
txtWorkTel.Locked = True
txtemail.Locked = True
txtStreet.Locked = True
txtTown.Locked = True
txtCounty.Locked = True
txtPostCode.Locked = True
txtCountry.Locked = True
txtNotes.Locked = True
End Sub

Private Sub cmdEditCustomer_Click()
Dim strEdit As String
Dim reply As Integer
strEdit = "Edit the details and then press the left arrow button"
reply = MsgBox(strEdit, vbOKCancel, "Edit Record")
If reply = vbOK Then
txtTitle.Locked = False
txtForename.Locked = False
txtMaiden.Locked = False
txtMarriedName.Locked = False
txtWeddingDate.Locked = False
txtHomeTel.Locked = False
txtWorkTel.Locked = False
txtemail.Locked = False
txtStreet.Locked = False
txtTown.Locked = False
txtCounty.Locked = False
txtPostCode.Locked = False
txtCountry.Locked = False
txtNotes.Locked = False
datCustomers.Recordset.Edit
datCustomers.Recordset.Fields("Title").Value = txtTitle.Text
datCustomers.Recordset.Fields("ContactFirstName").Value = txtForename.Text
datCustomers.Recordset.Fields("Maiden").Value = txtMaiden.Text
datCustomers.Recordset.Fields("Married Name").Value = txtMarriedName.Text
datCustomers.Recordset.Fields("Wedding Date").Value = txtWeddingDate.Text
datCustomers.Recordset.Fields("Home Phone Number").Value = txtHomeTel.Text
datCustomers.Recordset.Fields("Work Number").Value = txtWorkTel.Text
datCustomers.Recordset.Fields("EmailAddress").Value = txtemail.Text
datCustomers.Recordset.Fields("StreetAddress").Value = txtStreet.Text
datCustomers.Recordset.Fields("Town").Value = txtTown.Text
datCustomers.Recordset.Fields("County").Value = txtCounty.Text
datCustomers.Recordset.Fields("PostalCode").Value = txtPostCode.Text
datCustomers.Recordset.Fields("Country").Value = txtCountry.Text
datCustomers.Recordset.Fields("Notes").Value = txtNotes.Text
datCustomers.Recordset.Update
txtTitle.Locked = True
txtForename.Locked = True
txtMaiden.Locked = True
txtMarriedName.Locked = True
txtWeddingDate.Locked = True
txtHomeTel.Locked = True
txtWorkTel.Locked = True
txtemail.Locked = True
txtStreet.Locked = True
txtTown.Locked = True
txtCounty.Locked = True
txtPostCode.Locked = True
txtCountry.Locked = True
txtNotes.Locked = True
End If
End Sub

Any ideas???

Thanks
Happle

Dennis DVR
01-10-2004, 06:09 AM
you cannot change the textbox in that way
the unlocking code should be place when the user wants to edit the data not to save them.
and another thing your locking it again in your code so the code that you have shown is just to unlock the textboxes save it to the table and lock it again.

Dennis DVR
01-10-2004, 06:14 AM
you should have a save button for your saving.
don't include your saving on your cmdEditClick

this code should be in your save button


datCustomers.Recordset.Edit 'test it whether it is add or edit
datCustomers.Recordset.Fields("Title").Value = txtTitle.Text
datCustomers.Recordset.Fields("ContactFirstName").Value = txtForename.Text
datCustomers.Recordset.Fields("Maiden").Value = txtMaiden.Text
datCustomers.Recordset.Fields("Married Name").Value = txtMarriedName.Text
datCustomers.Recordset.Fields("Wedding Date").Value = txtWeddingDate.Text
datCustomers.Recordset.Fields("Home Phone Number").Value = txtHomeTel.Text
datCustomers.Recordset.Fields("Work Number").Value = txtWorkTel.Text
datCustomers.Recordset.Fields("EmailAddress").Value = txtemail.Text
datCustomers.Recordset.Fields("StreetAddress").Value = txtStreet.Text
datCustomers.Recordset.Fields("Town").Value = txtTown.Text
datCustomers.Recordset.Fields("County").Value = txtCounty.Text
datCustomers.Recordset.Fields("PostalCode").Value = txtPostCode.Text
datCustomers.Recordset.Fields("Country").Value = txtCountry.Text
datCustomers.Recordset.Fields("Notes").Value = txtNotes.Text
datCustomers.Recordset.Update
txtTitle.Locked = True
txtForename.Locked = True
txtMaiden.Locked = True
txtMarriedName.Locked = True
txtWeddingDate.Locked = True
txtHomeTel.Locked = True
txtWorkTel.Locked = True
txtemail.Locked = True
txtStreet.Locked = True
txtTown.Locked = True
txtCounty.Locked = True
txtPostCode.Locked = True
txtCountry.Locked = True
txtNotes.Locked = True

Dennis DVR
01-10-2004, 06:26 AM
perhaps you're confuse what i'm telling you is that you can't unlock the textboxes and lock it again in the sametime or in the same command button. so you should only lock it when the data is already saved so when you click the edit button the textboxes should be unlocked so that user can edit the textboxes and if the user wants to save the record he/she should click on the save button and from your save button that's where your locking code goes. read my last post

Happle
01-10-2004, 08:49 AM
perhaps you're confuse what i'm telling you is that you can't unlock the textboxes and lock it again in the sametime or in the same command button. so you should only lock it when the data is already saved so when you click the edit button the textboxes should be unlocked so that user can edit the textboxes and if the user wants to save the record he/she should click on the save button and from your save button that's where your locking code goes. read my last post

Ahhhhh, I now understand! I've done what you have suggested and it works great! Thanks very much for all your help.

Thanks
Happle :D

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum