I have this procedure attached to a button on a form. This button works fine is I have less than 25 people on the GroupAttendance listbox. When I have more than that, I get an error message about the SQL being too complex. How can I change the code below to shortened the sql string. Getting this one to work already took a long time.
Private Sub cmdCopyNote_Click()
Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL1 As String, strSQL2 As String, strSQL3 As String
Dim ndx As Integer
Dim rowcount As Integer
DoCmd.SetWarnings False
Set frm = Forms!frmGroupAttendanceNotes
Set ctl = frm!GroupAttendance2
‘Store all clientid in groupattendance in a temporary table
strSQL1 = “INSERT INTO tblTemp ( clientid )”
strSQL1 = strSQL1 & “SELECT dsdtcmas.clientid ”
strSQL1 = strSQL1 & “FROM dsdtcmas ”
strSQL1 = strSQL1 & “WHERE (dsdtcmas.clientid)= ‘”
Me!GroupAttendance2.Selected(0) = True
‘make sure all clientids are highlighted
For ndx = 0 To Me!GroupAttendance2.ListCount
Me!GroupAttendance2.Selected(ndx) = True
Next ndx
‘build the sql string by adding one clientid after another as it iterates down the listbox
For Each varItem In ctl.ItemsSelected
strSQL1 = strSQL1 & ctl.ItemData(varItem) & “‘ OR (dsdtcmas.clientid) = ‘”
Next varItem
‘final query statement after deleting the (dsdtcmas.clientid) = ‘” at the end of statement – 27 spaces
strSQL1 = Left$(strSQL1, Len(strSQL1) – 27)
‘MsgBox strSQL1
DoCmd.RunSQL strSQL1
strSQL2 = “INSERT INTO tblClientNotes ( act, servdate, Notes, NoteDate, Clinician, SessionStart, SessionEnd, clientid ) ”
strSQL2 = strSQL2 & “SELECT tblGroupNotes.act, tblGroupNotes.servdate, tblGroupNotes.Notes, tblGroupNotes.NoteDate, tblGroupNotes.Clinician, tblGroupNotes.SessionStart, tblGroupNotes.SessionEnd, tblTemp.clientid ”
strSQL2 = strSQL2 & “FROM tblGroupNotes, tblTemp ”
strSQL2 = strSQL2 & “WHERE (tblGroupNotes.NoteID)=[Forms]![frmGroupNotes]![NoteID];”
‘MsgBox (strSQL2)
DoCmd.RunSQL strSQL2
Call LongLoop
strSQL3 = “DELETE tblTemp.clientid FROM tblTemp;”
DoCmd.RunSQL strSQL3
DoCmd.SetWarnings True
For ndx = 0 To Me!GroupAttendance2.ListCount
Me!GroupAttendance.Selected(ndx) = False
Next ndx
End Sub