473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Change Record Source of Form on Opening/Loading

43 32bit
Hi there!

I have 16 tables with different names but all the fields are same in these tables even data types too. I have also one form named "Main Page" where I have 16 buttons for those tables called btn1, btn2, btn3, ....... btn16. I want to open another form named "Form1". Can I set the record source of this form on opening/loading as RecordSource = Me.btn1 or RecordSource = Me.btn2 etc.
Aug 19 '18 #1
4 4145
PhilOfWalton
1,430 Expert 1GB
Firstly the answer is yes, but it almost certainly is a thoroughly bad idea.

To do what you want, you need to do something like
Expand|Select|Wrap|Line Numbers
  1. Private Sub Btn1_Click
  2.     Dim StrSQL as String
  3.  
  4.     StrSQL = "SELECT Table1.* FROM Table1"
  5.  
  6.     DoCmd.OpenForm Form1
  7.     Forms!Form1.RecordSource = StrSQL
  8.  
  9. End Sub
Obviously for Btn2 you would use Table2 etc.

I think you should have a look at a single table combining the 16 existing tables and add a new field "TableNo" indicating which table the data was from initially.

Then it is a simple matter to apply a filter to your form filtering data from Table1, Table2 etc. This will save so much effort if you need to change the table design.

Phil
Aug 19 '18 #2
zmbd
5,501 Expert Mod 4TB
Aftab Ahmad:
While I agree with PhilOfWalton that this isn't the thing to be doing (besides we frequently late-bind forms to subforms in tab-controls for performance and this really is no different) - I do disagree with some of the finer points:

- Intead of the "single table combining the 16 existing tables and add a new field "TableNo" as suggested by PhilOfWalton - you should re-evaluate the overall design of the database. The fact that you have multiple tables with the same internal structure points to a highly non-normalized database.

This will lead you to issues such as needing a navigation form to open the tables for say 2014's records, 2015's records, 2016's records, etc... with each new year's records (or however one is batching the data) you'll have to go in and add a new button and new code. This becomes a maintenance nightmare; however, job security is assured... it also makes trending within queries between tables a lot more complicated than it really should be - whereas a normalized database should be much easier to handle and query.

We see this type of database all too often when someone directly converts a workbook to a database, assuming that each worksheet needs to be a table within the database - or simply, that they don't understand the nature of a RDMS. (hmm, mea culpa, in my early days, some very ugly databases - live and learn :) )

Normalization is your friend!
home > topics > microsoft access / vba > insights > database normalization and table structures

- The nice thing about PhilOfWalton's maping table is that you could use a combobox and pull the table name from the combobox and set the record source that way - example attached! - IMHO, if you insist on the current table arrangement this would be a much better solution than either command buttons or frame-option buttons. In this case I would use a combobox, one could just as easily use a list-box control; however, I personally dislike the list-box for things like selecting an option... for me, in this usage a list-box just seems cluttered to the eye.

+ I've also refined your approach using command buttons by changing over to Frame-Option buttons. I've not included it in the code; however, one could use a select-case to set the table name when using the option button approach. In this example the table names all have a root name and only need the value of the option-button (via the frame control) to be appended.

The "calling form" will open with the database.
When you click on either of the command buttons the calling form will close and the selected table will open in the "table form."

The code should all be self explanatory; at least it made sense to me at the time :)

The error trapping in the on_open event of the "table form" is to guard against the event a user mangles a table name (known to happen) or if a table name were to be mis-entered into the mapping table.
Attached Files
File Type: zip Bytes971244.zip (47.2 KB, 126 views)
Aug 19 '18 #3
Aftab Ahmad
43 32bit
Thank you so much friends. This is just what I am searching.
Aug 19 '18 #4
NeoPa
32,556 Expert Mod 16PB
ZMBD:
- Intead of the "single table combining the 16 existing tables and add a new field "TableNo" as suggested by PhilOfWalton - you should re-evaluate the overall design of the database. The fact that you have multiple tables with the same internal structure points to a highly non-normalized database.
I suspect you may actually be agreeing if I understand correctly, just expressing the same problem in different ways.

I would also support the idea that changing the RecordSource is a perfectly viable way to go - except it looks like the reason in this case is wholly invalid. There are good reasons to do this, and of course it can be done, but this does very much sound like your design is the real problem and with a proper design you wouldn't even be getting into this area.

Good luck with your project :-)
Aug 20 '18 #5

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

Similar topics

1
by: Sham | last post by:
I have a form that is used for entering data. The problem is I have several tables, so I need some way to change the form's record source via combo box. 1. How can I make the combo box look for...
2
by: Douglas Buchanan | last post by:
This is a mystery! Why does the boolean value of a record change without my input. What follows describes the situation and the behavior. Table1: Field1: Text Field2: Yes/No Form1 has two...
2
by: Joshua Ammann | last post by:
How do you change what row is highlighted in a listbox (so that it highlights the current record you are viewing in the form) when you use the built-in navagation buttons (or any method other than...
2
by: Lyn | last post by:
If I have a form where the RecordSource property is set to the name of a table, then on opening the SingleForm form I can cycle through all the records in the table one at a time via Next and...
4
by: steph | last post by:
Hi, A question regarding Access 2002: I've got a form which should be displayed in Form View and in Datasheet View. But I want to use a different record source for each view. That means, when...
15
beacon
by: beacon | last post by:
Hi everybody, Using Access 2003. I'm trying to dynamically set the record source for the current form and set the control source for a text box to input data to one of three tables. I have a...
7
by: chrismaliszewski | last post by:
Hi. I created code which makes dynamically form with bounded controls for all columns. I show it to you below. My problem is, how I have to change this code to create form which record source...
1
by: Lordoasis | last post by:
I will appreciate some assistance with a project of mine. I am relatively new to access. I have a form with unbound text boxes. I want to be able to search for member IDs with one of the unbound...
1
by: prananv | last post by:
Hi Good Morning, I am in position to solve a problem in MS access 2007. Problem: I have a Main form and contains subform. Based on combo selection the subform records are displayed. Main...
3
mjoachim
by: mjoachim | last post by:
I am trying open a form from VBA to view a record that was moved from the original table to an Archive Table. I am having a hard time changing the record source of the form to look at the Archive...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.