Hi all once again!!!!
I have a table of upto 30-fields that supposed to be Converted to a table of 5-Columns by UnionQuery. The field names are: -
CustomerID FirstName LastName Products1 Products2 Product3 ...Products28
-
1 APO Denty 2 3 7 ... 4
-
2 Adezii Neo 4 2 3 ... 0
-
.. ... ... .. .. .. ........
-
.. ... ... .. .. .. ........
-
By using a Union Query I can Convert this table to five Columns which contains CustomerID, Firstname, LastName and Products as follows -
CustomerID Firstname LastName Products Quantity
-
1 APO Denty Products1 2
-
1 APO Denty Products2 3
-
1 APO Denty Products3 7
-
.. ... ... .. ..
-
1 APO Denty Products28 4
-
2 Adezii Neo Products1 4
-
2 Adezii Neo Products2 2
-
2 Adezii Neo Products2 3
-
.. ... ... .. ..
-
2 Adezii Neo Products28 0
-
.. ... ... .. ..
-
.. ... ... .. ..
-
.. ... ... .. ..
-
The problem is I have to write this Union Query Expression Manualy inorder for it to be in this formart.
I have more than 30Tables of this kind and new tables everyday.
Should there be any means of making it automatic please help!!!!!
(Note!!, FieldNames will remain constant for whaterver the convertions)
Please!!!!
1 3313
Hi. What you ask cannot be achieved in a generalised form in SQL, but can be done if you use VBA code for recordset processing.
The function below can be called as many times as required to process each source table and output the results to a named destination table. For the first of the tables processed you need to call the function with its argument ClearPreviousValues set to True, to delete any previous version of the destination table. For all other calls you have this argument set False.
You would need to call this function from code on a form somewhere, say in the OnClick event of a command button.
The function returns True if the extract of products was successful, and false if not.
A typical call to the function looks like this: - blResult = fExtractProductQuantities("Name of First Input Table", "Name of Output Table", 4, 28, True)
-
blResult = fExtractProductQuantities("Name of Second Input Table", "Name of Output Table", 4, 28, False)
The function itself is shown below. You would need to place it in a public code module (one which is shown in the Modules tab), or create a new module if there is no existing module you can use. - Public Function fExtractProductQuantities(ByVal SourceTableName As String, _
-
ByVal DestinationTableName As String, _
-
ByVal FirstProductFieldNo As Integer, _
-
ByVal NoOfProducts As Long, _
-
ByVal ClearPreviousValues As Boolean) As Boolean
-
'This function extracts individual product quantities from a non-normal source table and inserts
-
'these as individual rows into a specified destination table.
-
'
-
'Although field names are noted as fixed for the application in which this function is to be used
-
'no assumptions about names are made. Instead, the fields are referred to by counting the left-most
-
'field in the source table as field 1, the next left as field 2, and the final field as fieldN where
-
'N is the number of columns in the source table.
-
-
'Assumptions
-
'1. The first field through to the field before FirstProductFieldNo are to be repeated on each row
-
' inserted in the destination table
-
'2. The first field is a numeric customer ID, not a text field
-
'3. The fields between the FirstProductNo column and the LastProductNo column contain numeric values
-
' representing the quantity of that product. The name of the field is the name of the product
-
' or if not, a synonym for it.
-
'4. There are no fields to the right of the LastProductNo column which need to be output.
-
'
-
'IN: SourceTableName: Name of the source table containing the repeated product rows
-
' DestinationTableName: Name of the destination table into which the result rows will be inserted
-
' FirstProductNo: Column number of the first product field, taking the left-most field to be column 1 of the table
-
' NoOfProducts: Number of product columns in sequence in each row
-
' ClearPreviousValues: If this is set to True then the previous values in the destination table are deleted.
-
' If False, the values are appended to those already in the table.
-
'
-
' All arguments are required values which must be supplied when the function is called.
-
'
-
'Out: function returns True if rows were inserted into the destination table, False if not.
-
'
-
'Example source data
-
'CustomerID FirstName LastName Products1 Products2 Product3 ...Products28
-
' 1 APO Denty 2 3 7 ... 4
-
'
-
'Example destination data
-
'CustomerID Firstname LastName Products Quantity
-
' 1 APO Denty Products1 2
-
' 1 APO Denty Products2 3
-
' .. ... ... .. ..
-
' 1 APO Denty Products28 4
-
'
-
'As the DAO object library is required a reference to the DAO object library must be available
-
'If the function does not compile select Tools, References and check that there is a tick against
-
'the Microsoft DAO 3.6 or similar object library.
-
Dim intCurrentField As Integer
-
Dim intFieldCount As Integer
-
Dim intProductCount As Integer
-
Dim rsSource As DAO.Recordset
-
Dim dbThisDB As DAO.Database
-
Dim strSQLCreate As String
-
Dim strSQLInsert As String
-
Set dbThisDB = CurrentDb
-
Set rsSource = dbThisDB.OpenRecordset(SourceTableName)
-
If rsSource.RecordCount > 0 Then
-
On Error Resume Next
-
If ClearPreviousValues Then
-
dbThisDB.Execute "DROP TABLE [" & DestinationTableName & "]"
-
End If
-
strSQLCreate = "Create Table [" & DestinationTableName & "] ("
-
For intFieldCount = 0 To FirstProductFieldNo - 2
-
strSQLCreate = strSQLCreate & rsSource.Fields(intFieldCount).Name & IIf(intFieldCount = 0, " INT ,", " VARCHAR(255), ")
-
Next
-
strSQLCreate = strSQLCreate & "Product VARCHAR(255), Quantity INT);"
-
dbThisDB.Execute strSQLCreate
-
On Error GoTo errEncountered
-
Do While Not rsSource.EOF
-
For intProductCount = 1 To NoOfProducts
-
strSQLInsert = "INSERT INTO [" & DestinationTableName & "] Values (" & rsSource.Fields(0) & ", "
-
For intFieldCount = 1 To FirstProductFieldNo - 2
-
strSQLInsert = strSQLInsert & "'" & rsSource.Fields(intFieldCount) & "', "
-
Next intFieldCount
-
intCurrentField = intProductCount + FirstProductFieldNo - 2
-
strSQLInsert = strSQLInsert & "'" & rsSource.Fields(intCurrentField).Name & "', "
-
strSQLInsert = strSQLInsert & rsSource.Fields(intCurrentField) & ");"
-
dbThisDB.Execute strSQLInsert
-
Next intProductCount
-
rsSource.MoveNext
-
Loop
-
rsSource.Close
-
fExtractProductQuantities = True
-
End If
-
Exit Function
-
errEncountered:
-
fExtractProductQuantities = False
-
End Function
I attach a text file export of a test run on a single table. I have also tested this with multiple table imports.
-Stewart
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Buttercup |
last post by:
I am having alot of trouble with a union query Im trying to create.
The bizarre thing is I initially created this query and managed to
save it as a view
SELECT ID, DepartureDate, City,...
|
by: Dalan |
last post by:
From reading Access 97 help text, it seems that to do what I need to
do will require a Union Query. As this would be my first, I think I
might require a little guidance.
I have two tables with...
|
by: M Stery |
last post by:
Have two base queries used for mailing labels with
id,name,address,city,state,zip that are combined via a union query.
One base query uses home address; the other base query uses biz
address. I...
|
by: M Stery |
last post by:
Using Transfer Text macro to create comma-delimited file of the
results of union query. Underlying queries use \(999") "999\-9999! to
format area code/phone number and work fine, but once the...
|
by: mattytee123 |
last post by:
I have about 20 tables, of which I would like to do a union query and
count of how many of each different code there is?
The simplified verson of the table is structured like this.
Code ...
|
by: NoodNutt |
last post by:
G'day ppl.
Can anyone assist me with the correct structure of the following in a Union
Query.
tblBookings.FinYear
tblBookings.DepPrefPay
tblBookings.IntPrefPay
tblBookingsFinPrefPay
|
by: planetthoughtful |
last post by:
Hi All,
I'd like to place a subreport onto a report that draws its records from
a UNION query.
When I design the query and view the records all looks fine. However,
when I put the query as...
|
by: bgreenspan |
last post by:
Hi Everyone, I'm back for some more expert help. Here's what I am
doing and what I tried.
My database has entries with Contract Names and Expiry Dates, among
other fields. I have a form...
|
by: cashotte |
last post by:
Hi,
Is there a way to have a union query (2 tables) in an other query then
join a third table having the ability to edit records in the third
table?
Thanks,
Stephane
|
by: kcdoell |
last post by:
Hello:
I am trying to create a union query but do not have a lot of experience. Basically I have the below tables:
The Tables:
Table Name = tblPrior
CreditRegIDFK; Number; Foreign Key...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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: 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...
| |