473,407 Members | 2,320 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,407 software developers and data experts.

How can I construct a variable column name?

6
Access2007 VBA
I currently have the following code:
Expand|Select|Wrap|Line Numbers
  1.  If Cust!UTC17 = "1" Then
  2.         U17 = Cod!Cod1
  3.     Else
  4.     If Cust!UTC17 = "2" Then
  5.         U17 = Cod!Cod2
  6.     Else 
etc. etc
Is there any way I can reduce it to something like
Expand|Select|Wrap|Line Numbers
  1.  U17 = "Cod!Cod" & Cust!UTC17 
Cust!UTC17 contains a number
Cod contains multiple columns named COD1, COD2, COD3 etc.
I want to use the number contained in Cust!UTC17 to decide which columns value to assign to U17
Any advice much appreciated as my searching has not revealed a solution.
Dec 11 '14 #1
13 1595
twinnyfo
3,653 Expert Mod 2GB
To "answer" your question, yes it should be possible.

Much of it depends on how you are trying to assign these values. It is also unclear how "Cod" plays into this? Is this another table? How is that Table related to your first table? How do you know which record in table Cod to use? OR--are you trying to assign a particular field in Table Cod as a record source for a control names U17?

So, before we can guide to any more definite solutions, we need a little more information.
Dec 11 '14 #2
HRGED
6
I have a transaction table and need to update each row with up to 10 values from another table (COD) containing up to 30 possible values (columns Cod1 to Cod30) . The 2 tables have a one to one relationship. The problem is that the selection of the 10 columns from the 30 availble varies from client to client. I have another table (Cust) which has one row per client with 10 columns,(UTC17 to UTC26) one for each of the columns in the transaction table that needs to be updated. Each of the 10 columns in the Cust table has a number corresponding to the source column in the COD table.
The flow is:
1. Read a transaction
2. Get the COD row with the same ID
3 Get the Cust row for the transaction
4. For each Cust!UTCnn get the Cod!Codnn
e.g. if Cust!UTC17 contains 5 the value in Cod!Cod5 is selected.
5. Update UTCnn in the transaction with the value from step 4

I hope this has made the process a bit clearer. I acknowledge that the table design leaves a bit to be desired but, except for the Cust table, they are out of my control.
Dec 11 '14 #3
jforbes
1,107 Expert 1GB
I think Twinnyfo is thinking of a slick way to do it things in one line, but a quick and dirty way to do this is:
Expand|Select|Wrap|Line Numbers
  1. Select Case Cust!UTC17 
  2.     Case "2" 
  3.         U17 = Cod!Cod1
  4.     Case "1" 
  5.         U17 = Cod!Cod2
  6. End Select
http://msdn.microsoft.com/en-us/library/cy37t14y.aspx
Dec 11 '14 #4
twinnyfo
3,653 Expert Mod 2GB
First, I am going to recommend you look through this thread: Database Normalization.

