473,387 Members | 1,863 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,387 software developers and data experts.

Referencing a field name common to two or more tables...

patjones
931 Expert 512MB
Hi:

I have two tables in my Access database, tblBase and tblRIP. Both of these tables have field names in common. The fields contain different data, but simply have the same names. For example, both tables have a field called fldAsstCoordinator. It's just that the numbers under this field name in tblBase are different from the numbers under the same field name in tblRIP...

My question is this. When referencing these tables in Visual Basic, how do I differentiate between the two? Right now, when I put in

Expand|Select|Wrap|Line Numbers
  1. .txtBaseRate = rst![tblBase].[fldLfgd] 
where txtBaseRate is just a textbox on my main form, and rst is the recordset based on a SQL query containing all the tables in my database, I get an error "3265 - Item not found in this collection".

I'm explicitly telling VB which table to pull the information from (i.e. tblBase and not tblRIP) - so why is it giving me an error?

Thanks in advance for any help that you can offer...

Pat
Nov 11 '07 #1
7 6210
Jim Doherty
897 Expert 512MB
Hi:

I have two tables in my Access database, tblBase and tblRIP. Both of these tables have field names in common. The fields contain different data, but simply have the same names. For example, both tables have a field called fldAsstCoordinator. It's just that the numbers under this field name in tblBase are different from the numbers under the same field name in tblRIP...

My question is this. When referencing these tables in Visual Basic, how do I differentiate between the two? Right now, when I put in

Expand|Select|Wrap|Line Numbers
  1. .txtBaseRate = rst![tblBase].[fldLfgd] 
where txtBaseRate is just a textbox on my main form, and rst is the recordset based on a SQL query containing all the tables in my database, I get an error "3265 - Item not found in this collection".

I'm explicitly telling VB which table to pull the information from (i.e. tblBase and not tblRIP) - so why is it giving me an error?

Thanks in advance for any help that you can offer...

Pat
if you are pulling data into rst from one table at a time then to assign the value to your textbox from your rst using rst!fldLfgd is sufficient because there exists no conflict of name within the rst defined by the SQL.

However if you are JOINING tables together in SQL where a conflict of name would arise in the defined columns in SQL then you need to alias the instances of conflicting named columns ie:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT tblMyTable1.myid, tblMyTable1.fldLfgd, tblMyTable1.myfield3, tblMyTable1.myfield4, tblMyTable2.fldLfgd AS MyfldLfGdAlias, tblMyTable1.myfield6, tblMyTable1.myfield7, tblMyTable1.myfield8 
  3. FROM tblMyTable1 
  4. INNER JOIN tblMyTable2 ON tblMyTable.myid=tblMyTable2.myid
  5.  
  6.  
You would then refer to the sql defined ALIAS provided to your rst in your textbox assignment like this

Expand|Select|Wrap|Line Numbers
  1. txtBaseRate = rst!MyfldLfDgAlias
The naming of your alias for the column can obviously be whatever you like, it need not be as I have given here necessarily

Regards

Jim :)
Nov 11 '07 #2
patjones
931 Expert 512MB
if you are pulling data into rst from one table at a time then to assign the value to your textbox from your rst using rst!fldLfgd is sufficient because there exists no conflict of name within the rst defined by the SQL.

However if you are JOINING tables together in SQL where a conflict of name would arise in the defined columns in SQL then you need to alias the instances of conflicting named columns ie:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT tblMyTable1.myid, tblMyTable1.fldLfgd, tblMyTable1.myfield3, tblMyTable1.myfield4, tblMyTable2.fldLfgd AS MyfldLfGdAlias, tblMyTable1.myfield6, tblMyTable1.myfield7, tblMyTable1.myfield8 
  3. FROM tblMyTable1 
  4. INNER JOIN tblMyTable2 ON tblMyTable.myid=tblMyTable2.myid
  5.  
  6.  
You would then refer to the sql defined ALIAS provided to your rst in your textbox assignment like this

Expand|Select|Wrap|Line Numbers
  1. txtBaseRate = rst!MyfldLfDgAlias
The naming of your alias for the column can obviously be whatever you like, it need not be as I have given here necessarily

Regards

Jim :)
Thank you...I'll give this a try today and report back on how it went!
Nov 11 '07 #3
patjones
931 Expert 512MB
Works perfectly! Thanks so much...

Pat
Nov 11 '07 #4
Jim Doherty
897 Expert 512MB
Works perfectly! Thanks so much...

Pat
You're welcome and thanks for reporting back

Jim :)
Nov 11 '07 #5
NeoPa
32,556 Expert Mod 16PB
It's good policy to alias confusing fields in SQL, but in this case it gets around the fundamental problem rather than answering it (not a criticism - I'm just interested in the answer to the original question Jim).
Could you try running the following code against your recordset and see what it returns for the names of the two fields in question. The names should be obvious once they come out and will indicate the general principle for future reference.
Expand|Select|Wrap|Line Numbers
  1. Public Sub TestItOut
  2.   Dim varThis As Variant
  3.   Dim rst As DAO.Recordset
  4.  
  5.   '... Whatever you do to open your recordset
  6.   For Each varThis In rst.Controls
  7.     Debug.Print varThis.Name
  8.   Next varThis
  9. End Sub
