Auto-populate Information in a Form

erind

New Member
Joined
Sep 22, 2009
Messages
32
I'm trying to figure out how to tie one text box to another, to limit the amount of entering needed and errors when typing. Everytime I add in a new row I want some of the information to populate based on the information entered in the first (or second) column. The first Column is numbers, the second column is names and each time I create a row and choose a number, the name needs to be tied to that number (1 will always equal A, 2=B, 3=C etc.) Is there a way that I can set the options so that everytime I put 1 in the first box, the next will automatically populate as A?

And from there, I need the third column to give a drop down list of the items that are connected to the first two; there are more options for this, about 5 per number/name, but the descriptions will be different for each, so I just want a dropdown of those that are relevant to the number/name columns in the table. I can get the dropdown boxes, but I haven't been able to make them autopopulate or limit the choices.

Table is done, relationships are establised, how do I make the rest work? Thanks!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

revans

Well-known Member
Joined
Apr 5, 2010
Messages
576
In your first column make it a combo box with two columns: the IDNumber and the Name. Set the combo box's bound column to 1 (the first column). The name column will just be for "show" to remind the user what the id column "really" is. I would set the After Update event of the combo to set the text box's value like this (change the names as necessary). Note that the column count is one-based in the property sheet and zero based in vba (dumb I know).

Code:
Private Sub MyCombo_AfterUpdate()
Me.MyText = Me.MyCombo.Column(1) ' (1) is the second column in vba
Call RefreshCombo ' See below why this is here
End Sub

Then your third column combo will reference these two fields. It sounds like you've got your form set up as a grid. You'll want to set the combo box dynamically. Create a sub to do this behind your form then call this sub in various places: the first column After Update event, the row's On Current Event, and perhaps others as necessary.

Code:
Private Sub RefreshCombo
me.MyOtherCombo.RowSource= "SELECT * FROM MyTable WHERE [FirstField]='" & Me.MyText & "' AND [OtherField]='" & Me.MyCombo.Column(1)  & "'"
End Sub

Your rowsource SQL will probably look different from this but the point is you'll reference the two controls in the SQL. You will want to check for nulls (empty fields), strings with apostrophes, and other standard dynamic sql issues as normal. Feel free to ask about that stuff as it comes up.

hth,

Rich
 

erind

New Member
Joined
Sep 22, 2009
Messages
32
Thanks, that helped, so now I have found a way to get the columns to auto-populate, but for the list box, they are not refreshing when I switch between subforms. I have it set-up like this:
Form Header has the Area Code that I filter to get the entries for a certain area. In the sub-form the area code is repeated, and then I have a list box that drops down the sub-areas within it to choose from, linked to a query where you enter the area code and it displays the sub-choices, which works fine. But when I filter to a new area code the list box does not update to the new area code choices tied to them for this area code, so instead of choosing from this areas sub-choices, I am choosing from the previous areas sub-choices. The only way I have found to refresh this is to go back to design view and then back to form view, which refreshes the list box properly, but I'd rather not have to go back and forth everytime I filter.

Any more ideas?
 

revans

Well-known Member
Joined
Apr 5, 2010
Messages
576
After you set the combo's rowsource, you can try something like

MyCombo.Requery

I've never been sure when to requery, except when I needed it :)

hth,

Rich
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,398
Messages
5,831,407
Members
430,065
Latest member
ctornabe

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top