Can you help me understand your Cod Table a bit better? Does it only have 1 record, but 30 separate values? E.g., your table looks like this (just for example's sake):

Expand|Select|Wrap|Line Numbers
  1. Cod1   Cod2  Cod3    ...  Cod30
  2. Apple  Pear  Orange       Blue
If this is the case, and if your Customer Table has 10 COD columns, I would restructure your tables such that you have the following:

Table COD:
Expand|Select|Wrap|Line Numbers
  1. Field    Description
  2. CODKey   PK, Autonumber
  3. CODDesc  Description of COD
This Table (for now) would have 30 records.

I would remove the 10 UTC columns form your Cust Table.

Create a new Table based on Cust and Cod:

Expand|Select|Wrap|Line Numbers
  1. Field   Description
  2. CustID  FK to Cust Table
  3. CODKey  FK to COD Table
This Table would have up to 10 records for each Customer. however, it gives you more flexibility, because if you ever have to add an 11th COD to a customer, you don't have to restructure your entire DB. Additionally, this is a more efficient design, in that when you have a Customer that has fewer than 10 CODs, there is no wasted space.

Later on, when you want to display the CODs belonging to each Customer, a simple join query will do the trick.

This is a standard DB structure that is tried and true and complies with the best principles of design.

However, if you want to continue with the old structure, we can work toward a solution. however, keep in mind that any such solution will be pregnant with future problems once any of your data changes.
Dec 11 '14 #5
twinnyfo
3,653 Expert Mod 2GB
@jforbes,

I was initially thinking more along the lines of:
Expand|Select|Wrap|Line Numbers
  1. Me("COD" & Cust!UTC17)
But, as you can see above, I think there are more serious issues with the DB design.
Dec 11 '14 #6
HRGED
6
My introduction to the hallowed principles of 'the key, the whole key, and nothing but the key' was around 1980 and I certainly wouldn't design tables like this given a free hand.
The application is a data transform which takes table extracts from a 'real database' (I can't access it directly for security reasins), imports them and converts them to
an horrendous multi-record format text file of which this transaction table is but one of about 40 record types. It's not my design and I'm sure that the guys who originally designed this retired with the IBM 360.
I have constructed the file using Access without a drop of VBA just lots of queries but it's all hard coded and each new client needs a new database. I'm trying to build a single database which will process multiple clients. The original version of the COD file is how you advise but it contains 100s of Ks of records so the flattened version is much smaller and faster. The flat Cust table could be normalised at the expense of ease of maintenance as I haven't built any maintenance routines, customers are added directly into the table.
Thanks for your help.
Dec 11 '14 #7
twinnyfo
3,653 Expert Mod 2GB
Well, let's see if I can use a Commodore 64 to find a solution....

I will still need just a bit of information to assist. I am still not sure I understand how COD relates to Cust. You say COD has 100K+ records?

If I understand you correctly, If you have Cust1234, their 10 UTC17-UTC26 could be (for example) 1, 3, 5, 7, 9, 13, 14, 21, 22, 28. And I understand that you want to reference from the COD Table the corresponding fields: COD1, COD3, COD5, COD7, COD9, COD13, COD14, COD21, COD22, COD28. I get that.

But, the challenge for my understanding is, if COD has 100K records, are we saying we want the 100K records associated with COD1, COD3, COD5, COD7, COD9, COD13, COD14, COD21, COD22, COD28? Or, just a specific record within that table corresponding to the Cust.

Again, this is very possible, I am just having problems grasping the data flow.

Perhaps several concrete examples in how it is used (no proprietary or personal information, please).
Dec 11 '14 #8
HRGED
6
I was big iron (well, medium sized) in the days of the Commadore 64.
What we are trying to do is to undo good database design.
In the original database each transaction row has up to 30 Cod rows with the same transaction ID.
Lets say each transaction represents the purchase of an item (the legendary widget).
The customer asks us to collect certain information with each purchase and each customer has different pieces of information they want collected to aid their analysis of the data. Lets say one customer asked us to record the weather at time of purchase and the purchasers hair colour and another customer wants us to collect the colour of their shoes and their star sign (these examples are of course complete fantasy. The original database version of the COD table has one row for each customer, purchase and piece of data. The flattened version has them in columns 1 and 2 for example.
The recipient of the output file has specified that for the first customer the weather must be in column UTC20 and the hair colour must be in UTC18 wheras for the second customer the shoe colour must be in UTC17 and the star sign must be in UTC26.
If we had just one customer there would be the same number of transaction rows as Cod rows and just one row in Cust. The one row in Cust defines which columns should be mapped to which columns in the transaction table.
Dec 11 '14 #9
twinnyfo
3,653 Expert Mod 2GB
The tables you are using... Is this close to what they look like:?

Expand|Select|Wrap|Line Numbers
  1. COD
  2.  
  3. Field  Description
  4. Trans  Transaction ID
  5. COD1   Your first COD Data
  6. ...
  7. COD30  Your last COD data

Expand|Select|Wrap|Line Numbers
  1. Customer
  2.  
  3. Field   Description
  4. Trans   Transaction ID (same ID as in COD?)
  5. Fields  Other Fields
  6. UTC17   This is number that refers to the COD?
  7. ...
  8. UTC26   Last COD reference
Now, is the intent to move the data from COD1-COD30 into the UTC17-UTC26 Fields, or are you trying to create another flat file in which you have the Customer with the corresponding data from those specified COD numbers? Example:

Expand|Select|Wrap|Line Numbers
  1. Customer  UTC17    UTC18   UTC19   ...  UTC26
  2. ABC123    Green    Fast    Orphan  ...  Nike
  3. YYZ2112   Morning  Boring  Legos   ...  Snowflake
(I get the impression this data is kinda scattered and unique to each record?)

Is this data going to then be stored somewhere or is this exercise in futility just so you can send a report? Saving it might be easier, but will take up tons of space. A report is not going to be able to save anything.

Either way, I think you will have to use VBA to create the data, whether it is through the use of a home-made Function (which might be pretty easy) or modifying a recordset/Array. The Function method would be slow, but I think we could control it better.

Let me know if I am getting closer to understanding your data. I don't want to dive in the deep end, only to find out this is the kiddie-pool side....
Dec 11 '14 #10
HRGED
6
I've attached an Excel spreadsheet with example rows from my tables.
There are 3 transactions and 3 Cod rows. Transactions Field3 has the same value as COD Field2 so there is a one to one relationship.
The columns UTC17 to UTC26 in the Transactions rows were blank but I have populated them with the correct entries from COD.
Cust contains just one row, there is a one to many relationship with COD on CustCd.

Hopefully this will make it clearer.

Thanks
Attached Files
File Type: xls Example.xls (25.5 KB, 197 views)
Dec 11 '14 #11
twinnyfo
3,653 Expert Mod 2GB
To further clarify, Transactions, COD and Customers (as shown in the Spreadsheet) are existing tables in your DB?

Can we also assume that you are trying to convert all records into the Transactions table?

Here is an outline of the VBA code you will want to build:

Create a recordset based on the Customers Table. Include all fields.

You will cycle through each record in this recordset, one customer at a time.

For each record in the Customers recordset, build another Query, based on the COD table, with the Criteria:

Expand|Select|Wrap|Line Numbers
  1. WHERE CustCd = '" & RecordsetName1!CustCd & "'"
This will produce a list of all records in the COD table belonging to that Customer.

Cycle through these records one at a time. For each record, you will want to do this:

Using the values in UTC17-UTC26, use that as an index for the fields in the COD table. This is important!!!!! In your COD Table, what is the number of the first field that has the COD Values? As shown in your Spreadsheet, it looks like the first field (COD1) is the 4th field. When Access numbers fields, it starts counting at 0, so, your 4th field is actually referred to as RecordsetName2.Fields(3). I hope this makes sense. However, if we are trying to calculate the actual field from your example, UTC17 is looking for COD3, which would evaluate to RecordsetName2.Fields(6): 4 is the first COD, 5 is the 2nd, etc.

So, to copy the values to the Transactions table, you would cycle through UTC17-UTC26, and if there is a value, the corresponding Field from the second recordset will be in field (RecordsetName1.UTC17 + 3)

You can also automate the finding of these values. I am going to straw-man this out, as I am freehanding it.....

Expand|Select|Wrap|Line Numbers
  1. 'Create First Recordset
  2. 'Create second recordset based on CustCd
  3. For intCounter = 17 To 26
  4.     strCODValue = SecondRecordset.Fields(FirstRecordset("UTC" & intCounter) + 3)
  5.     'Assign this value to the Transactions Table
  6. Next intCounter
This is very confusing--even for me as I am trying to work through it. So, you will want to go slow, and make sure that you are able to append all the data associated with the customer to the transactions table first. Then work through adding the COD values. Keep your breakpoints active and walk through the code step by step.

I am more than happy to work through the more complexified aspects of this code, but hopefully you have enough experience to at least get the ball rolling. We will gladly troubleshoot and provide additional advice.
Dec 11 '14 #12
HRGED
6
Sorry for the delay in replying. I didn't know columns could be referred to postionally instead of by column name. I'll give that a try and let you know.
Thanks for all your time, knowledge, and, especially, patience.
Dec 12 '14 #13
twinnyfo
3,653 Expert Mod 2GB
Hey, this stuff can make you go gray or bald--or both. I'm just going gray. I've learned a lot over the past few years of little stuff that some of the geniuses here know about. They have been gracious enough to share, so I am glad to help others when I can.
Dec 12 '14 #14

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

Similar topics

8
by: Shino | last post by:
Hi, Can anyone help with this error: "ORA-00904: invalid column name"? Thanks! SQL> create view PPFa as 2 SELECT L.UserID AS LecID, U.Name, U.Email, I.IntakeID, S.UserID AS StudID 3 FROM...
10
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
11
by: Steven Smith | last post by:
When this event is triggered the following exception error occurs: 'System.Data.SyntaxErrorException' Missing operand after 'of' operator. So presumably it doesn't like the spaces in the column...
4
by: Ying Lu | last post by:
Hello, Under mysql, we have "desc tablename" to get the detail information about a table. My question is about to get column name, and column type for a specific table under PostgreSQL through...
1
by: imauser | last post by:
I have a database(PostgreSQL) table(about 70k rows).I am developing an ASP webpage and there is a list-box on it which contains the name of the columns of that table. User selects the column name...
5
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist...
1
by: jump | last post by:
hi ! i m using the java program and i want to access the variable column from the ms sql server data base . when i try to access the particular value as"select * from where =; i get the error as...
2
by: richkid | last post by:
Good Day, I'm Trying to pass parameters to represent a column name and value to a stored procedure to execute but havinfg difficulties... can anyone help? declare @columnName...
2
by: Big Daddy | last post by:
For example, if I have a DB table called DownloadPoints with a column named DownloadPointNo, then SqlMetal will create a file with a class called DownloadPoints with an accessor named...
2
by: sunilwije | last post by:
I have following code snippet to implement a multilingual website. Idea is to, depending on user's language i need to translate word "Man" to "Hombre" My Question: Will it be valid to use...
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.