0
votes

I realise this question has been asked before and I have tried numerous suggestions, but I guess I am just too much of a novice when it comes to VBA coding.

Here is my scenario: I have an Access 2007 "Application" with a few forms. On the main form, I have 2 text boxes. For simplicity let's call them textbox1 and textbox2.

textbox1 is used to enter a style code. a button on the main form then opens another in modal/dialog mode and runs a query with the style code as the where clasue. the modal popup gives me a list box which is populated from the database based on the query that was passed. I then need to select one of the products in the list and upon closing the popup, populate textbox2 with the brand (column2 in the list from the popup) number.

Please remember that I am a novice. Any help would be greatly appreciated.

1
"I realise this question has been asked before" - then why are you asking the question again? -- Duplicate questions will usually just be removed from Stack Overflow. - Kmeixner

1 Answers

0
votes

I would put a button on the second form, and in the _Click() function, put something like this

   If ListBox1.ListIndex = -1 Then
       MsgBox "Nothing Selected!"
       Exit Sub
   End If
   UserForm1.TextBox1.Text = ListBox1.List(ListBox1.ListIndex)
   Unload Me

Where ListBox1 is the list box containing the content you need the user to select from, where UserForm1 is the name of the calling form, and TextBox1 is the name.

Explanation The ListIndex property of the list box returns the index of the selected list item. If nothing is selected, it returns a -1. To reference an item on one from from another, you reference the form, the object and the property.

In The example I gave, the form is UserForm1, the object is TextBox1 and the Property is Text. While typing, intellisense should auto complete the object and properties once you type the period.