473,394 Members | 2,052 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,394 software developers and data experts.

String take rows from query to populate a cell

I am attempting to make a table which contains a list of room numbers and a yes/no column populate a cell in another table or textbox IF the second cell is true.

example

RoomNum | Clean
110-1 | 1
111-1 | 0
112-1 | 1
113-2 | 1
114-1 | 0
114-2 | 1

Next table

Clean Rooms 110-1,112-1,113-2,114-2
Sep 25 '18 #1

✓ answered by twinnyfo

I had a few minutes to share this:

Expand|Select|Wrap|Line Numbers
  1. Public Function ConcatQuery(strSQL As String) As String
  2. On Error GoTo EH
  3.     Dim db          As DAO.Database
  4.     Dim rst         As DAO.Recordset
  5.     Dim strResult   As String
  6.  
  7.     strResult = ""
  8.     Set db = CurrentDb()
  9.     Set rst = db.OpenRecordset(strSQL)
  10.     With rst
  11.         If Not (.BOF And .EOF) Then
  12.             If .Fields.Count > 1 Then
  13.                 MsgBox "Only one field allowed!"
  14.             Else
  15.                 .MoveFirst
  16.                 Do While Not .EOF
  17.                     If strResult = "" Then
  18.                         strResult = .Fields(0)
  19.                     Else
  20.                         strResult = strResult & _
  21.                             ", " & .Fields(0)
  22.                     End If
  23.                     .MoveNext
  24.                 Loop
  25.             End If
  26.         End If
  27.         .Close
  28.     End With
  29.     db.Close
  30.     Set rst = Nothing
  31.     Set db = Nothing
  32.  
  33.     ConcatQuery = strResult
  34.  
  35.     Exit Function
  36. EH:
  37.     MsgBox "There was an error Concatenating!" & vbCrLf & vbCrLf & _
  38.         "Error: " & Err.Number & vbCrLf & _
  39.         "Description: " & Err.Description & vbCrLf & vbCrLf & _
  40.         "Please contact your Database Administrator.", vbCritical, "WARNING!"
  41.     Exit Function
  42. End Function
You can use it in your query like this:

Expand|Select|Wrap|Line Numbers
  1. ConcatQuery("SELECT RoomNum FROM tblRooms WHERE Clean;") AS CleanRooms
Hope this hepps!

20 1064
twinnyfo
3,653 Expert Mod 2GB
crmrpage,

Welcome to Bytes!

First, just about any expert on this forum will tell you about the same thing concerning your request. We would all advise against creating or updating a second table based upon the results of your first table.

What you are looking for is a list of clean rooms, yes? Then, to get that information, you simply create a query that looks at your first table, and returns only those records which have the Clean field set to "True".

It would look something similar to this:

Expand|Select|Wrap|Line Numbers
  1. SELECT RoomNum 
  2. FROM tblRooms 
  3. WHERE Clean;
This would produce the following results:

Expand|Select|Wrap|Line Numbers
  1. 110-1
  2. 112-1
  3. 113-2
  4. 114-2
Hope that hepps!
Sep 25 '18 #2
That is part of the issue, it must populate a single textbox or somehow be written in a string for a printable report. The query part I'm okay with, it is putting it as a string to show
110-1,112-1,113-2,114-2 in a single text box.
I am attempting to not make the user input the data in an additional way for the printable report.
Similar to how it appears in the notes2 section of the report. Unfortunately that one I have it where they have to enter each one individualy

I only need it that way for the purpose of displaying in a report



Attached Images
File Type: jpg Capture1.jpg (190.8 KB, 221 views)
File Type: jpg Capture2.jpg (91.9 KB, 251 views)
Sep 25 '18 #3
twinnyfo
3,653 Expert Mod 2GB
You can do that easily through a function. How comfortable are you working with VBA? It is really an easy function to create....
Sep 25 '18 #4
twinnyfo
3,653 Expert Mod 2GB
I had a few minutes to share this:

