Results 1 to 6 of 6
  1. #1
    Annnnn is offline Novice
    Windows 10 Access 2007
    Join Date
    Feb 2018
    Posts
    17

    Question result SQL in msgbox

    I’m looking for a way to show my SQL results in a msgbox.
    I am able to show 1 value, the first match, in the msgbox but I would like to have the whole list. Which is approx. 5 rows. This is the code I am using now


    Code:
     
    Private Sub btCPCT_Click()
    Dim a As String
    Dim b As DAO.Recordset
    Dim patientnm As String
     
    patientnm = patientnm & ", '" & Me.txPatientID & "'"
    patientnm = Right(patientnm, Len(patientnm) - 1)
     
    a = "SELECT Patient_name, Sample_name " & _
         "FROM tblSamples " & _
         "WHERE Patient_name IN (" & patientnm & ")" & _
         "AND Source = 'Blood'" & _
         "ORDER BY Sample_name DESC"
    Set b = CurrentDb.OpenRecordset(a)
     
    msgBox b.Fields("Sample_name")
    b.Close
    End Sub
    So me.txtpatient is the value I am searching in the main table tblsamples the result of this search ar multiple samples names (Sample_name). With this code it only shows 1 sample and I want it to show all the sample names which match the entered patient ID

    Hope it's possible XD

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you wouldnt use a msgbox for multiple records,
    thats what a datasheet is for.

    you dont use a screwdriver to hammer in nails.

  3. #3
    Annnnn is offline Novice
    Windows 10 Access 2007
    Join Date
    Feb 2018
    Posts
    17
    the thing is that I am using a form to add samples to the table. It is possible that the patient, to which the sample belongs, is already registered in the table. When this is the case a roman 2 has to be added to the sample ID. This
    is so that it remains a unique value, it can also be that it is the third, fourth sample etc. This is why I would like the user to visually see what the other sample ID are that are registered.

    If you have a better method....

  4. #4
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    If you really want to do this, you could loop through the table/query one record at a time then print each result to your message box on a separate line
    Something like this (untested)

    Code:
    Private Sub btCPCT_Click()Dim a As String
    Dim b As DAO.Recordset
    Dim patientnm As String
    Dim strText As String
     
    patientnm = patientnm & ", '" & Me.txPatientID & "'"
    patientnm = Right(patientnm, Len(patientnm) - 1)
    strText=""
     
    a = "SELECT Patient_name, Sample_name " & _
         "FROM tblSamples " & _
         "WHERE Patient_name IN (" & patientnm & ")" & _
         "AND Source = 'Blood'" & _
         "ORDER BY Sample_name DESC"
    Set b = CurrentDb.OpenRecordset(a)
    
    With b
        .MoveLast
        .MoveFirst
        Do Until .EOF
        strText=strText & vbCrLf & !Sample_Name
        .MoveNext
        Loop
        .Close
    End With
    
    
    Set b=Nothing
     
    msgBox "The sample names for patient " & Me.txtPatientID & " are " & vbCrLf & strText
    
    
    End Sub
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  5. #5
    Annnnn is offline Novice
    Windows 10 Access 2007
    Join Date
    Feb 2018
    Posts
    17
    Perfect that works !
    Many thanks

  6. #6
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Excellent. All the better for not being tested!
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Msgbox
    By Swh86 in forum Forms
    Replies: 1
    Last Post: 09-20-2017, 03:39 AM
  2. Replies: 1
    Last Post: 09-14-2017, 10:19 AM
  3. Yes No msgbox
    By imintrouble in forum Access
    Replies: 3
    Last Post: 10-14-2011, 02:24 PM
  4. MsgBox
    By Mtyetti in forum Forms
    Replies: 4
    Last Post: 07-27-2011, 01:51 PM
  5. If Query result Is Null...MsgBox..Okl
    By Bruce in forum Forms
    Replies: 28
    Last Post: 03-10-2010, 10:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums