PC Review


Reply
Thread Tools Rate Thread

Crosstab or Concatenate multple text selections for one value

 
 
=?Utf-8?B?Q2hyaXM=?=
Guest
Posts: n/a
 
      25th Jan 2006
I have a table that is a reference for a plan name and it's id. Some plans
have multiple ID's. What I need is for a query to produce the following out
of this table format:

Plan ID
BCBS MA 21
UPMC 12
BCBS MA 25

Query Would produce

PLAN ID
BCBS MA 21,25
UPMC 12

I do not know SQL, is this possible, I've herad people say concatenate
etc... but I only have one table with this fields...
 
Reply With Quote
 
 
 
 
Allen Browne
Guest
Posts: n/a
 
      25th Jan 2006
Chris, the function below is aircode to give you a guide as to how to loop
through the matching records and concatenate the values.

The idea is to put this into a standard module (Modules tab of Database
window). You can then use it in a query such as this:
SELECT Plan, GetIDs([Plan])
FROM Table1
GROUP BY Plan;

Function GetIDs(varPlan As Variant) As Variant
Dim rs As DAO.Recordset
Dim strSql As String
Dim strOut As String
Dim lngLen As Long
Const strcSep = ","

'Loop through all matching records, concatenating the ID values.
If Not IsNull(varPlan) Then
strSql = "SELECT ID FROM Table1 WHERE Plan = """ & varPlan & """;"
Set rs = dbEngine(0)(0).OpenRecordset(strSql)
Do While Not rs.EOF
strOut = strOut & rs!ID & strcSep
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End If

'Return the string without trailing separator, or Null.
lngLen = Len(strOut) - Len(strcSep)
If lngLen > 0 Then
GetIDs = Left(strOut, lngLen)
Else
GetIDs = Null
End If
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Chris" <(E-Mail Removed)> wrote in message
news:3A9D9492-7DCD-444D-8627-(E-Mail Removed)...
>I have a table that is a reference for a plan name and it's id. Some plans
> have multiple ID's. What I need is for a query to produce the following
> out
> of this table format:
>
> Plan ID
> BCBS MA 21
> UPMC 12
> BCBS MA 25
>
> Query Would produce
>
> PLAN ID
> BCBS MA 21,25
> UPMC 12
>
> I do not know SQL, is this possible, I've herad people say concatenate
> etc... but I only have one table with this fields...



 
Reply With Quote
 
=?Utf-8?B?Q2hyaXM=?=
Guest
Posts: n/a
 
      25th Jan 2006
Allen, any help if I give youthe field names and table names?

"Allen Browne" wrote:

> Chris, the function below is aircode to give you a guide as to how to loop
> through the matching records and concatenate the values.
>
> The idea is to put this into a standard module (Modules tab of Database
> window). You can then use it in a query such as this:
> SELECT Plan, GetIDs([Plan])
> FROM Table1
> GROUP BY Plan;
>
> Function GetIDs(varPlan As Variant) As Variant
> Dim rs As DAO.Recordset
> Dim strSql As String
> Dim strOut As String
> Dim lngLen As Long
> Const strcSep = ","
>
> 'Loop through all matching records, concatenating the ID values.
> If Not IsNull(varPlan) Then
> strSql = "SELECT ID FROM Table1 WHERE Plan = """ & varPlan & """;"
> Set rs = dbEngine(0)(0).OpenRecordset(strSql)
> Do While Not rs.EOF
> strOut = strOut & rs!ID & strcSep
> rs.MoveNext
> Loop
> rs.Close
> Set rs = Nothing
> End If
>
> 'Return the string without trailing separator, or Null.
> lngLen = Len(strOut) - Len(strcSep)
> If lngLen > 0 Then
> GetIDs = Left(strOut, lngLen)
> Else
> GetIDs = Null
> End If
> End Function
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Chris" <(E-Mail Removed)> wrote in message
> news:3A9D9492-7DCD-444D-8627-(E-Mail Removed)...
> >I have a table that is a reference for a plan name and it's id. Some plans
> > have multiple ID's. What I need is for a query to produce the following
> > out
> > of this table format:
> >
> > Plan ID
> > BCBS MA 21
> > UPMC 12
> > BCBS MA 25
> >
> > Query Would produce
> >
> > PLAN ID
> > BCBS MA 21,25
> > UPMC 12
> >
> > I do not know SQL, is this possible, I've herad people say concatenate
> > etc... but I only have one table with this fields...

>
>
>

 
Reply With Quote
 
Allen Browne
Guest
Posts: n/a
 
      25th Jan 2006
You can work that out, Chris.

Just create a query that looks right, and then switch it to SQL View (View
menu.) There's an example of what you need.

You will learn much more if you work through that than if someone does it
for you.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Chris" <(E-Mail Removed)> wrote in message
news:9ACE070F-DF59-4C44-A7A7-(E-Mail Removed)...
> Allen, any help if I give youthe field names and table names?
>
> "Allen Browne" wrote:
>
>> Chris, the function below is aircode to give you a guide as to how to
>> loop
>> through the matching records and concatenate the values.
>>
>> The idea is to put this into a standard module (Modules tab of Database
>> window). You can then use it in a query such as this:
>> SELECT Plan, GetIDs([Plan])
>> FROM Table1
>> GROUP BY Plan;
>>
>> Function GetIDs(varPlan As Variant) As Variant
>> Dim rs As DAO.Recordset
>> Dim strSql As String
>> Dim strOut As String
>> Dim lngLen As Long
>> Const strcSep = ","
>>
>> 'Loop through all matching records, concatenating the ID values.
>> If Not IsNull(varPlan) Then
>> strSql = "SELECT ID FROM Table1 WHERE Plan = """ & varPlan &
>> """;"
>> Set rs = dbEngine(0)(0).OpenRecordset(strSql)
>> Do While Not rs.EOF
>> strOut = strOut & rs!ID & strcSep
>> rs.MoveNext
>> Loop
>> rs.Close
>> Set rs = Nothing
>> End If
>>
>> 'Return the string without trailing separator, or Null.
>> lngLen = Len(strOut) - Len(strcSep)
>> If lngLen > 0 Then
>> GetIDs = Left(strOut, lngLen)
>> Else
>> GetIDs = Null
>> End If
>> End Function
>>
>>
>> "Chris" <(E-Mail Removed)> wrote in message
>> news:3A9D9492-7DCD-444D-8627-(E-Mail Removed)...
>> >I have a table that is a reference for a plan name and it's id. Some
>> >plans
>> > have multiple ID's. What I need is for a query to produce the
>> > following
>> > out
>> > of this table format:
>> >
>> > Plan ID
>> > BCBS MA 21
>> > UPMC 12
>> > BCBS MA 25
>> >
>> > Query Would produce
>> >
>> > PLAN ID
>> > BCBS MA 21,25
>> > UPMC 12
>> >
>> > I do not know SQL, is this possible, I've herad people say concatenate
>> > etc... but I only have one table with this fields...



 
Reply With Quote
 
=?Utf-8?B?Q2hyaXM=?=
Guest
Posts: n/a
 
      25th Jan 2006
Allen,

DId so, but get all sorts of erros, DOA errors, rs errors,...sorry thank
you for you help

"Allen Browne" wrote:

> You can work that out, Chris.
>
> Just create a query that looks right, and then switch it to SQL View (View
> menu.) There's an example of what you need.
>
> You will learn much more if you work through that than if someone does it
> for you.
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Chris" <(E-Mail Removed)> wrote in message
> news:9ACE070F-DF59-4C44-A7A7-(E-Mail Removed)...
> > Allen, any help if I give youthe field names and table names?
> >
> > "Allen Browne" wrote:
> >
> >> Chris, the function below is aircode to give you a guide as to how to
> >> loop
> >> through the matching records and concatenate the values.
> >>
> >> The idea is to put this into a standard module (Modules tab of Database
> >> window). You can then use it in a query such as this:
> >> SELECT Plan, GetIDs([Plan])
> >> FROM Table1
> >> GROUP BY Plan;
> >>
> >> Function GetIDs(varPlan As Variant) As Variant
> >> Dim rs As DAO.Recordset
> >> Dim strSql As String
> >> Dim strOut As String
> >> Dim lngLen As Long
> >> Const strcSep = ","
> >>
> >> 'Loop through all matching records, concatenating the ID values.
> >> If Not IsNull(varPlan) Then
> >> strSql = "SELECT ID FROM Table1 WHERE Plan = """ & varPlan &
> >> """;"
> >> Set rs = dbEngine(0)(0).OpenRecordset(strSql)
> >> Do While Not rs.EOF
> >> strOut = strOut & rs!ID & strcSep
> >> rs.MoveNext
> >> Loop
> >> rs.Close
> >> Set rs = Nothing
> >> End If
> >>
> >> 'Return the string without trailing separator, or Null.
> >> lngLen = Len(strOut) - Len(strcSep)
> >> If lngLen > 0 Then
> >> GetIDs = Left(strOut, lngLen)
> >> Else
> >> GetIDs = Null
> >> End If
> >> End Function
> >>
> >>
> >> "Chris" <(E-Mail Removed)> wrote in message
> >> news:3A9D9492-7DCD-444D-8627-(E-Mail Removed)...
> >> >I have a table that is a reference for a plan name and it's id. Some
> >> >plans
> >> > have multiple ID's. What I need is for a query to produce the
> >> > following
> >> > out
> >> > of this table format:
> >> >
> >> > Plan ID
> >> > BCBS MA 21
> >> > UPMC 12
> >> > BCBS MA 25
> >> >
> >> > Query Would produce
> >> >
> >> > PLAN ID
> >> > BCBS MA 21,25
> >> > UPMC 12
> >> >
> >> > I do not know SQL, is this possible, I've herad people say concatenate
> >> > etc... but I only have one table with this fields...

>
>
>

 
Reply With Quote
 
=?Utf-8?B?VG9tIFdpY2tlcmF0aA==?=
Guest
Posts: n/a
 
      25th Jan 2006
Chris,

Make sure you have a reference set to the "DAO 3.6 Object Library", since
the code that Allen provided is DAO code. This reference is not set by
default in new databases created with Access 2000 or 2002. It is set by
default with new database created with Access 2003.

To set this reference, click on Tools > References when in the Visual Basic
Editor. Scroll down the list until you find this reference. In rare cases you
may need to browse for the file, in which case you want to browse for the
DAO360.dll file. After setting this reference and clicking on OK to dismiss
the references dialog, verify that you have Option Explicit as the second
line of code in your module. Here's why you want to see this:
http://www.access.qbuilt.com/html/ge...tml#VBEOptions

Then click on Debug > Compile ProjectName. Correct any compile time errors
that you might have.


Tom

http://www.access.qbuilt.com/html/ex...tributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________

"Chris" wrote:

Allen,

DId so, but get all sorts of erros, DOA errors, rs errors,...sorry thank
you for you help


 
Reply With Quote
 
=?Utf-8?B?VG9tIFdpY2tlcmF0aA==?=
Guest
Posts: n/a
 
      25th Jan 2006
Correction: It's called the "Microsoft DAO 3.6 Object Library".


Tom

http://www.access.qbuilt.com/html/ex...tributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________

"Tom Wickerath" wrote:

> Chris,
>
> Make sure you have a reference set to the "DAO 3.6 Object Library", since
> the code that Allen provided is DAO code. This reference is not set by
> default in new databases created with Access 2000 or 2002. It is set by
> default with new database created with Access 2003.
>
> To set this reference, click on Tools > References when in the Visual Basic
> Editor. Scroll down the list until you find this reference. In rare cases you
> may need to browse for the file, in which case you want to browse for the
> DAO360.dll file. After setting this reference and clicking on OK to dismiss
> the references dialog, verify that you have Option Explicit as the second
> line of code in your module. Here's why you want to see this:
> http://www.access.qbuilt.com/html/ge...tml#VBEOptions
>
> Then click on Debug > Compile ProjectName. Correct any compile time errors
> that you might have.
>
>
> Tom
>
> http://www.access.qbuilt.com/html/ex...tributors.html
> http://www.access.qbuilt.com/html/search.html
> __________________________________________
>
> "Chris" wrote:
>
> Allen,
>
> DId so, but get all sorts of erros, DOA errors, rs errors,...sorry thank
> you for you help
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate Values in Crosstab Query Jeff H Microsoft Access Queries 5 24th Jul 2009 06:40 PM
How can I make multple worksheet text all show in one box as a tot NavyGunner Microsoft Excel Misc 1 6th Aug 2008 08:56 PM
searching text and conditional format with multple conditions diaare Microsoft Excel Worksheet Functions 4 17th Dec 2007 08:12 PM
Drawing lines around multple text boxes in Detail section =?Utf-8?B?UmF0aGVyQmVlSG9tZQ==?= Microsoft Access Reports 4 26th Jul 2007 06:01 AM
help creating function that combines multple text box values dibblm Microsoft Access VBA Modules 1 16th Mar 2005 06:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:07 PM.