Expand|Select|Wrap|Line Numbers
  1. Public Function ConcatQuery(strSQL As String) As String
  2. On Error GoTo EH
  3.     Dim db          As DAO.Database
  4.     Dim rst         As DAO.Recordset
  5.     Dim strResult   As String
  6.  
  7.     strResult = ""
  8.     Set db = CurrentDb()
  9.     Set rst = db.OpenRecordset(strSQL)
  10.     With rst
  11.         If Not (.BOF And .EOF) Then
  12.             If .Fields.Count > 1 Then
  13.                 MsgBox "Only one field allowed!"
  14.             Else
  15.                 .MoveFirst
  16.                 Do While Not .EOF
  17.                     If strResult = "" Then
  18.                         strResult = .Fields(0)
  19.                     Else
  20.                         strResult = strResult & _
  21.                             ", " & .Fields(0)
  22.                     End If
  23.                     .MoveNext
  24.                 Loop
  25.             End If
  26.         End If
  27.         .Close
  28.     End With
  29.     db.Close
  30.     Set rst = Nothing
  31.     Set db = Nothing
  32.  
  33.     ConcatQuery = strResult
  34.  
  35.     Exit Function
  36. EH:
  37.     MsgBox "There was an error Concatenating!" & vbCrLf & vbCrLf & _
  38.         "Error: " & Err.Number & vbCrLf & _
  39.         "Description: " & Err.Description & vbCrLf & vbCrLf & _
  40.         "Please contact your Database Administrator.", vbCritical, "WARNING!"
  41.     Exit Function
  42. End Function
You can use it in your query like this:

Expand|Select|Wrap|Line Numbers
  1. ConcatQuery("SELECT RoomNum FROM tblRooms WHERE Clean;") AS CleanRooms
Hope this hepps!
Sep 25 '18 #5
Great, Thanks! I'll give this a shot and let you know.
Sep 25 '18 #6

I'm struggling to get this to work. I created and compiled the module and entered the second part into the text box but it keeps popping up a message box requesting the information.
The text box also keeps saying that the expression contains Invalid Syntax
Attached Images
File Type: png table.PNG (11.9 KB, 94 views)
Sep 27 '18 #7



To better explain I added a my table in a screen shot. I need for the list of [BedNum] to be placed into a textbox for every patient that has a blood glucose checked, one for drips, one for foley, one for central, one for restraints and last one for isolation. The image shown here is how I want it to look but it is currently set up where the user has to input the data twice.
Attached Images
File Type: png CHARGE.PNG (7.0 KB, 106 views)
Sep 27 '18 #8
twinnyfo
3,653 Expert Mod 2GB
I'm struggling to get this to work. I created and compiled the module and entered the second part into the text box but it keeps popping up a message box requesting the information.
I do not understand what you mean when you say you've "entered the second part into the text box".

Please explain.

This code works perfectly on my machine when I send it a properly formatted SQL string.
Sep 27 '18 #9
Where was I supposed to place this part?
Expand|Select|Wrap|Line Numbers
  1. ConcatQuery("SELECT RoomNum FROM tblRooms WHERE Clean;") AS CleanRooms
Sep 27 '18 #10
twinnyfo
3,653 Expert Mod 2GB
This is where you have made it a bit difficult for us....

Based on Post #8 (and previous posts), it "appears" that you have a report based on the information that you want to list. That report must have an underlying table or query as its RecordSource.

Since we don't have that RecordSource, we can't tell you exactly where to put it.

Additionally, based upon your initial post, you wanted to list Rooms that were "Clean." However, since that initial post, I have not seen any images or any indicators on any report that shows anything about "Clean" rooms; so, I can't point to anywhere on any of your images to say, "Put it there!"

Please--I'm not trying to be a jerk. I just can't advise you without additional information. Based upon what I see so far, you don't appear to be a complete beginner. But, you have to help us out with more details.
Sep 27 '18 #11
NeoPa
32,556 Expert Mod 16PB
If you check post #5 you'll see that it's to go in your query.

It's actually in SQL format so add it into the SQL of the query. Alternatively, if adding it in the QBE (Query By Example) window it would look like :
Expand|Select|Wrap|Line Numbers
  1. CleanRooms: ConcatQuery("SELECT RoomNum FROM tblRooms WHERE Clean;")
Sep 27 '18 #12
I used the clean part as an example, the other is identical in that they are just yes/no
I placed an image showing how my table is setup (7). The only part that I need for this issue is the BEDNUM and the yes/no fields such as clean or bloodglucose. The user puts a check for each patient that has each of those listed such as FOLEY, DRIPS, BLOOD GLUCOSE, RESTRAINTS, CENTRAL, ISOLATION etc.

