Hello there Good Day!

Anybody can help me or teach me the code on how to lock the text box on my program.

I have the navigation keys and add, edit, delete search command button. What i want is during the run time you cannot changed everything information in the text box without clicking the edit, delete, and add button. I have 10 text box object in my program with corresponding fields.

And this is the working code of my program...

Private Sub cmdadd_Click()
    Adodc1.Recordset.AddNew
End Sub

Private Sub cmdclose_Click()
    Dim h As String
    h = MsgBox("Are you sure do you want to exit the program?", vbInformation + vbYesNo, "Exit")
    If h = vbYes Then
    End
    End If
End Sub

Private Sub cmddel_Click()
    Adodc1.Recordset.Delete
    If Adodc1.Recordset.BOF Then
    Adodc1.Recordset.MoveFirst
    End If
End Sub

Private Sub cmdedit_Click()
    Adodc1.Recordset.Update
End Sub

Private Sub cmdfirst_Click()
    Adodc1.Recordset.MoveFirst
    Text11.Text = DateDiff("YYYY", Text8.Text, Now)
End Sub

Private Sub cmdlast_Click()
    Adodc1.Recordset.MoveLast
    Text11.Text = DateDiff("YYYY", Text8.Text, Now)
End Sub

Private Sub cmdnxt_Click()
    Adodc1.Recordset.MoveNext
    If Adodc1.Recordset.EOF Then
    Adodc1.Recordset.MoveLast
    MsgBox "End of file!", vbInformation
    End If
    Text11.Text = DateDiff("YYYY", Text8.Text, Now)
End Sub

Private Sub cmdopen_Click()
    Label1.Visible = True
    Label2.Visible = True
    Label3.Visible = True
    Label4.Visible = True
    Label5.Visible = True
    Label6.Visible = True
    Label7.Visible = True
    Label8.Visible = True
    Label9.Visible = True
    Label10.Visible = True
    Text1.Visible = True
    Text2.Visible = True
    Text3.Visible = True
    Text4.Visible = True
    Text5.Visible = True
    Text6.Visible = True
    Text7.Visible = True
    Text8.Visible = True
    Text9.Visible = True
    Text10.Visible = True
    Label13.Visible = True
    Combo1.Visible = True
    DataGrid1.Visible = True
    Image1.Visible = True
    Frame1.Visible = True
    Label15.Visible = True
    Text11.Visible = True
    
End Sub

Private Sub cmdprvs_Click()
    Adodc1.Recordset.MovePrevious
    If Adodc1.Recordset.BOF Then
    Adodc1.Recordset.MoveFirst
    MsgBox "Beginning of file!", vbInformation
    End If
    Text11.Text = DateDiff("YYYY", Text8.Text, Now)
End Sub

Private Sub cmdsearch_Click()
    On Error Resume Next
    If Combo1.ListIndex = -1 Then
        MsgBox "No field selected", vbInformation
        Exit Sub
        End If
    Dim dok As String
    dok = InputBox("Enter " & Combo1 & " to search", "Searching")
    Adodc1.Recordset.Close
    Dim dwi As String
    dwi = "Select * From warriorsinfo where " & Combo1.List(Combo1.ListIndex) & " = '" & dok & "'"
    Adodc1.RecordSource = dwi
    Adodc1.Refresh
    If Adodc1.Recordset.EOF Then
    MsgBox "No record found!", vbInformation
    End If
    Text11.Text = DateDiff("YYYY", Text8.Text, Now)
End Sub

Private Sub Form_Load()
    Adodc1.CommandType = adCmdText
    Text11.Text = DateDiff("YYYY", Text8.Text, Now)
End Sub

without clicking the edit, delete, and add button

That's the key to your solution. Write code in the click event of these buttons to enable the text box controls so that the control will respond to user activity.

TextBox1.Enabled = False ' Need to set this at design time or at an appropriate time during the execution of your program. This property set to false disables interactive use.
TextBox1.Enabled = True ' Set this property during a click event to allow the user to interact with the Text Box Control.

@hkdani

how about sir after some make changes of the text box info it will lock again? Because the code that you give me after unlocked the text box you cannot unlock it again unless you run the program again.

Hi,

To Make your Job Easy, Cut and Paste all the Controls (Labels and TextBoxes) from the form to a Frame Control.
Then Just make Visible True Or False (Or Enable / Disable) ONLY THE FRAME CONTROL.... No need to write to all the controls

Regards
Veena

after some make changes of the text box info it will lock again?

Again, you have pointed out the need to write code to respond to an event in your program.

You need to determine the appropriate time for locking the text boxes. This could be when the appropriate data has been entered: you might want to test for appropriate data.

If the user is using the add button to input the final result of his answer, then you could add code in the add button click event to disable the text boxes. And then use the edit button click event to enable the text boxes.

This would make sense. You can't edit if the text boxes are disabled. So enable them in the code for the edit button click event.

The user should be done editing when he clicks on the add button; so, disable the text boxes after posting the results.

Private Sub cmdEdit_Click()
     TextBox1.Enabled = True
End Sub

Private Sub cmdAdd_Click()
     ' Add code to post user's answer.
     ' Possible code to test user's answer before posting
     ' if user's answer is satisfactory --> Enabled = False  Else Enabled = False
     TextBox1.Enabled = False
     ' etc.
End Sub

@hkdani

by the way sir the goes like these?

Private Sub cmdedit_Click()
Adodc1.Recordset.Update
frame1.enabled = true
end sub

Private Sub cmdadd_Click()
Adodc1.Recordset.AddNew
frame1.enabled = true
End Sub

can you give the correct code add button? By the way i use frame enabled i just follow you instruction to make work easy :) I put all the object in the frame. Again that code that i made it you cannot unlocked it again after some changes. As i what i said i want automatically unlocked after do some changes.

As i what i said i want automatically unlocked after do some changes.

As the programmer, you must make the decision as to when you want the text box to be enabled or disabled. If you want the text box enabled after making the changes, then you have to write the code to make that happen.

I have only a vague idea of when that should be, since I'm not the author.

@hkdani

Thank you sir for the time and ideas...........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.