Populating Items in Access - sql

I have two dropdown menus on an access form one for the main category and one for a subcategory. Based upon what you select in the main category, the sub-category dropdown should populate accordingly.
Now based on whats selected in the sub-category, a textbox should then populate.
Its been awhile since I've done any work with SQL and even then I didn't use MS access frequently so any help is much appreciated.
Thanks!

So if you have two comboboxes, Combobox1 and Combobox2, and a textbox, set them up like this:
Make the Combobox2 properties like this (change to suit your needs)
Row Source Type = Table/Query
Row Source = SELECT <Fields> FROM <Table> WHERE Crit_Field=[Combobox1]
Combobox1 should have and a Sub for the Change event which tells Combobox2 to populate:
Private Sub Combobox1_Change()
Me.Combobox2.Requery
End Sub
Combobox2 should also have a Sub for its Change event which tells the textbox to populate:
Private Sub Combobox2_Change()
' Stuff that populates the textbox
End Sub

Related

How to Open new form and insert ID from first form, Access [duplicate]

*****EDITED
So I have a form titled "NewInvoice". This form edits a table "Invoice" which contains the following; Invoice Number, Customer, Order Date. In this form I have a button which opens a subform titled "InvoiceItem". This edits a table with the same name that contains Invoice Number, Item Code, Dimensions, Etc.
Now currently, my button has an event procedure as follows.
Private Sub CreateInvoiceItem_Click()
DoCmd.OpenForm "InvoiceItem", OpenArgs:="InvoiceNumber"
End Sub
(The reason I am using a button and opening the form in a separate window is because I have cascading combo boxes in the sub form that become broken when I insert the sub form into the parent form)
Now where I am having trouble is setting the Form Load command. What I would like to occur is that the InvoiceNumber which is filled out in the Parent form auto fills in the sub form when the button is clicked.
Private Sub Form_Load()
**** NEEDED CODE****
End Sub
So try fixing the comboboxes as described in comment under question. Also, recommend code to requery the dependent combobox be in its GotFocus event. Keep in mind, cascading combobox with lookup alias will not work nice in continuous or datasheet form.
If you really want to pass value to independent form, the OpenArgs is a good approach.
Probably need to open the form to a new record row.
DoCmd.OpenForm "InvoiceItem", , , , acFormAdd, acDialog, Me!InvoiceNumber
Need code that makes sure the form is on a new record.
Private Sub Form_Load()
If Me.NewRecord Then Me!InvoiceNumber = Me.OpenArgs
End Sub
I find that the best way to do this is to add a Public sub to the form that you're opening and then pass whatever parameters you need to this function when you open the form. So to do what you're looking to do add a function like this to the form that you're opening;
Public Sub SetUpForm(InvoiceNumber as Long)
txtInvoiceNumber.Value = InvoiceNumber
End Sub
Where txtInvoiceNumber is the control on the form that you want to put the value into.
Then from your button;
DoCmd.OpenForm "InvoiceItem"
Forms!InvoiceItem.SetUpForm InvoiceNumber
This will pass your value for the invoice number to the control on the form that you're opening. This also gives you a lot more flexibility to control the process because you can pass more than one parameter to the sub and in the sub you can perform any number of tasks.

How can I populate empty rows in Excel using VBA programming?

I have created a drop-down menu and am trying to use VBA so that when certain menu items are selected, empty rows with populate beneath the menu item. The number of empty rows will be dependent on the menu item chosen.
Thanks for the help!
Try use "change" activeX event, which can be added to your sheet code where your comboBox are in.
i.e:
Something like this:
Private Sub comboBoxName_change()
Select case comboBoxName.list(comboBoxName.listIndex)
case "a"
'your code to populate rows
case "b"
'your code to populate rows
case "c"
'your code to populate rows
case [...]
end Select
end sub

Populating a dynamically added listbox from dynamically added combobox selection