Nov 12 '07 #6
patjones
931 Expert 512MB
It's good policy to alias confusing fields in SQL, but in this case it gets around the fundamental problem rather than answering it (not a criticism - I'm just interested in the answer to the original question Jim).
Could you try running the following code against your recordset and see what it returns for the names of the two fields in question. The names should be obvious once they come out and will indicate the general principle for future reference.
Expand|Select|Wrap|Line Numbers
  1. Public Sub TestItOut
  2. Dim varThis As Variant
  3. Dim rst As DAO.Recordset
  4.  
  5. '... Whatever you do to open your recordset
  6. For Each varThis In rst.Controls
  7. Debug.Print varThis.Name
  8. Next varThis
  9. End Sub
Hi NeoPa:

I put your code into my subroutine as follows:

Expand|Select|Wrap|Line Numbers
  1.  Sub DisplayEmployeeInfo(rstEmployee As DAO.Recordset) 
  2.  
  3. On Error GoTo Err_DisplayEmployeeInfo
  4.  
  5.     Dim varThis As Variant
  6.  
  7.     With Me
  8.  
  9.         .txtNameLast = rstEmployee!fldNameLast
  10.         .txtNameFirst = rstEmployee!fldNameFirst
  11.         .txtERN = rstEmployee!fldERN
  12.         .txtLocationPrevious = rstEmployee!fldLocationPrevious
  13.         .txtTitlePrevious = rstEmployee!fldTitlePrevious
  14.         .txtCurrentSeason = rstEmployee!fldSeasonCurrent
  15.  
  16.     End With
  17.  
  18.     For Each varThis In rstEmployee.Fields
  19.         Debug.Print varThis.Name
  20.     Next varThis
  21.  
  22.     Set rstEmployee = Nothing
  23.  
In other words, I pass my recordset to this subroutine for printing to my form. Also note that I changed your code from rstEmployee.Controls to rstEmployee.Fields since there doesn't seem to be a Controls property available for this object. The code runs fine, and the Immediate window shows:

Expand|Select|Wrap|Line Numbers
  1. fldERN
  2. fldNameLast
  3. fldNameFirst
  4. fldTitlePrevious
  5. fldLocationPrevious
  6. fldAgencyStartDate
  7. fldSeasonCurrent
  8. tblLifeguards.fldBaseAddition
  9. fldAsstCoordinatorBase
  10. fldLfgdCoordinatorBase
  11. fldLfgdBase
  12. fldBoroCoordinatorBase
  13. fldChiefBase
  14. fldLieutenantBase
  15. fldAsstCoordinatorRIP
  16. fldLfgdCoordinatorRIP
  17. fldLfgdRIP
  18. fldBoroCoordinatorRIP
  19. fldChiefRIP
  20. fldLieutenantRIP
  21. fldSeason
  22. fldLongevityAmt
  23. tblBaseAdditionCategories.fldBaseAddition
  24. fldBaseAdditionAmt
  25.  
But this is with the alias in effect in the SQL code, so all the fields are appearing with their aliased names. For instance, fldAsstCoordinator appears as fldAssCoordinatorBase for one table and fldAssCoordinatorRIP for the other table..is this what you were looking for? Or do you want me to do this prior to the aliasing?

Are you trying to say that I should simply give my fields more sensible names in the tables? : - )

Thanks...

Pat
Nov 15 '07 #7
NeoPa
32,556 Expert Mod 16PB
...Also note that I changed your code from rstEmployee.Controls to rstEmployee.Fields since there doesn't seem to be a Controls property available for this object. The code runs fine, and the Immediate window shows:
...
Good spot. My bad :)
Notice that the fields in lines #8 & #23 have the same fundamental name, but Access has given references to the fields which include the table names. This is exactly what happens when the same fieldnames are used in a multi-table query (in a simple QueryDef).
But this is with the alias in effect in the SQL code, so all the fields are appearing with their aliased names. For instance, fldAsstCoordinator appears as fldAssCoordinatorBase for one table and fldAssCoordinatorRIP for the other table..is this what you were looking for? Or do you want me to do this prior to the aliasing?
Not all ;)
Are you trying to say that I should simply give my fields more sensible names in the tables? : - )
NOOOooo. I wouldn't dream of it.
Actually, there are often times where tables need to use the same fieldnames as other tables. In a query though, I would always alias fields which are not uniquely named within the query. It just makes life so much easier. Why wouldn't one?
Nov 15 '07 #8

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

Similar topics

2
by: Jim G | last post by:
I have two tables: TestA and TestB. Both tables have 3 fields: ID, Name, and RunDate. I need to create a query which will join the two tables first on Name but then I need to match up the...
4
by: Andy Hutchings | last post by:
Hi everybody - hope you can help out here. I have a form in a database, which is a columnar form from one of the tables in the db - there is a sub-form to the form which is a datasheet view of...
1
by: Don Leverton | last post by:
Hi Folks, I have been given a CD with approx 130 .xls files (bean-counters!) that I would like to import and merge to ONE table (tblTradeshow). The XL files are *similarly*, but not...
2
by: Rooksarii | last post by:
Hello folks, Let me first apologize for any impropper terminology I may use as I am presently trying to broaden my Office knowledge by diving into Access head on. My specific problem is this....
9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
5
by: Amelyan | last post by:
I am struggling here trying to determine what is a good programming practice as far as referencing your URLs. When you use Response.Redirect, do you use 1) Hard-coded string --...
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
5
by: rdemyan via AccessMonster.com | last post by:
I have a need to add another field to all of my tables (over 150). Not data, but an actual field. Can I code this somehow. So the code presumabley would loop through all the tables, open each...
3
by: dstorms | last post by:
Hi there, I'm developing a database for my company, and for the most part I've been successful, with some complex sub-routines already working fine. But this one has me stumped! Some background...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.