Edit listbox from subform.

Klingest

Registered User.
Local time
Today, 16:33
Joined
Aug 11, 2017
Messages
11
Hi guys

I have a form with the name "frmMainMenu". In this form i have a listbox with the name "LstUsers", and a button with the name "edit".

The listbox contains the records from a table named "tblUsers". In this table I have the following fields:

"Name"
"Last name"
"User name"

When I click the button a subform named "frmEditUsers" is opened.
I do this with the command:

DoCmd.OpenForm "frmEditUsers"

The subform has the following textfields:

"txtName"
"txtLastName"
"txtUserName"

What I want:

If the user marks a record, with the mouse, from the listbox "LstUsers" and presses the "Edit" button the subform "frmEditUsers" shall open and the data from the selected record shall be shown in the the textfields shall be shown respecively as follows:

Name -> "txtName"
Last name -> "txtLastName"
User name -> "txtUserName"

The user can now edit the text in the textfields, and then when he presses another button, the data in the table "tblUser" shall be updated and the updated data shall be shown in the listbox "tblListbox"

How can I do that?

Thanks for you time
 
add code to frmEditUsers, on its Load Event
and check if there is a selected item on
form's frmMainMenu listbox:

Code:
Private Sub Form_Load()
Dim frm As Form
On Error Goto Err_Handler
Set frm =Forms!frmMainMenu
If frm!lstUsers.ListIndex <> -1 Then
	'there is a selection in the Listbox
	'set fields to selected item
	Me.RecordSet.FindFirst "Name=" & Chr(34) & frm!LstUsers.Column(0) & Chr(34)

	Me.txtName = frm!LstUsers.Column(0)
	Me.txtLastName = frm!LstUsers.Column(1)
	Me.txtLastName = frm!LstUsers.Column(2)
End If

Exit_Sub:
	Set frm=Nothing
	Exit Sub
Err_Handler:
	Msgbox Err.Number & ": " & Err.Description
	Resume Exit_Sub
End Sub

to update the LstUsers after making changes
to the record in frmEditUsers form, add this
code to frmEditUsers AfterUpdate Event:

Code:
Private Sub Form_AfterUpdate()
Dim Index As Integer
On Error Resume Next
' save this
Index = Forms!frmMainMenu!lstUsers.ListIndex
' requery the List
Forms!frmMainMenu!lstUsers.Requery
' return to same Index
Forms!frmMainMenu!lstUsers=Forms!frmMainMenu!lstUsers.ItemData(Index)
End Sub
 

Users who are viewing this thread

Back
Top Bottom