The userform has 3 columns, a combobox (list of vendors), and listbox (list of vendors' product / service) and a textbox with the price of the selected product / service.
Example of menu (3rd column item not added)
The user presses a button to add a row, which populates within a scrollable frame a new row of the 3 columns. My goal is to have the listbox's options change according to the selection in the combobox on that row.
I have a listener (see my previous post on combobox(number)_change() to see how this works) which is the following ...
Private Sub ControlHandlerCollection_ComboBoxChange(ComboBox As MSForms.ComboBox)
MsgBox ComboBox.Name
End Sub
I wrote this to test if the change sub would work for ComboBox #87 for example. However, I don't know how to tell the same numbered listbox to change its list options according to the selection of ComboBox#
If I had a listener specifically for ComboBox1_Change() then I'd simply write the change for ListBox1 within it, but I'm writing this for a dynamic 'infinite' amount of ComboBoxes.
My current plan of attack would be to write within the sub in the code box above a block of code to parse the result of ComboBox.Name to grab the number as the result would be for example "ComboBox15" I could write a variable assigned to "ListBox" + (ComboBox.Name - "ComboBox") to call ListBox(number) however I'm not sure I can actually then call a variably based name of a listbox.
OK my father helped me come to this solution. It's a little static, but it works perfectly for what I need.
Had to edit the vendor data into multiple sheets and used the Define Name tool under Data to name the range. The listbox changes as per the selection of combobox.
Dim listBoxName As String
listBoxName = "myList" & ComboBox.Tag
Dim rangeName As String
rangeName = "company_1"
Select Case ComboBox.value
Case "Company 1"
rangeName = "company_1"
Case "Company 2"
rangeName = "company_2"
End Select
Dim listBox As Control
Set listBox = Me.Controls(listBoxName)
listBox.RowSource = rangeName
The above code was added to the private sub mentioned in the original thread.

How to pull text from the selected TextBox on a Report? VBA

I have a Report (rptCourses) which contains 170 TextBoxes which contain Course names, pulled from an access Table (tblCourses). When the user clicks on one of the Course Name TextBoxes, a form (frmRateCourse) loads containing a TextBox (txtboxCourseTitle), essentially a header, which ideally would contain the Course Name of the selected Report TextBox.
My question is: How can I take the text from the selected Report Textbox and input that text into the loaded Form's TextBox?
I apologize if this question already exists! And thank you, in advance, for any assistance!
Easiest is to pass the string as OpenArgs.
Private Sub CourseName_Click()
DoCmd.OpenForm "frmRateCourse", OpenArgs:=Me.CourseName.Value
End Sub
and in frmRateCourse:
Private Sub Form_Open(Cancel As Integer)
Me.lbTitle.Caption = Nz(Me.OpenArgs, "<no Title>")
End Sub

VBA ComboBox not displaying values [duplicate]

This question already has answers here:
My ComboBox doesn't display the values I've added in VBA
(3 answers)
Closed 5 years ago.
Trying to use the ComoboBox with VBA and it only displays 1 empty row when you click on the down arrow. I have never used ComboBox before and am still a beginner to VBA.
Private Sub ComboBoxT_Change()
ComboBoxT.AddItem "apple"
ComboBoxT.AddItem "orange"
ComboBoxT.AddItem "banana"
End Sub
Like Saagar said, they way that the code is currently written, it will only add the items into your combobox if there is a change in the combobox. The code that I would use would look like this:
Private Sub UserFormName_Activate()
ComboBoxT.AddItem "apple"
ComboBoxT.AddItem "orange"
ComboBoxT.AddItem "banana"
end sub
The easiest way to achieve this is in the user form creator, double click on the userform frame. This should show the code you would be working with, then from the top dropdown bars (the first one might say either "UserForm" or "General") you can select "Userform" in the first one and "Activate" in the second one. That should give you a good place to start, and the items you wanted added to the ComboBox should be right there, ready to go. Hope this helps :)
The Event you selected to add items to the ComboBox will only be fired if there is a change in the ComboBox. You need to enter this piece of code to add new items to the ComboBox in another event which is fired before you want to use the ComboBox.
Most cases, it is done at the Form_Load event, so that you have the items available inside the ComboBox by the time the Form is loaded completely.
Try the below
Private Sub Form_Load()
ComboBoxT.AddItem "apple"
ComboBoxT.AddItem "orange"
ComboBoxT.AddItem "banana"
End Sub