473,385 Members | 1,829 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.

Combining Names in A Textbox on a ms access report or form

i have 2 tables which are linked together

table 1:
[Event] { EventID, EventName, CustomerID }

table 2:
[Customer] { CustomerID, CustomerFirstName, CustomerLastName, AGE }

they are join by CustomerID obvouisly, but i want a report that shows multiple customers in one textbox for one Event.

I.E. Report would look like:

Event: Saturday Customers: Charlie Brown, Alex Maine, Roger Wells
Event: Monday Customers: Dr. Watson, Dr. Green

i would like them to be joined by commas as well, i haven't been able to come up with any function that will combine several rows together of linked ids

any thoughts
Aug 18 '07 #1
10 3586
Scott Price
1,384 Expert 1GB
i have 2 tables which are linked together

table 1:
[Event] { EventID, EventName, CustomerID }

table 2:
[Customer] { CustomerID, CustomerFirstName, CustomerLastName, AGE }

they are join by CustomerID obvouisly, but i want a report that shows multiple customers in one textbox for one Event.

I.E. Report would look like:

Event: Saturday Customers: Charlie Brown, Alex Maine, Roger Wells
Event: Monday Customers: Dr. Watson, Dr. Green

i would like them to be joined by commas as well, i haven't been able to come up with any function that will combine several rows together of linked ids

any thoughts
The default view that you are going to get from any query that returns the records you want is going to be a column. You are wanting to go from column view to a row view, which is not going to be very easy nor intuitive to accomplish!

One approach would be to populate a hidden form (or possibly would require mulitiple hidden forms, don't know as I've not tried this) with the values you wish to concatenate, then draw from this hidden form(s) to populate the values of a label or text box on your report.

