Is there a way to create a colum from values in a row?
For example, Let's say I have the following:
bname,postcode,floor,business
a,qqq qqq,ground,office
a,qqq qqq first, office
a, qqq qqq, second,office
b, www www, first, shop
b,www www, second, office
i can turn this using crosstab like this;
bname,postcode,ground,first,second
a,qqq qqq, office,office,office
b,www www,,shop,office
now i want to convert
if 1bname have different business at its same floor like
bname,postcode,ground,first,second
a,qqq qqq,ground,office
a,qqq qqq first, office
a, qqq qqq, ground,shop
a,qqq qqq,second,office
i want like
bname,postcode,ground,first,second
a, qqq qqq,office/shop,office,office
i have lots of data, how can i convert this using vb-6/MS-Access, or in crosstab option how can i join this string.
Thanks for all your help guys!
Aziz
9 2127
Hi Aziz, and Welcome to Bytes.
The dummy data you have posted is too far removed from real data to get a feel for what you are actually wanting to see as output. (Posting a's and b's and a lot of 'qqq qqq' postcodes is actually much less clear than if you had supplied proper values with some changes to protect confidentiality.)
If you can please post something more like what you want to see - with different values for your groupings so that the range and type of difference is clearer - perhaps it will be possible to come up with practical suggestions.
Access crosstab queries can only pivot one column, so these are not really suitable if you want more than one column, or if you need to select different columns at times.
Please clarify what your real needs are and I'm sure we will do our best to help you.
Regards
-Stewart
Basically you want to RollUp a field value into one field.
A solution could be to create a function that will "rollup" the floor into one field like: -
Function fncRollUp(strBusinessName as string)
-
-
dim rs as dao.recordset
-
-
set rs = currentdb.openrecordset("select * from tblX Where 1bname='" & strBusinessName & "'")
-
While NOT rs.EOF
-
fncRollUp = fncRollUp & ", " & rs!floor
-
rs.MoveNext
-
End With
-
-
End Function
-
This function must be copy/pasted into a module and can be used in a query like: -
SELECT DISTINCT 1bname, fncRollUp([1bname]) As Floors FROM tblX ;
-
Nic;o)
I may have over complicated matters a little, but here goes, using your data. For now, it just Prints the results, but it's too close to bedtime so I'll leave it as such. If Nico's idea works well for you, I strongly suggest you go with his approach. - Dim strSQL As String
-
Dim strSQL_2 As String
-
Dim strBusiness As String
-
Dim strPostCode As String
-
Dim MyDB As DAO.Database
-
Dim rstUniqueBusiness As DAO.Recordset
-
Dim rstBData As DAO.Recordset
-
-
strSQL = "SELECT DISTINCT tblBusinesses.bname FROM tblBusinesses " & _
-
"ORDER BY tblBusinesses.bname;"
-
-
Set MyDB = CurrentDb
-
-
-
'Generate a Recordset representing Unique Businessess Ascending
-
Set rstUniqueBusiness = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
-
-
With rstUniqueBusiness
-
Do While Not .EOF
-
strSQL_2 = "SELECT * From tblBusinesses Where tblBusinesses.bname = '" & _
-
![bname] & "'"
-
Set rstBData = MyDB.OpenRecordset(strSQL_2, dbOpenForwardOnly)
-
strPostCode = rstBData![postcode]
-
Do While Not rstBData.EOF 'build the Business String
-
strBusiness = strBusiness & rstBData![Business] & ", "
-
rstBData.MoveNext
-
Loop
-
Debug.Print ![bname] & ", " & strPostCode & ", " & _
-
Left$(strBusiness, Len(strBusiness) - 2)
-
strBusiness = "" 'Reset
-
.MoveNext
-
Loop
-
End With
-
-
rstUniqueBusiness.Close
-
rstBData.Close
-
Set rstUniqueBusiness = Nothing
-
Set rstBData = Nothing
OUTPUT: - a, qqq qqq, office, office, office, shop
-
b, www www, shop, office
Basically you want to RollUp a field value into one field.
A solution could be to create a function that will "rollup" the floor into one field like: -
Function fncRollUp(strBusinessName as string)
-
-
dim rs as dao.recordset
-
-
set rs = currentdb.openrecordset("select * from tblX Where 1bname='" & strBusinessName & "'")
-
While NOT rs.EOF
-
fncRollUp = fncRollUp & ", " & rs!floor
-
rs.MoveNext
-
End With
-
-
End Function
-
This function must be copy/pasted into a module and can be used in a query like: -
SELECT DISTINCT 1bname, fncRollUp([1bname]) As Floors FROM tblX ;
-
Nic;o)
Really nice approach Nico, I was totally unaware that you can call a Function recursively within the context of an SQL Statement. That's what I love about this place, I get to learn something new every day! (LOL). Again, nice job.
User functions in a query can be used "inside" Access and will be executed for every row that's extracted. You can't use this approach when passing the query directly to the Jet engine e.g. directly from VB.
Regards,
Nic;o)
User functions in a query can be used "inside" Access and will be executed for every row that's extracted. You can't use this approach when passing the query directly to the Jet engine e.g. directly from VB.
Regards,
Nic;o)
I was more interested in the fact that you can have Recursive calls to the fncRollUp() Function for each Record processed in the SQL Statement. Again, thanks for the information.
I was more interested in the fact that you can have Recursive calls to the fncRollUp() Function for each Record processed in the SQL Statement. Again, thanks for the information.
? Nothing recursive about this call in the query as it's "repeated" for every row.
A recursive function would be calling itself.
In my RollUp function the statement: -
fncRollUp = fncRollUp & ", " & rs!floor
-
Perhaps caused you to think it's recursive, but it's a string handling operation on the variable fncRollUp, so no call to the function, but the variable fncRollUp.
Nic;o)
Thank you very much.
I want to view this result at msflexgrid uisng fncRollUp, and i write this code to view
Private Sub Form_Load()
Call connect 'for database connection
' msflexgrid code to view data
Dim rs As New ADODB.Recordset 'variable
Dim i, j As Integer 'variable
rs.Open "SELECT DISTINCT bname, fncRollUp([1bname]) As Floor FROM Table2 ; ", cnn, adOpenKeyset, adLockOptimistic
With rs
msfg1.Rows = rs.RecordCount + 1 'to show Rows
msfg1.Cols = rs.Fields.Count 'to show Colum
'msfg1.Cols = rs.Fields.Count + 1 'to show Colum
For i = 0 To rs.Fields.Count - 1
msfg1.TextMatrix(0, i) = rs.Fields(i).Name 'to show field name at 1st row.
' msfg1.TextMatrix(0, i + 1) = rs.Fields(i).Name 'to show field name at 1st row.
Next i
rs.MoveFirst
For i = 1 To rs.RecordCount
For j = 0 To rs.Fields.Count + 1
msfg1.TextMatrix(i, j) = rs.Fields(j)
'msfg1.TextMatrix(i, j + 1) = rs.Fields(j)
Next j
rs.MoveNext
Next i
rs.Close
End With
its give an error message:
Run-time error ‘-214721900(80040e14)’
Undefined function ‘fncRollUp’ in expression
where i mistake, or how can i view
and Thank You again
Aziz
Hi Aziz,
I already warned: "You can't use this approach when passing the query directly to the Jet engine e.g. directly from VB."
That's exactly what you're doing. Only when executing queries from "within" Access a user defined function will work.
You can however use:
rs.Open "SELECT DISTINCT bname FROM Table2 ; "
and in the WHILE/WEND loop call the fncRollUp(rs!bname) to get the rolled up data.
Nic;o)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ken1 |
last post by:
I am going to drop a primary key from one column and create a new
column to be used as primary key in an existing database.
The old column was a date column which someone earlier though was a
good...
|
by: Philip Sherman |
last post by:
I'm trying to copy production statistics to a test database and can't
set the column statistics for a VARGRAPHIC column to match what RUNSTATS
generated on production.
The reason code and some...
|
by: Brian Brane |
last post by:
I have properties that wrap DataRow columns as in:
public int aNumber
{ get{ return m_DataRow; }
set{ m_DataRow = value; } }
If the column happens to contain DBNull, I get a cast
exception...
|
by: Amber |
last post by:
There are times when you will need to highlight or otherwise modify
the contents of a particular DataGrid row-column value based upon the
value in the column. In this example we will select the...
|
by: |
last post by:
Hello All,
I am having a lot of difficulty trying to bind a templated column, that is
programmatically created for a datagrid, to a datasource column.
I have a datasource containing 2 columns,...
|
by: Owen T. Soroke |
last post by:
Using VB.NET
I have a ListView with several columns.
Two columns contain integer values, while the remaining contain string
values.
I am confused as to how I would provide functionality to...
|
by: Bob Day |
last post by:
VS 2003, sql
How do you determine the data type of a column if its value is DBNull?
1)Table:
Column1 STRING non-nullable
2) Fill to a DataSet via DataAdapter
3) dim Data_Type_Is...
|
by: Peter Gibbs |
last post by:
I need some help with this problem.
I'm using Access 2002 with XP.
My problem is with a 2-column listbox.
My VBA code puts text data into the listbox.
The problem is that the text data...
|
by: VbaNewbee |
last post by:
I have a form with a few filters. Once the user clicks "search button", the code first evaluates my filters, then shows the query results in a List Box" titled backschedule.
I have a few text...
|
by: Lou O |
last post by:
Is it possible to use the row (index) of a list box as control source
Property for a text box?
Example: Text1.ControlSource Property is set to "= List1.Column(0,2)"
in design view.
When I open...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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: 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: 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,...
|
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...
| |