title: Query done by SQL showing same results multiple times (multiple copies of same record in query)
body: <p>I've got a query being made via an SQL statement. I'm getting the correct results, just each result is repeated 4 times in the query. I'm confused as to why I am getting 4 copies of the same result.</p>
  
  <p>Here is my code for the form:</p>
  
  <pre><code>Dim strTables As String
  Private Sub btnFollowUpQs_Click()
      If (btnFollowUpQs.Value = True) Then
          Set db = CurrentDb()
          Set rs = db.OpenRecordset(btnFollowUpQs.Caption)
          Dim fld As DAO.Field
          For Each fld In rs.Fields
              Me.lstVariablesFollowUpQs.AddItem (fld.Name)
          Next
          Set fld = Nothing
          db.Close
          strTables = strTables + "," + btnFollowUpQs.Caption
      Else
          lstVariablesFollowUpQs.RowSource = ""
          strTables = Replace(strTables, "," + btnFollowUpQs.Caption, "")
      End If
      Debug.Print strTables
  End Sub
  
  Private Sub btnBaseline_Click()
      If (btnBaseline.Value = True) Then
          Set db = CurrentDb()
          Set rs = db.OpenRecordset(btnBaseline.Caption)
          Dim fld As DAO.Field
          For Each fld In rs.Fields
              Me.lstVariablesBaseline.AddItem (fld.Name)
          Next
          Set fld = Nothing
          db.Close
          strTables = strTables + "," + btnBaseline.Caption
      Else
          lstVariablesBaseline.RowSource = ""
          strTables = Replace(strTables, "," + btnBaseline.Caption, "")
      End If
          Debug.Print strTables
  End Sub
  
  Private Sub btnTreatments_Click()
      If (btnTreatments.Value = True) Then
          Set db = CurrentDb()
          Set rs = db.OpenRecordset(btnTreatments.Caption)
          Dim fld As DAO.Field
          For Each fld In rs.Fields
              Me.lstVariablesTreatments.AddItem (fld.Name)
          Next
          Set fld = Nothing
          db.Close
          strTables = strTables + "," + btnTreatments.Caption
      Else
          lstVariablesTreatments.RowSource = ""
          strTables = Replace(strTables, "," + btnTreatments.Caption, "")
      End If
          Debug.Print strTables
  End Sub
  
  Private Sub btnQuestionnaires_Click()
      If (btnQuestionnaires.Value = True) Then
          Set db = CurrentDb()
          Set rs = db.OpenRecordset(btnQuestionnaires.Caption)
          Dim fld As DAO.Field
          For Each fld In rs.Fields
              Me.lstVariablesQuestionnaires.AddItem (fld.Name)
          Next
          Set fld = Nothing
          db.Close
          strTables = strTables + "," + btnQuestionnaires.Caption
      Else
          lstVariablesQuestionnaires.RowSource = ""
          strTables = Replace(strTables, "," + btnQuestionnaires.Caption, "")
      End If
          Debug.Print strTables
  End Sub
  
  Private Function createSQL(ByRef lstCtrl As Control, v() As String) As String
          Dim count As Integer
          count = 0
          With lstCtrl
              For Each varSelected In .ItemsSelected
                  If Not IsNull(varSelected) Then
                      Dim sel As String
                      sel = (lstCtrl.Column(0, varSelected))
                      strSQL = strSQL + sel &amp; " " &amp; v(count) &amp; " AND "
                  End If
                  count = count + 1
              Next
          End With
  
          createSQL = strSQL
  End Function
  
  Private Sub btnBuildQuery_Click()
  
      If Left(strTables, 1) = "," Then
          strTables = Right(strTables, Len(strTables) - 1)
      End If
  
      Dim tables() As String
      tables = Split(strTables, ",")
  
  
      Dim strSQL As Variant
      strSQL = "SELECT * FROM " &amp; strTables &amp; " WHERE "
  
      For Each Table In tables
  
          Dim values() As String
  
          Select Case Table
  
          Case "tblPatientHistoryBaseline"
              values = Split(txtSearchValueBaseline, ",")
              strSQL = strSQL + createSQL(lstVariablesBaseline, values)
          Case "tblQuestionnaires"
              values = Split(txtSearchValueQuestionnaires, ",")
              strSQL = strSQL + createSQL(lstVariablesQuestionnaires, values)
          Case "tblTreatments"
              values = Split(txtSearchValueTreatments, ",")
             strSQL = strSQL + createSQL(lstVariablesTreatments, values)
          Case "tblFollowUpQs"
              values = Split(txtSearchValueFollowUpQs, ",")
             strSQL = strSQL + createSQL(lstVariablesFollowUpQs, values)
          End Select
  
      Next
          strSQL = Left(strSQL, Len(strSQL) - 5)
  
      Debug.Print (strSQL)
      Dim qdf As QueryDef
      Set qdf = CurrentDb.CreateQueryDef("qry" &amp; txtQueryName, strSQL)
      DoCmd.OpenQuery qdf.Name
  End Sub
  </code></pre>
  
  <p>Here's what the query returns:</p>
  
  <pre><code>LastName    FirstName    ID          Visit
  Line        Georgia      1234567     0
  Line        Georgia      1234567     0
  Line        Georgia      1234567     0
  Line        Georgia      1234567     0
  Doe         Jane         0123456     0
  Doe         Jane         0123456     0
  Doe         Jane         0123456     0
  Doe         Jane         0123456     0
  </code></pre>
  
  <p>Here's a sample SQL that I've generated:</p>
  
  <pre><code>SELECT * FROM tblQuestionnaires, tblPatientHistoryBaseline WHERE Visit = 0 AND LastName Like '*e*'
  </code></pre>
  
  <p>I'm guessing its something wrong with my SQL but I cannot figure out what.</p>
  
  <p>Thanks!</p>
  
  <p>'EDIT-----------------
  LastName is from tblPatientHistoryBaseline, 
  Visit is from tblQuestionnaires</p>