The actual code to concatenate (this doesn't include the code to open the hidden form, validate the info, handle errors, etc) would look something like this:

In the on open event of your report:

Expand|Select|Wrap|Line Numbers
  1. label1.caption = Forms![YourFormName]![YourTextBoxName] & ": " & Forms![YourFormName]![YourTextBoxName1] & ", " & Forms![YourFormName]![YourTextBoxName2]  
etc etc etc.

This vb code would concatenate three text boxes on one form with a : and space between the first two and a , and space between the second two, then assign the value to a label creatively named label1 on your report.

Might be quite a bit easier to just resign yourself to seeing the thing in column view and adjust your report accordingly!

I'll be happy to be corrected by anyone that has any better ideas on how to do this!

Regards,
Scott
Aug 19 '07 #2
FishVal
2,653 Expert 2GB
i have 2 tables which are linked together

table 1:
[Event] { EventID, EventName, CustomerID }

table 2:
[Customer] { CustomerID, CustomerFirstName, CustomerLastName, AGE }

they are join by CustomerID obvouisly, but i want a report that shows multiple customers in one textbox for one Event.

I.E. Report would look like:

Event: Saturday Customers: Charlie Brown, Alex Maine, Roger Wells
Event: Monday Customers: Dr. Watson, Dr. Green

i would like them to be joined by commas as well, i haven't been able to come up with any function that will combine several rows together of linked ids

any thoughts
One of the way to do it is to write VBA function getting as argument CustomerID and returning concatenated string you are mentioning. The function code
  • opens a Recordset retrieving all records from [Customer] table where [CistomerID]=<argument passed to the function>
  • iterates all records in the Recordset concatenating necessary fields ([CustomerFirstName], [CustomerLastName]) into a string
  • returns the string
This function may be used in query as calculated field or form/report control as ControlSource.
Aug 19 '07 #3
Is it possible to join these rows with a select statement or grouping them together in a string value?
Aug 21 '07 #4
Scott Price
1,384 Expert 1GB
Is it possible to join these rows with a select statement or grouping them together in a string value?

Afraid not! I've just worked out a code sample in my test database that will do what you want. To explain:

The process of making this work in your database will be as follows:

Create a query that pulls the values you want to see. I called mine qryEventCustomer, and it had this SQL:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblCustomer.CustomerID, tblEvent.EventName, tblCustomer.CustomerFirstname & " " & tblCustomer.CustomerLastName AS Customer
  2. FROM tblCustomer INNER JOIN tblEvent ON tblCustomer.CustomerID = tblEvent.CustomerID
  3. WHERE (((tblEvent.EventName)="Saturday Morning"));
Next, create an unbound form (I named it frmEventCustomer) in design view. On this form place a listbox named lstEventCustomer. When the wizard comes up, have the listbox look up the values in your qryEventCustomer. Save and close the form.

Next open your vba editor window and on the left-hand side click on the + sign next to the Modules folder. If there are no modules, insert one.

Place this code in the module:

Expand|Select|Wrap|Line Numbers
  1. Public Function EventCustomer() As String
  2.  
  3. Dim MyArray() As Variant
  4. Dim evcust As String
  5. Dim cust As String
  6. Dim ev As String
  7. Dim rs As DAO.Recordset
  8. Dim ls As Integer
  9. Dim intCounter As Integer
  10. Dim list As Integer
  11. DoCmd.OpenForm "frmEventCustomer", , , , , acHidden
  12.  
  13. Set rs = Forms!frmEventCustomer!lstEventCustomer.Recordset
  14. ls = Forms!frmEventCustomer!lstEventCustomer.ListCount
  15.             With rs
  16.                 .MoveFirst
  17.                 MyArray() = .GetRows(ls)
  18.             End With
  19.  
  20. ev = MyArray(1, 0)
  21. cust = MyArray(2, 0)
  22. list = 0
  23. evcust = ev & ": " & cust
  24. For intCounter = 1 To (ls - 1)
  25.         list = list + 1
  26.         cust = MyArray(2, list)
  27.         evcust = evcust & ", " & cust
  28. Next
  29.  
  30. EventCustomer = evcust
  31. DoCmd.Close acForm, "frmEventCustomer", acSaveNo
  32. End Function
Next, on your report, create a label named lblEventCustomer. In the OnOpen event of your report, place this code:
Expand|Select|Wrap|Line Numbers
  1. Call EventCustomer
  2. Me!lblEventCustomer.Caption = EventCustomer
Save everything, test... Let us know if this works for you, or if you have any further trouble implementing it!

Regards,
Scott
Aug 21 '07 #5
well i have followed what you have done, and you have done a good job, I see the logic behind this process a little bit better, however, I am stuck on an error that needs debugging: it says user function not defined... when i enter in debug mode it has rs As DAO.Recordset

I fiddled around with this making it a string/integer/recordset but i still haven't been able to correct this error

ps: when i do chance DAO.Recordset to recordset it says that rs is a miss match.
Aug 23 '07 #6
JKing
1,206 Expert 1GB
Check to make sure you have a reference to the Microsoft DAO Object Library. You do this from Tools > References in the VBA editor.
Aug 23 '07 #7
ok that was a simple fix, however now it's saying Subscripts are out of range with MyArrays() or ev and cust
Aug 23 '07 #8
Scott Price
1,384 Expert 1GB
ok that was a simple fix, however now it's saying Subscripts are out of range with MyArrays() or ev and cust
Had to do a spot of refiguring... for some reason, the ls variable was counting the wrong number of records in the recordset... Anyway, try this instead:
Expand|Select|Wrap|Line Numbers
  1. Public Function EventCustomer() As String
  2.  
  3. Dim MyArray() As Variant
  4. Dim evcust As String
  5. Dim cust As String
  6. Dim ev As String
  7. Dim rs As DAO.Recordset
  8. Dim ls As Integer
  9. Dim intCounter As Integer
  10. Dim list As Integer
  11. DoCmd.OpenForm "frmEventCustomer", , , , , acHidden
  12.  
  13. Set rs = Forms!frmEventCustomer!lstEventCustomer.Recordset
  14. Erase MyArray
  15.             With rs
  16.                 .MoveFirst
  17.                 .MoveLast
  18.                 ls = .RecordCount
  19.                 .MoveFirst
  20.                 MyArray() = .GetRows(ls)
  21.             End With
  22.  
  23. ev = MyArray(1, 0)
  24. cust = MyArray(2, 0)
  25. list = 0
  26. evcust = ev & ": " & cust
  27. For intCounter = 1 To (ls - 1)
  28.         list = list + 1
  29.         cust = MyArray(2, list)
  30.         evcust = evcust & ", " & cust
  31. Next
  32. EventCustomer = evcust
  33. DoCmd.Close acForm, "frmEventCustomer", acSaveNo
  34. Erase MyArray
  35. rs.Close
  36.  
  37. End Function
Regards,
Scott
Aug 23 '07 #9
ok totally works perfectly now... the problem was that for the array I only had it called for the customername in the form listbox, once i added the event and then modified the array dimension to:

Expand|Select|Wrap|Line Numbers
  1. ev = MyArray(0, 0)
  2. cust = MyArray(1, 0)
it fixed my subsript out of range problems


THANKS TO ALL!!!

Now i am just going to play with if for a bit so that i dont need to do a form for each event. and maybe even using this tech of storing information I might try running a query store it and place the recordset inside an array and gather that array into a list string

again thank you!
Aug 23 '07 #10
Scott Price
1,384 Expert 1GB
ok totally works perfectly now... the problem was that for the array I only had it called for the customername in the form listbox, once i added the event and then modified the array dimension to:

Expand|Select|Wrap|Line Numbers
  1. ev = MyArray(0, 0)
  2. cust = MyArray(1, 0)
it fixed my subsript out of range problems


THANKS TO ALL!!!

Now i am just going to play with if for a bit so that i dont need to do a form for each event. and maybe even using this tech of storing information I might try running a query store it and place the recordset inside an array and gather that array into a list string

again thank you!
Glad it's working for you! and you're welcome.

Regards,
Scott
Aug 23 '07 #11

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

Similar topics

2
by: Paul .V. | last post by:
I have a report which I call to print from a form. For security reasons I have created a global variable: Const Company = "This Test Company" I call that constant whereever I want the company...
1
by: kmnotes04 | last post by:
I have a data entry form that contains drop-down lists such as 'Assigned to:' with a list of staff member names. Those names end up as numerical codes in the main table ('ProjectInfo') of the...
4
by: Rodrigo DeJuana | last post by:
Howdy, I'm new to this .net stuff and really have little to no training. Im trying to create a new page for a web form, so i have been pretty much jsut coping code. I having some issue with...
3
by: IRAS Blues | last post by:
Hi all, I've got a form that consists of a bunch of textboxes and also file upload inputs. For both sets of fields, I need to be able to add in additional elements on the fly. This is done by an...
4
by: justin tyme | last post by:
Hello Experts! I would like to combine (which may not be the correct technical term) two text fields from the same table in a query. Specifically, text field A and text field B are both lists of...
3
by: John Smith | last post by:
I have two text fields in a table. One is Height, one is width. Some examples of what might be each field: Height Width 35' 35' 8' 6' 4 to 6...
4
by: nallen05 | last post by:
Is there a standardized recommendation for combining names and namespaces into a single URI? I found a post on the Stylus Studio forum asking the same question, the response was "use James...
10
by: H | last post by:
Hi, I have the following address fields in a table: flat_number house_name_or_number street village postal_town county postcode
7
by: robert.waters | last post by:
I have an Access database frontend linked via ODBC to a large (gigabytes) mysql database. I need to view a large amount of data in a a textbox (variable up to 300K), but I receive a 'there isnt...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.