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!
 

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,731
Messages
5,513,068
Members
408,935
Latest member
Jmtramos

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top