473,385 Members | 1,958 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,385 software developers and data experts.

column from Row value

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
Aug 24 '08 #1
9 2127
Stewart Ross
2,545 Expert Mod 2GB
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
Aug 24 '08 #2
nico5038
3,080 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. Function fncRollUp(strBusinessName as string)
  2.  
  3. dim rs as dao.recordset
  4.  
  5. set rs = currentdb.openrecordset("select * from tblX Where 1bname='" & strBusinessName & "'")
  6.    While NOT rs.EOF
  7.        fncRollUp = fncRollUp & ", " & rs!floor
  8.        rs.MoveNext
  9.    End With
  10.  
  11. End Function
  12.  
This function must be copy/pasted into a module and can be used in a query like:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT 1bname, fncRollUp([1bname]) As Floors  FROM tblX ;
  2.  
Nic;o)
Aug 24 '08 #3
ADezii
8,834 Expert 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim strSQL_2 As String
  3. Dim strBusiness As String
  4. Dim strPostCode As String
  5. Dim MyDB As DAO.Database
  6. Dim rstUniqueBusiness As DAO.Recordset
  7. Dim rstBData As DAO.Recordset
  8.  
  9. strSQL = "SELECT DISTINCT tblBusinesses.bname FROM tblBusinesses " & _
  10.          "ORDER BY tblBusinesses.bname;"
  11.  
  12. Set MyDB = CurrentDb
  13.  
  14.  
  15. 'Generate a Recordset representing Unique Businessess Ascending
  16. Set rstUniqueBusiness = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
  17.  
  18. With rstUniqueBusiness
  19.   Do While Not .EOF
  20.     strSQL_2 = "SELECT * From tblBusinesses Where tblBusinesses.bname = '" & _
  21.                 ![bname] & "'"
  22.     Set rstBData = MyDB.OpenRecordset(strSQL_2, dbOpenForwardOnly)
  23.      strPostCode = rstBData![postcode]
  24.       Do While Not rstBData.EOF         'build the Business String
  25.         strBusiness = strBusiness & rstBData![Business] & ", "
  26.         rstBData.MoveNext
  27.       Loop
  28.       Debug.Print ![bname] & ", " & strPostCode & ", " & _
  29.                   Left$(strBusiness, Len(strBusiness) - 2)
  30.       strBusiness = ""      'Reset
  31.     .MoveNext
  32.   Loop
  33. End With
  34.  
  35. rstUniqueBusiness.Close
  36. rstBData.Close
  37. Set rstUniqueBusiness = Nothing
  38. Set rstBData = Nothing
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. a, qqq qqq, office, office, office, shop
  2. b, www www, shop, office
Aug 25 '08 #4
ADezii
8,834 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. Function fncRollUp(strBusinessName as string)
  2.  
  3. dim rs as dao.recordset
  4.  
  5. set rs = currentdb.openrecordset("select * from tblX Where 1bname='" & strBusinessName & "'")
  6.    While NOT rs.EOF
  7.        fncRollUp = fncRollUp & ", " & rs!floor
  8.        rs.MoveNext
  9.    End With
  10.  
  11. End Function
  12.  
This function must be copy/pasted into a module and can be used in a query like:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT 1bname, fncRollUp([1bname]) As Floors  FROM tblX ;
  2.  
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.
Aug 25 '08 #5
nico5038
3,080 Expert 2GB
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)
Aug 25 '08 #6
ADezii
8,834 Expert 8TB
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.
Aug 25 '08 #7
nico5038
3,080 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. fncRollUp = fncRollUp & ", " & rs!floor
  2.  
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)
Aug 25 '08 #8
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
Aug 26 '08 #9
nico5038
3,080 Expert 2GB
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)
Aug 26 '08 #10

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

Similar topics

5
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...
12
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...
4
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...
0
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...
2
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,...
19
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...
3
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...
4
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...
0
VbaNewbee
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...
4
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...
0
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,...
0
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$) { } ...
0
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
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...

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.