Below is a very handy function that transposes fields from "Table1" into "Table2". Note, in Table1 the Field2, Field3, etc represent Option codes.
Basically it takes this... Table1
ProdBreakDown --Field2----- Field3-----Field4
Widget1------------1253--------3843-------3986
Widget2------------1130--------1234-------3843
Widget3------------1234--------5698-------4207
And transposes it into this... Table2
ProdTarget--OptionCodes
Widget1--------1253
Widget1--------3843
Widget1--------3986
Widget2--------1234
Widget2--------1253
Widget2--------3843
Widget3--------1234
Widget3--------5698
Widget3--------4207
To see this in action. Look at my attached database. In Module1, run function TransRecords() and look at Table2 to see the results. MY QUESTION:
Instead of transposing ALL records, I only want to transpose records from that contain specific option codes. For instance, the criteria would restrict transposed records to be where only Option Codes from Table1 that have a string that begins with '12' OR a string that equals '3843' are transposed.
For a small set of records, I could easily query this from the total results of Table2 after my function has been ran by using a simple "OR" statement in SQL. The problem is, my real data consists of over 70,000 records. So transposing all the data results in millions of records being created which over inflates my database and causes it to corrupt. That's why I need the restrict to specific criteria when transposing it into Table2 to avoid creating all the addition unnecessary records.
I hope this explanation is helpful. Any feedback is much appreciated! =) - Option Compare Database
-
Option Explicit
-
-
Public Sub TransRecords()
-
Call TransposeRecordset("Table1", "Table2", "ProdBreakDown")
-
MsgBox "Table2 updated!", vbExclamation + vbOKOnly
-
End Sub
-
-
Private Function TransposeRecordset(pstrrecoriginal As String, pstrrecnew As String, pstrkey As String)
-
-
Dim db As Database
-
Dim recorg As Recordset
-
Dim recnew As Recordset
-
Dim intCount As Integer
-
Dim varkeyvalue As Variant
-
Dim bolfound As Boolean
-
-
Set db = CurrentDb()
-
-
Set recorg = db.OpenRecordset("select * from [" & pstrrecoriginal & "]")
-
Set recnew = db.OpenRecordset("select * from [" & pstrrecnew & "]")
-
-
'Loop through records in recorginal
-
While Not recorg.EOF
-
-
intCount = 0
-
bolfound = False
-
-
'Loop through fields in recorginal looking for key
-
While intCount <= recorg.Fields.Count - 1 And bolfound = False
-
-
If recorg(intCount).Name = pstrkey Then
-
-
varkeyvalue = recorg(intCount)
-
bolfound = True
-
DoCmd.Echo True, "Transposing " & varkeyvalue
-
End If
-
-
intCount = intCount + 1
-
-
Wend
-
-
For intCount = 0 To recorg.Fields.Count - 1
-
-
'skip key field
-
If recorg(intCount).Name <> pstrkey Then
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
End If
-
-
-
Next
-
-
recorg.MoveNext
-
-
Wend
-
DoCmd.Echo True, ""
-
End Function
- Completely replace TransposeRecordset() with this newer Version if you wish to use the newly added functionality:
- Private Function TransposeRecordset(pstrrecoriginal As String, pstrrecnew As String, pstrkey As String, Optional strCriteria As String = "")
-
Dim db As DAO.Database
-
Dim recorg As DAO.Recordset
-
Dim recnew As DAO.Recordset
-
Dim intCount As Integer
-
Dim varkeyvalue As Variant
-
Dim bolfound As Boolean
-
Dim intCtr As Integer
-
Dim varCriteria As Variant
-
-
If strCriteria <> "" Then varCriteria = Split(strCriteria, ",")
-
-
Set db = CurrentDb()
-
-
Set recorg = db.OpenRecordset("select * from [" & pstrrecoriginal & "]")
-
Set recnew = db.OpenRecordset("select * from [" & pstrrecnew & "]")
-
-
CurrentDb.Execute "DELETE * FROM Table2", dbFailOnError
-
-
'Loop through records in recorginal
-
While Not recorg.EOF
-
intCount = 0
-
bolfound = False
-
-
'Loop through fields in recorginal looking for key
-
While intCount <= recorg.Fields.Count - 1 And bolfound = False
-
If recorg(intCount).Name = pstrkey Then
-
varkeyvalue = recorg(intCount)
-
bolfound = True
-
DoCmd.Echo True, "Transposing " & varkeyvalue
-
End If
-
intCount = intCount + 1
-
Wend
-
-
For intCount = 0 To recorg.Fields.Count - 1
-
'skip key field
-
If recorg(intCount).Name <> pstrkey Then
-
If strCriteria = "" Then 'No Criteria specified
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
Else '1 or more Criteria specified
-
For intCtr = LBound(varCriteria) To UBound(varCriteria)
-
If recorg(intCount) = varCriteria(intCtr) Then
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
End If
-
Next
-
End If
-
End If
-
Next
-
recorg.MoveNext
-
Wend
-
DoCmd.Echo True, ""
-
-
recorg.Close
-
recnew.Close
-
Set recorg = Nothing
-
Set recnew = Nothing
-
End Function
- Sample Calls depending on whether or not Criteria is requested and how many Criteria:
- Public Sub TransRecords()
-
'To Transpose the Data in Table1 with no Criteria on Op Codes
-
'Call TransposeRecordset("Table1", "Table2", "ProdBreakDown")
-
-
'To Transpose the Data in Table1 with Criteria set for a single Op Code
-
'Call TransposeRecordset("Table1", "Table2", "ProdBreakDown", "6384")
-
-
'To Transpose the Data in Table1 with Criteria set for 2 specific Op Codes
-
'Call TransposeRecordset("Table1", "Table2", "ProdBreakDown", "7777,8888")
-
-
'To Transpose the Data in Table1 with Criteria set for 10 specific Op Codes
-
'Call TransposeRecordset("Table1", "Table2", "ProdBreakDown", "1006,1122,3597,999,1007,1111,1234,9582,6754,1204")
-
-
'To Transpose the Data in Table1 with Criteria set for 16 specific Op Codes
-
Call TransposeRecordset("Table1", "Table2", "ProdBreakDown", _
-
"1006,1122,3597,999,1007,1111,1234,9582,6754,1204,9823,5512,4456,9872,87832,1283")
-
-
MsgBox "Table2 updated!", vbExclamation + vbOKOnly
-
End Sub
8 4561 Code Line #31 is the Key Code Line: - Private Function TransposeRecordset(pstrrecoriginal As String, pstrrecnew As String, pstrkey As String)
-
Dim db As Database
-
Dim recorg As Recordset
-
Dim recnew As Recordset
-
Dim intCount As Integer
-
Dim varkeyvalue As Variant
-
Dim bolfound As Boolean
-
-
Set db = CurrentDb()
-
Set recorg = db.OpenRecordset("select * from [" & pstrrecoriginal & "]")
-
Set recnew = db.OpenRecordset("select * from [" & pstrrecnew & "]")
-
-
'Loop through records in recorginal
-
While Not recorg.EOF
-
-
intCount = 0
-
bolfound = False
-
-
'Loop through fields in recorginal looking for key
-
While intCount <= recorg.Fields.Count - 1 And bolfound = False
-
If recorg(intCount).Name = pstrkey Then
-
varkeyvalue = recorg(intCount)
-
bolfound = True
-
DoCmd.Echo True, "Transposing " & varkeyvalue
-
End If
-
intCount = intCount + 1
-
Wend
-
-
For intCount = 0 To recorg.Fields.Count - 1
-
If recorg(intCount).Name <> pstrkey Then
-
If recorg(intCount) = "3843" Or Left$(recorg(intCount), 2) = "12" Then
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
End If
-
End If
-
Next
-
recorg.MoveNext
-
Wend
-
DoCmd.Echo True, ""
-
End Function
This is precisely what I needed!!! Thanks so much!!!!
You are quite welcome. I also took the liberty to modify your code so that you can either: - Transpose ALL the Data residing in Table1.
- Transpose only the Data for either a Single or Variable Number of Op Codes by passing a Comma Delimited String containing those specific Op Codes to the TransposeRecordset() Procedure using an Optional last Argument. I'll Post all this later, since I think that this can be quite a handy/beneficial feature for you. This will eliminate the need to modify the code directly within TransposeRecordset().
- Completely replace TransposeRecordset() with this newer Version if you wish to use the newly added functionality:
- Private Function TransposeRecordset(pstrrecoriginal As String, pstrrecnew As String, pstrkey As String, Optional strCriteria As String = "")
-
Dim db As DAO.Database
-
Dim recorg As DAO.Recordset
-
Dim recnew As DAO.Recordset
-
Dim intCount As Integer
-
Dim varkeyvalue As Variant
-
Dim bolfound As Boolean
-
Dim intCtr As Integer
-
Dim varCriteria As Variant
-
-
If strCriteria <> "" Then varCriteria = Split(strCriteria, ",")
-
-
Set db = CurrentDb()
-
-
Set recorg = db.OpenRecordset("select * from [" & pstrrecoriginal & "]")
-
Set recnew = db.OpenRecordset("select * from [" & pstrrecnew & "]")
-
-
CurrentDb.Execute "DELETE * FROM Table2", dbFailOnError
-
-
'Loop through records in recorginal
-
While Not recorg.EOF
-
intCount = 0
-
bolfound = False
-
-
'Loop through fields in recorginal looking for key
-
While intCount <= recorg.Fields.Count - 1 And bolfound = False
-
If recorg(intCount).Name = pstrkey Then
-
varkeyvalue = recorg(intCount)
-
bolfound = True
-
DoCmd.Echo True, "Transposing " & varkeyvalue
-
End If
-
intCount = intCount + 1
-
Wend
-
-
For intCount = 0 To recorg.Fields.Count - 1
-
'skip key field
-
If recorg(intCount).Name <> pstrkey Then
-
If strCriteria = "" Then 'No Criteria specified
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
Else '1 or more Criteria specified
-
For intCtr = LBound(varCriteria) To UBound(varCriteria)
-
If recorg(intCount) = varCriteria(intCtr) Then
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
End If
-
Next
-
End If
-
End If
-
Next
-
recorg.MoveNext
-
Wend
-
DoCmd.Echo True, ""
-
-
recorg.Close
-
recnew.Close
-
Set recorg = Nothing
-
Set recnew = Nothing
-
End Function
- Sample Calls depending on whether or not Criteria is requested and how many Criteria:
- Public Sub TransRecords()
-
'To Transpose the Data in Table1 with no Criteria on Op Codes
-
'Call TransposeRecordset("Table1", "Table2", "ProdBreakDown")
-
-
'To Transpose the Data in Table1 with Criteria set for a single Op Code
-
'Call TransposeRecordset("Table1", "Table2", "ProdBreakDown", "6384")
-
-
'To Transpose the Data in Table1 with Criteria set for 2 specific Op Codes
-
'Call TransposeRecordset("Table1", "Table2", "ProdBreakDown", "7777,8888")
-
-
'To Transpose the Data in Table1 with Criteria set for 10 specific Op Codes
-
'Call TransposeRecordset("Table1", "Table2", "ProdBreakDown", "1006,1122,3597,999,1007,1111,1234,9582,6754,1204")
-
-
'To Transpose the Data in Table1 with Criteria set for 16 specific Op Codes
-
Call TransposeRecordset("Table1", "Table2", "ProdBreakDown", _
-
"1006,1122,3597,999,1007,1111,1234,9582,6754,1204,9823,5512,4456,9872,87832,1283")
-
-
MsgBox "Table2 updated!", vbExclamation + vbOKOnly
-
End Sub
Outstanding! Thank you, thank you, thank you so much for the extra you are putting into this. It is very much appreciated!!! The modification you made is terrific. The only exception is, it doesn't allow me to do mid string exceptions like this line of code did. - If recorg(intCount) = "929KA1" Or Left$(recorg(intCount), 3) = "816" Then
Is there anyway to apply this logic to the new code you provided? Thanks again so much!!!!
- To apply Custom Criteria without making the code any more complex, Call the TransposeRecordset() Routine WITHOUT ANY CRITERIA, and manually enter the comparison as indicated below (Line #6). This Logic would simply call for the use of another If...End If Clause (Lines #6 and Line #11):
- ...
-
For intCount = 0 To recorg.Fields.Count - 1
-
'skip key field
-
If recorg(intCount).Name <> pstrkey Then
-
If strCriteria = "" Then 'No Criteria specified
-
If Left$(recorg(intCount), 3) = "816" Then
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
End If
-
Else '1 or more Criteria specified
-
For intCtr = LBound(varCriteria) To UBound(varCriteria)
-
If recorg(intCount) = varCriteria(intCtr) Then
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
End If
-
Next
-
End If
-
End If
-
Next
-
...
- Sample Call with no Criteria:
- 'To Transpose the Data in Table1 with no Criteria on Op Codes
-
Call TransposeRecordset("Table1", "Table2", "ProdBreakDown")
- To Transpose ALL Data with no Criteria whatsover, simply REM the previous Lines Out (Line #6 and Line #11):
- ...
-
For intCount = 0 To recorg.Fields.Count - 1
-
'skip key field
-
If recorg(intCount).Name <> pstrkey Then
-
If strCriteria = "" Then 'No Criteria specified
-
'If Left$(recorg(intCount), 3) = "816" Then
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
'End If
-
Else '1 or more Criteria specified
-
For intCtr = LBound(varCriteria) To UBound(varCriteria)
-
If recorg(intCount) = varCriteria(intCtr) Then
-
recnew.AddNew
-
recnew(0) = varkeyvalue
-
recnew(1) = Nz(recorg(intCount).Value, "")
-
recnew.Update
-
End If
-
Next
-
End If
-
End If
-
Next
-
...
Excellent! That will work!!! Thanks again so much!!! =)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sanjay Asrani |
last post by:
I have a table like the following
Field1 Field2 Field3
------ ------- ------
x1 y1 z1
x1 y2 z2
x1 y3 z3
x1 ...
|
by: Gregory S Moy |
last post by:
I'm playing around with the following tables and need some advice.
TablePeople (5000 records)
peopleID
sex
age
race
TableExam (5000 records)
examID
|
by: jenny.rhodes |
last post by:
Hello,
Please can anyone guide me on how to transpose an access table where I
have many records per id eg
UserID Question Answer
1 1 a
1 2 d
1 3 ...
|
by: Jenny |
last post by:
Hello,
Please can anyone guide me on how to transpose an access table where I
have many records per id eg
UserID Question Answer
1 1 a
1 2 d
|
by: sangeetha |
last post by:
Hi,
i need to transpose a nx1 matrix to 1xn matrix inorder to multiply
with nxn matrix in c language ...
can anyone help in this coding ..the nx1 matrix is pi the data type
is double *pi..this is...
|
by: kenshiro |
last post by:
Hi All,
I'm having a problem with some VBA code in one of my Access 2003
databases. I'm getting the following error when running code behind a
command button on a form: "Item not found in this...
|
by: BerkshireGuy |
last post by:
Is there a way to create a shortcut to open a MS Acccess DB and have it
go to a specific record?
In my DB, when a user delegates a record to another users, I currently
send out an email to the...
|
by: m.wanstall |
last post by:
Hi All,
This is similar to a question I asked earlier however this is following
a more "correct" way of doing things. I have normalised and summarised
an Exchange addressbook (a few thousand...
|
by: noname101 |
last post by:
I'm very new to VBA and am trying to teach myself a lot of this so please
bare with me. I have just one table and what I'd like to do is have a text
box and button captioned "Find". Of course...
|
by: shriil |
last post by:
Hi
I have this database that calculates and stores the incentive amount
earned by employees of a particular department. Each record is
entered
by entering the Date, Shift (morn, eve, or night)...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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: 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...
|
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,...
| |