Robert Waggoner wrote:
Thanks! But I'm not sure how to write the code to create the new table. Any
help getting started would be appreciated.
Creating the table isn't the problem.
Something like
CutID
CutType
CutValue
"CutType" would correspond to your column name in your current table.
"CutValue" would correspond to the number/value in the non-null field.
The way I would go about solving it is creating and executing a series
of SQL statements in code that select the non-null value "pairs" and
then inserting them into a normalized table using CurrentDB.Execute
<SQL statement>. Then the code would switch the fields and you'd be
done in no time.
This is what I've come up with so far...
<Note to serious developers... put the coffee down, finish
swallowing... I don't want to be responsible for hot-beverage-related
mishaps.
'----CODE START------------------
Option Compare Database
Public Sub NormalizeData()
Dim tdf As DAO.TableDef
Dim strFieldName As String
Dim intCounter As Integer
Const cSQL As String = "INSERT INTO tblCutsFinal (ID, MyValue,
MyFieldName) SELECT tblNonNormalCuts.MyID,"
Dim strSQL As String
Set tdf = DBEngine(0)(0).TableDefs("tblNonNormalCuts")
'---Loop through the field pairs fields(0) and fields(intCounter),
'--- and append them to the final table
For intCounter = 1 To tdf.Fields.Count - 1
'-----Build Insert statement
strSQL = cSQL & "[" & tdf.Name & "].[" &
tdf.Fields(intCounter).Name & "],'" & tdf.Fields(intCounter).Name & "'
FROM [" & tdf.Name & "] "
strSQL = strSQL & "WHERE ((([" & tdf.Name & "].[" &
tdf.Fields(intCounter).Name & "] IS NOT NULL)));"
'----Execute it
DBEngine(0)(0).Execute strSQL, dbFailOnError
Next intCounter
Set tdf = Nothing
End Sub
'----CODE END...