Forum Discussion

Kiswani87's avatar
Kiswani87
Copper Contributor
Sep 10, 2022

VBA Edit BTN

Dear Community 

I need help, I create a button to edit the field in the form, but when I make an edit on any field and then press in edit button nothing change, everything stays as is 

the code for all form is :

 

Private Sub btnadd_Click()
Dim lastrow As Long

lastrow = WorksheetFunction.CountA(Sheets("Employees").Range("A:A"))

usrform.ListBox.RowSource = ""

If usrform.txtname.Value = "" Or (usrform.optfemale = False And usrform.optmale = False) Or usrform.cmbnat.Value = "" Or usrform.txtphone.Value = "" Or usrform.txtvisa.Value = "" Or usrform.txtid.Value = "" Or usrform.txtjoin.Value = "" Or usrform.txtsalary.Value = "" Or usrform.cmbjob.Value = "" Or usrform.txtlabour.Value = "" Or usrform.txtcontract.Value = "" Or usrform.txtdateexp.Value = "" Or usrform.cmdcompany.Value = "" Then
MsgBox " Please Fill All Field "
Else

Sheets("Employees").Cells(lastrow + 1, 2).Value = usrform.txtname.Value

If usrform.optmale = True Then
Sheets("Employees").Cells(lastrow + 1, 3).Value = "Male"
Else
Sheets("Employees").Cells(lastrow + 1, 3).Value = "Female"
End If

Sheets("Employees").Cells(lastrow + 1, 4).Value = usrform.cmbnat.Value
Sheets("Employees").Cells(lastrow + 1, 5).Value = usrform.txtphone.Value
Sheets("Employees").Cells(lastrow + 1, 6).Value = usrform.txtvisa.Value
Sheets("Employees").Cells(lastrow + 1, 7).Value = usrform.txtid.Value
Sheets("Employees").Cells(lastrow + 1, 8).Value = usrform.txtjoin.Value
Sheets("Employees").Cells(lastrow + 1, 9).Value = usrform.txtsalary.Value
Sheets("Employees").Cells(lastrow + 1, 10).Value = usrform.cmbjob.Value
Sheets("Employees").Cells(lastrow + 1, 11).Value = usrform.txtlabour.Value
Sheets("Employees").Cells(lastrow + 1, 12).Value = usrform.txtcontract.Value
Sheets("Employees").Cells(lastrow + 1, 13).Value = usrform.txtdateexp.Value
Sheets("Employees").Cells(lastrow + 1, 14).Value = usrform.cmdcompany.Value

usrform.txtname.Value = ""
usrform.optmale = False
usrform.optfemale = False
usrform.cmbnat.Value = ""
usrform.txtphone.Value = ""
usrform.txtvisa.Value = ""
usrform.txtid.Value = ""
usrform.txtjoin.Value = ""
usrform.txtsalary.Value = ""
usrform.cmbjob.Value = ""
usrform.txtlabour.Value = ""
usrform.txtcontract.Value = ""
usrform.txtdateexp.Value = ""
usrform.cmdcompany.Value = ""
End If

usrform.ListBox.RowSource = "Emptable"

End Sub

Private Sub btnedit_Click()
Dim UsrIndx As Long
Dim RowNum As Long

UsrIndx = usrform.ListBox.ListIndex
RowNum = usrform.ListBox.List(UsrIndx, 0) + 1

If usrform.txtname.Value = "" Or (usrform.optfemale = False And usrform.optmale = False) Or usrform.cmbnat.Value = "" Or usrform.txtphone.Value = "" Or usrform.txtvisa.Value = "" Or usrform.txtid.Value = "" Or usrform.txtjoin.Value = "" Or usrform.txtsalary.Value = "" Or usrform.cmbjob.Value = "" Or usrform.txtlabour.Value = "" Or usrform.txtcontract.Value = "" Or usrform.txtdateexp.Value = "" Or usrform.cmdcompany.Value = "" Then
MsgBox " Please Fill All Field "
Else
Sheets("Employees").Cells(RowNum, 2).Value = usrform.txtname.Value
If usrform.optmale = True Then
Sheets("Employees").Cells(RowNum, 3).Value = "Male"
Else
Sheets("Employees").Cells(RowNum, 3).Value = "Female"
End If
Sheets("Employees").Cells(RowNum, 4).Value = usrform.cmbnat.Value
Sheets("Employees").Cells(RowNum, 5).Value = usrform.txtphone.Value
Sheets("Employees").Cells(RowNum, 6).Value = usrform.txtvisa.Value
Sheets("Employees").Cells(RowNum, 7).Value = usrform.txtid.Value
Sheets("Employees").Cells(RowNum, 8).Value = usrform.txtjoin.Value
Sheets("Employees").Cells(RowNum, 9).Value = usrform.txtsalary.Value
Sheets("Employees").Cells(RowNum, 10).Value = usrform.cmbjob.Value
Sheets("Employees").Cells(RowNum, 11).Value = usrform.txtlabour.Value
Sheets("Employees").Cells(RowNum, 12).Value = usrform.txtcontract.Value
Sheets("Employees").Cells(RowNum, 13).Value = usrform.txtdateexp.Value
Sheets("Employees").Cells(RowNum, 14).Value = usrform.cmdcompany.Value
End If

End Sub

 

Private Sub ListBox_Click()
Dim userindex As Long
userindex = usrform.ListBox.ListIndex
usrform.txtname.Value = usrform.ListBox.List(userindex, 1)
If usrform.ListBox.List(userindex, 2) = " Male " Then
usrform.optmale = True
usrform.optfemale = False
Else
usrform.optfemale = True
usrform.optmale = False
End If
usrform.cmbnat.Value = usrform.ListBox.List(userindex, 3)
usrform.txtphone.Value = usrform.ListBox.List(userindex, 4)
usrform.txtvisa.Value = usrform.ListBox.List(userindex, 5)
usrform.txtid.Value = usrform.ListBox.List(userindex, 6)
usrform.txtjoin.Value = usrform.ListBox.List(userindex, 7)
usrform.txtsalary.Value = usrform.ListBox.List(userindex, 😎
usrform.cmbjob.Value = usrform.ListBox.List(userindex, 9)
usrform.txtlabour.Value = usrform.ListBox.List(userindex, 10)
usrform.txtcontract.Value = usrform.ListBox.List(userindex, 11)
usrform.txtdateexp.Value = usrform.ListBox.List(userindex, 12)
usrform.cmdcompany.Value = usrform.ListBox.List(userindex, 13)

usrform.btnedit.Visible = True
usrform.btndelete.Enabled = True
usrform.btnclear.Caption = "Return"

 

need help to know where is the problem 

 

  • GeorgieAnne's avatar
    GeorgieAnne
    Iron Contributor

    HelloKiswani87 

    Oh Dear! I am afraid your post is confusing enough to warrant some clarifications:

    A) What does " nothing change" mean? Is something supposed to change? From what to what? When should it change.

    B) I would make the checks of all fields being filled at the back end of the Userform send data back to the worksheet.

    C) Which button runs the code that is "failing"?

     

    Georgie

    • Kiswani87's avatar
      Kiswani87
      Copper Contributor
      Dear Georgie
      I create a button to edit the fields, so when I want to change for ex the name ( from Ramzi to Ahmad) I change it and then press the button edit, nothing happens, the field name didn't take the change, it will stay ( Ramzi )

Resources