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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

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
Back
Top