What I need is to have a text box in my report that displays all rooms with a check mark in FOLEY for example in a single string as displayed in the image attached(8).

I'm not a beginner, I just have been out of the game for 15+ years. I once made a living off of making MS ACCESS databases and have used Access since the early 90's while in the military but had to switch to teaching Math and Computer Science in 2002 for the last 15 years and now I work at a VA hospital as a Clerk and am trying to improve their assignment sheet system for nurses so I am a little rusty
Sep 27 '18 #13
I got it to work! YAY :) Thank you NeoPa for pointing out my blindness. Totally overlooked that part!
Sep 27 '18 #14
Worked great then when closed and reopened it gave a



Not sure why
Attached Images
File Type: png UndefinedFunction.PNG (6.1 KB, 94 views)
Sep 27 '18 #15
twinnyfo
3,653 Expert Mod 2GB
Did you forget to save the Module that had the Function or forget to set it at Public instead of Private?
Sep 27 '18 #16
It is public if you are referring to here?



And it is saved
Attached Images
File Type: png Public.PNG (6.1 KB, 174 views)
Sep 27 '18 #17
PhilOfWalton
1,430 Expert 1GB
crmrpage

You need to create a Standard Module, NOT a Class Module. Past in Twinnyfo's code and when you save it, call the module anything you like OTHER THAN "ConcatQuery". Access sometimes gets confused if a Module Name & a Function Name are the same.

Personally, I tend to call it ModQueries, and any functions used in Queries goes into this module. Sim
Sep 27 '18 #18
That was it thank you!
Sep 27 '18 #19
twinnyfo
3,653 Expert Mod 2GB
@All - My guess is the naming convention you used for the Module. That will always cause a problem with Name resolution.

You should also always get into a habit of a proper naming convention; e.g., Modules begin with "mod" or "bas", Forms beign with "frm", Tables with "tbl". You get the picture. This allows you--and other users who are troubleshooting your work, to quickly understand what refers to what.
Sep 27 '18 #20
NeoPa
32,556 Expert Mod 16PB
It's funny how, by getting a couple of things wrong during this process, you've learned far more than you would have if you'd got everything right first off.

I'm very pleased to see you got it all working in the end. Well done :-)
Sep 27 '18 #21

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

Similar topics

10
by: RH | last post by:
<SELECT name="txtServices" size="3" multiple> <OPTION value="Massage">Massage</OPTION> <OPTION value="Facials">Facials</OPTION> <OPTION value="Wet & Dry Body Treatments">Wet &amp; Dry Body...
2
by: Dee | last post by:
Hi I have to <asp:button>'s one on top of the other in a cell of a Table layout. When I put a <br> I get too big a gap. Without it they are touching. The only way I can think of separating them...
0
by: tfsmag | last post by:
i'm sure this is really easy, but can some one help me figure out how to prune a string in a cell on a gridview? like left(string,5) to return the first 5 characters of the string? I tried...
2
by: mpmason14 | last post by:
i know there is a way to populate one combobox based on another, but i've never done it myself and am looking for some help. i have a table that has SwitchNo and NumberofPorts as two columns. i...
0
by: Java25 | last post by:
I am trying to insert a date to my access database and i get the error below. please help: Syntax error in string in query expression ''Fri Jun 08 10:49:00 CAT 2007)'. I only want in the...
0
shahjapan
by: shahjapan | last post by:
Can anyone tell me using C# or VB.NET Word Interop how to insert sub Rows in a Cell. Regards, Japan Shah
4
by: srinathvs | last post by:
Hi, I have an access db that I am trying to query from a vb6 program. I've the following code: Dim sSQLQuery As String sSQLQuery = "SELECT * FROM TblData WHERE ID = " & Chr(39) & ID &...
5
by: rolltide | last post by:
I've seen many similar threads, but despite repeated efforts I cannot figure out my problem. I am running Access 2003, VB 6.5, Office XP Pro. Code excerpt is below (you can see where I've tried...
4
by: bullfrog83 | last post by:
I wrote a function that takes a comma-separated string, parses out the individual values, places quotations around each value and a comma in-between each value so that they can be processed in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.