Help needed in creating either a crosstab query or an equivalent VBA function.

Hi Experts,

I need to have the following in a sub form within a main form.

From the table Patients_Daily_Medications

with the fields below.

I need a crosstab query that would show me Medications as column heading and sequence as row heading and TimeGiven as the values.

See attached what the data looks like, and what I am trying to accomplish (not completed as you can see...)

At the end I would need this query as the record source of a form that has a patientID and a date as fields, so this should only return values of a sibgle patient at a particular day.

PS- Note TimeGiven has a date+time, we need to extract the Time only.

PS -, a VBA function that will open a recordset of that table, Accept two params (PatientID,Day) and return the following would also serve the purpose.
ALBUTEROL: 9:00 AM, 11:00 AM      BUDESONIDE: 11:AM, 1:00PM, 5:00 PM      MIRALAX: 3:PM

Thanks in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
I think that a form based on a crosstab query is not a very good idea...since crosstab is readonly...unless you use it as an optical that you put it as a "subform' along with a helping form to input the "data"
bfuchsAuthor Commented:
so that you put it as a "subform' along with a helping form to input the "data"
Correct, this is what I'm trying to do.
However I am open to a vba solution as well.
bfuchsAuthor Commented:
This is the function I was looking for...
Public Function GetMedications(PatID As Long, Day As Date) As String
    Dim rs As DAO.Recordset, s As String, sMed As String
    Set rs = CurrentDb.OpenRecordset("Select Medication, TimeGiven from Patients_Daily_Medications where PatientID = " & PatID & " and Day = #" & Day & "# and medication is not null and TimeGiven is not null order by Medication, Sequence")
    If rs.EOF Then
        GetMedications = ""
        Exit Function
    End If
    While Not rs.EOF
        If sMed <> rs("Medication") Then
            s = s & "    " & rs("Medication") & ";"
        End If
        s = s & ", " & Format(rs("TimeGiven"), "HH:MM")
        sMed = rs("Medication")
    GetMedications = Trim(Replace(s, ";,", ";"))
End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark EdwardsChief Technology OfficerCommented:
So you answered your own question......

I don't think you get any points for answering your own question, but I'll let the moderator be the judge of that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.