1

I have a table that lists Departments and Passwords in SQL Server. I am creating a Userform UI so that users are able to view list, amend an entry or delete an entry.

I have made it look like this:

enter image description here

I managed to set up the ADODB connection when a user opens the form, it would automatically load the list using:

Private Sub Userform_Activate()

My rational is that I want a user to select from the list box, it would display the Department and Password in textbox1 and textbox2, respectively.

The user then can either Update the record or Delete the record. This would be a very straight-forward SQL operation.

What I am hitting a road block with at the moment is actually getting the data displayed in Textbox1 and Textbox2.

Code

Private Sub Select_Click()
If Me.ListBox1.Selected(i) Then
    Me.TextBox1 = Me.ListBox1.List(i, 0)
    Me.TextBox2 = Me.ListBox1.List(i, 1)
End If
End Sub

Any advise would be appreciated.

Furthermore, is my logic sound? Or is there a better way of doing things?

I know this is 2-in-1 question, but thanks!

3
  • 1
    Private Sub Select_Click() this looks like a separate procedure that you wrote. Tie the code to the on click event of the actual listbox. So Private Sub Listbox1_Click() Commented Jun 25, 2018 at 16:52
  • That happens when a user clicks "Select" button. This will not work? Commented Jun 25, 2018 at 17:12
  • It does not work even on what you suggested... Commented Jun 25, 2018 at 17:13

1 Answer 1

1

Just tested and this works.

Private Sub ListBox1_Click()

    TextBox1.Value = ListBox1.List(ListBox1.ListIndex, 0)
    TextBox2.Value = ListBox1.List(ListBox1.ListIndex, 1)

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.