Solved

How can I get multiple records displayed in one line

Posted on 2014-12-08
8
117 Views
Last Modified: 2014-12-09
Hi

I have a query that returns the below data:

PDCode      PDData                      PortZoneID
W              AP 280   Qu              3
W              AS 264   Qu              3
W              AUH2 282   Qu      3
W              AUH2 282   SG      3
W              BA 278   Qu              3
W              BU1 288   SG              3
W              CO 288   SG              3
W              F1 254   SG              3
W              F2 234   SG              3
W              GA 278   Qu              3

What I would like it to display is:

W AP 280 Qu~AS 264 Qu~AUH2 282   Qu SG~ BA 278   Qu~BU1 288   SG~CO 288   SG~F1 254   SG~F2 234   SG~GA 278   Qu

Is this possible in Access?

Thanks

Ajae
0
Comment
Question by:ajaeclarke
  • 5
  • 2
8 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40488171
Private Sub Command0_Click()

Put a button on your form to run code like this:

Dim rst As Recordset
Dim strRecords As String
Set rst = CurrentDb.OpenRecordset("YourQuery")
    rst.MoveFirst
    Do Until rst.EOF
       strRecords = strRecords & rst!PDCode & "~" & rst!PDData
        rst.MoveNext
    Loop
   
   Me.txtRecordsString = strRecords

rst.Close
Set rst = Nothing

End Sub

Put a textbox on your form named: txtRecordsString

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40488176
Private Sub Command0_Click()

Dim rst As Recordset
Dim strRecords As String
Set rst = CurrentDb.OpenRecordset("YourQuery")
    rst.MoveFirst
    Do Until rst.EOF
       strRecords = strRecords & rst!PDCode & "~" & rst!PDData
        rst.MoveNext
    Loop
   
   Me.txtRecordsString = strRecords

rst.Close
Set rst = Nothing

End Sub

Open in new window

0
 
LVL 1

Author Comment

by:ajaeclarke
ID: 40488191
Thanks JeffCoachman.

I run the code and I get

Run-time error '3061':

Too few parameters.  Expected 2.

and Debug highlights the below:

Set rst = CurrentDb.OpenRecordset("qryPDTOPPRICE")

There are two parameters in the query.  Would this cause this error?
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 40488192
I think this code will work better:

Dim rst As Recordset
Dim strFirstLetter As String
Dim strRecords As String
Set rst = CurrentDb.OpenRecordset("YourTable")
    rst.MoveFirst
    strFirstLetter = rst!PDCode
    Do Until rst.EOF
       strRecords = strRecords & "~" & rst!PDData
        rst.MoveNext
    Loop
   strRecords = Right(strRecords, Len(strRecords) - 1)
   strRecords = strFirstLetter & " " & strRecords
   Me.txtRecordsString = strRecords

rst.Close
Set rst = Nothing

Open in new window



sample of output in a form and a report is attached
db5.mdb
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40488221
Yes, ...you never mentioned parameters in your original question.

This will not work with a parameter if it is not supplied.

But here again, you are not saying what field the parameter is in?, ...how you are specifying the parameter...
is it a simple prompt, ..or is the parameter referencing a form value...
0
 
LVL 1

Author Comment

by:ajaeclarke
ID: 40488236
All good....I have made a couple of changes and now you code is working wonderfully.

Thanks for your help.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40488238
Great!
;-)

Jeff
0
 
LVL 57
ID: 40488597
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question