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

Loop through columns extracting field names

2
Hi, first time poster, be gentle with me!

I have a table (called Raw) which I'd like to dump into a new table (called Working). Four of the fields are static and will be repeated each time. However there are 104 fields of data (each named by the date the sales are for) which I'd like to extract, using the name of the field as the value for a new field.

I've attached an example of the raw data, and how I'd like it to look in the new working table.

Is there any way I can do this by counting columns rather than using their names? This would be ideal, as I have to clean this sort of data all the time, so it would be good to be able to use the same code over various formats of raw data.

TIA!
Attached Files
File Type: xls sample.xls (23.5 KB, 402 views)
Sep 24 '14 #1
3 3652
twinnyfo
3,653 Expert Mod 2GB
zestee,

Welcome to Bytes!

Do you have 104 fields of Dates or 104 total fields (the four standard fields + 100 Dates)? Are there always 104?

These questions are important, because if the raw data is always the same, then it makes things a bit easier.

Below, I've assumed a total of 108 fields (four standard + 104 Dates). I think your spreadsheet listed a Date instead of the number of sales, that that was my assumption.

Double check tables and field names, and this should be a good place to start.....

Expand|Select|Wrap|Line Numbers
  1. Public Sub FieldNames()
  2.     Dim db As Database
  3.     Dim rst1 As Recordset
  4.     Dim rst2 As Recordset
  5.     Dim strSQL As String
  6.     Dim intField As Integer
  7.  
  8.     Set db = CurrentDb()
  9.     strSQL = "SELECT * FROM tblRaw;"
  10.     Set rst1 = db.OpenRecordset(strSQL, dbOpenDynaset)
  11.     If Not rst1.RecordCount = 0 Then
  12.         strSQL = "SELECT * FROM tblWorking;"
  13.         Set rst2 = db.OpenRecordset(strSQL, dbOpenDynaset)
  14.         rst1.MoveFirst
  15.         Do While Not rst1.EOF
  16.             For intField = 4 To 107
  17.                 With rst2
  18.                     .AddNew
  19.                     !prodname = rst1!prodname
  20.                     !prodcode = rst1!prodcode
  21.                     !mktname = rst1!mktname
  22.                     !measure = rst1!measure
  23.                     !Date = CDate(rst1.Fields(intField).Name)
  24.                     !Sales = rst1.Fields(intField).Value
  25.                     .Update
  26.                 End With
  27.             Next intField
  28.             rst1.MoveNext
  29.         Loop
  30.         rst2.Close
  31.         Set rst2 = Nothing
  32.     End If
  33.     rst1.Close
  34.     db.Close
  35.     Set rst1 = Nothing
  36.     Set db = Nothing
  37. End Sub
Hope this helps!
Sep 24 '14 #2
zestee
2
twinnyfo, you're a rockstar! I did a little jig, am so stoked it works :)
Sep 25 '14 #3
twinnyfo
3,653 Expert Mod 2GB
Glad I could be of assistance.
Sep 26 '14 #4

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

Similar topics

3
by: WC Justice | last post by:
I need to build an UPDATE statement that copies the values of roughly 40 fields from a table that stores standard or default values into a table of specific contracts. There are 8 or so fields in...
3
by: Tom | last post by:
Data is collected over time in an Excel worksheet with 20 columns. The Excel worksheet starts out as a copy of a template as is filled in as data is collected. Eventually the worksheet file is...
2
by: dhnriverside | last post by:
Hi guys Ok, here's the situation. I'm building a reporting system for my application, that basically creates a CSV file based on a load of data in my database. I'm trying to make the...
3
by: Justin Clift | last post by:
Hi all, I'm creating a centralised table to keep a log of changes in other tables. In thinking about the PL/pgSQL trigger to write and attach to the monitored tables (probably a row level...
0
by: John Smith | last post by:
I'm using PDO for the first time. I want to get the field/column names from a SELECT query. I can get the names from the associative array returned by fetch(). But how do I get the field names if...
2
by: 675i76 | last post by:
I need to import a spreadsheet but the spread sheet has blank collums and the first row contains abbreviations (e.g. DEPT. - with period). the transferspreadsheet gives me field names "noname1",...
11
by: Tim Hunter | last post by:
Hi I am using WinXP and Access 2003 Is it possible to store the field names of a table in an array and then loop through the array and update the table using the field names stored in the array? I...
4
by: Bob | last post by:
Hi all, I've got a table that I've imported and it has junk at the top of the table, so after import I run a delete query to remove the junk lines then I'm left with the field names I want for...
4
by: | last post by:
Given an XML file (dataset.writexml), here is my output (simplified for this posting): <?xml version="1.0" standalone="yes"?> <NewDataSet> <Category> <CategoryId>80</CategoryId>...
4
by: dreaken667 | last post by:
I have a MySQL database containing 16 tables of data. Each table has a different number of columns and there are few common field names accross tables. I do have one master table with which I connect...
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: 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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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,...

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.