469,301 Members | 2,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,301 developers. It's quick & easy.

Changing record source based on combo box

Teresa Kline
I have a form with a combo box. In the combo box is a list of tables in my database. I want to be able to select a table from my combo box and based on that selection open another form that uses that table as a record source.

I want to use the same form each time but change the record source based on the table I select from my combo box.

Thanks for any help.

Mar 27 '12 #1
8 8037
32,173 Expert Mod 16PB
That's possible if a little risky, but did you have a question?
Mar 27 '12 #2
Yes my question was how do I do this either in VBA or in the form?


Mar 28 '12 #3
32,173 Expert Mod 16PB
You seem to be missing the point somewhat, but maybe if I answer that question, then you'll understand how a question with more of the basic information included, can yield you a more helpful answer.

The answer to your question is that you design a form that can take any of the tables to be used. You populate a Combo/ListBox with a list of all these tables, and when the form is opened (after selecting an item from the list or from behind a Command Button) you change this form's .RecordSource property to a string value reflecting the name of the chosen table. This change can be done in the Open event procedure of the new form without the form trying to open any previously set record source.

The fact that you're even asking this question at all, leads me to suspect that you have a number of tables with the same (or very similar) design. This is indicative (a strong indicator if not entirely conclusive) of a database design which is not normalised (Database Normalisation and Table Structures). I strongly recommend that you stop now and consider the advisability of proceeding along this course. Unless a database is really quite trivial, normalisation is a must have. Probably the biggest cause of database problems is the lack of these concepts in databases. My advice is certainly that this should be dealt with as a matter of priority.

For now though, this is an answer (albeit fairly basically stated without named references) to your question.
Mar 28 '12 #4
I'm sorry that I didn't give enough information in my original post...I have a process that runs and selects a group of people based on their location from SAP. The selection is then written to a table in SAP that is named for example: tbllocation103302012. Then a second query is run for a different location and that table is named...tbllocation203302012..and so on. At this point various users will need to go into the database and update their specific table based on the location they service so I need for them to be able to select a table from the combo box that contains the list of tables in the database and open a standard form for updating but with their selected table. All the fields are the same in every table created and the form contains the same fields as well. The table names however will change and will be deleted and recreated with different locations. there are over a hundred locations possible and the date at the end will change. I have created a query that will pull the list of tables in my database for my combo box...and I have a form but i just can't seem to get the form recordsource to pull the variable from the combo box. Does this make sense?

Thanks so much.

Mar 28 '12 #5
759 512MB
NeoPa already answer to your "how to" question.
This post is only for more details.
So you have (at least) 3 possibilities:

1) From the main form pass the name of table to the second form using OpenArgs:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "SecondFormName", , , , , , Me.CmbTables
In the second form (_Open event) you will have:
Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = Me.OpenArgs

2)Also in _Open event (second form) place this code:
Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = Form_MainFormName.CmbTables
Note please that Form_MainFormName is the main form name as you see it in VB editor (Project Explorer) not the name you see in Navigation Pane.

3) Create a Public variable in a normal module:
Expand|Select|Wrap|Line Numbers
  1. Public TableToOpen As String
Assign value to this variable then open the second form:
Expand|Select|Wrap|Line Numbers
  1. TableToOpen = Me.CmbTables
  2. DoCmd.OpenForm "SecondFormName"
Then, under _Open event for the second form:
Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = TableToOpen
Hope this solve your problem.

And now an answer to your last question:
Does this make sense?
No. Because I can't understand why you run a Make Table query to create a lot of tables then use this tables as Record Sources for forms in your Front End databases. Then I suppose that you run other query (or queries) in order to update the main database.
Why you don't use that Make Table query (of course, as a Select query) as record source for your second form ? And the main database will be updated in that single step.

Of course I can't be sure that yo have not strong reasons to proceed as you proceed.
But I wish to re-advice you to follow the link you see in NeoPa's post.

Good luck !
Mar 28 '12 #6
32,173 Expert Mod 16PB
Teresa Kline:
Does this make sense?
It's much more of an explanation than before, so is much clearer. Unfortunately, without understanding what SAP means to you I'm still a little unclear, but I recognise the attempt to provide the missing info.

Mihail has already provided more detailed clues as to the sort of approach you'd need. It's still hard to know how much detail to include for you as we don't know how much you understand. Typical questions, as explained in the posts at the top of the Access forum (and most other forums), include the work you have done so far and the names of the objects you're working with. With this info to hand we can pitch an answer so that it's more likely to mean more to you. Explanations including named references that mean something to you are generally a lot easier for you to make sense of.

Normalisation is something that often doesn't hold very true for temporary tables (by which I mean it is less critically necessary and one can be tied to the capabilities of external systems), and I'm guessing these [tblLocation*] tables are just that (If not then you do have a problem). If so, remember to delete them when finished with.

I recommend Mihail's first approach for handling passing the selected table name, but not with positional parameters. Try instead :
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.OpenForm(FormName:="SecondFormName", OpenArgs:=Me.cboTables)
For the Open event of the new form use :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.     Me.RecordSource = Me.OpenArgs
  3. End Sub
Clearly, you will need to replace "SecondFormName" and Me.cboTables with the actual names of the objects in your project.
Mar 28 '12 #7
Thank you so much for all your help...It Works great!!! I am new to this online forum...I have used forums to search for answers before but never to post a question so I apologize for not giving better information in my question...I didn't supply my code as I work in HR and I am prohibited to do so and I do have business reasons for the temporary tables and the need for them.....sorry. :*(

But thank you all the solution works just as I needed it to. You guys are great.

Mar 28 '12 #8
32,173 Expert Mod 16PB
That's fine Teresa. I'm not sure the code needs to be protected so rigidly (if my experience is anything to go by), but HR data certainly should. All that said, it's a learning experience for you so I'm sure you'll have a better understanding for next time :-)

Great that it all helped anyway.
Mar 28 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Sham | last post: by
4 posts views Thread by Bob Sanderson | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.