I'm afraid I've tried all of these with no luck. I had kept things simple at first just to give an example of what I'm trying to do, but now I've included my full code below. Basically, the code is designed to go through a form, reading each child_key and its corresponding batch_key field and run queries against each of four different "data file" letter tables to determine which table the child and batch number are listed in, then display that data file number on the form.
I know some of this can be done with loops, but I ran into issues with that too and don't have time to work through that now.
You will find each suggestion commented out in the appropriate section (under "Dynamically build query string"). When a solution is found, only ONE line will be there and the others will be removed.
Private Sub cmdFindLetters_Click()
Dim cmd As New ADODB.Command
Dim strChildKey As String
Dim strBatchKey As String
Dim rstMaterials As New ADODB.Recordset
Dim intloop As Integer
Dim strDF As String
Dim strDFVal As String
Dim intKeyNumloop As Integer
Dim intChildKeyNum As String
Dim intBatchKeyNum As String
On Error Resume Next
Call OpenDB
With cmd
.activeconnection = Application.CurrentProject.Connection
.Prepared = True
End With
intloop = 1
Do
'Loop through child key text fields, populating DF text fields in form.
intKeyNumloop = 1
Do While intKeyNumloop < 13
If intKeyNumloop = 1 And Not Len(Me.txtChildKey1) = 0 Then
strChildKey = Me.txtChildKey1
strBatchKey = Me.txtBatchKey1
ElseIf intKeyNumloop = 2 And Not Len(Me.txtChildKey2) = 0 Then
strChildKey = Me.txtChildKey2
strBatchKey = Me.txtBatchKey2
ElseIf intKeyNumloop = 3 And Not Len(Me.txtChildKey3) = 0 Then
strChildKey = Me.txtChildKey3
strBatchKey = Me.txtBatchKey3
ElseIf intKeyNumloop = 4 And Not Len(Me.txtChildKey4) = 0 Then
strChildKey = Me.txtChildKey4
strBatchKey = Me.txtBatchKey4
ElseIf intKeyNumloop = 5 And Not Len(Me.txtChildKey5) = 0 Then
strChildKey = Me.txtChildKey5
strBatchKey = Me.txtBatchKey5
ElseIf intKeyNumloop = 6 And Not Len(Me.txtChildKey6) = 0 Then
strChildKey = Me.txtChildKey6
strBatchKey = Me.txtBatchKey6
ElseIf intKeyNumloop = 7 And Not Len(Me.txtChildKey7) = 0 Then
strChildKey = Me.txtChildKey7
strBatchKey = Me.txtBatchKey7
ElseIf intKeyNumloop = 8 And Not Len(Me.txtChildKey8) = 0 Then
strChildKey = Me.txtChildKey8
strBatchKey = Me.txtBatchKey8
ElseIf intKeyNumloop = 9 And Not Len(Me.txtChildKey9) = 0 Then
strChildKey = Me.txtChildKey9
strBatchKey = Me.txtBatchKey9
ElseIf intKeyNumloop = 10 And Not Len(Me.txtChildKey10) = 0 Then
strChildKey = Me.txtChildKey10
strBatchKey = Me.txtBatchKey10
ElseIf intKeyNumloop = 11 And Not Len(Me.txtChildKey11) = 0 Then
strChildKey = Me.txtChildKey11
strBatchKey = Me.txtBatchKey11
ElseIf intKeyNumloop = 12 And Not Len(Me.txtChildKey12) = 0 Then
strChildKey = Me.txtChildKey12
strBatchKey = Me.txtBatchKey12
Else
Exit Sub
End If
'Loop through letter queries until match is found.
strDFVal = 1
Do
'Determine which letter DF to query
If strDFVal = 1 Then strDF = "7"
If strDFVal = 2 Then strDF = "10"
If strDFVal = 3 Then strDF = "17"
If strDFVal = 4 Then strDF = "24"
If strDFVal = 5 Then strDF = "25"
'Dynamically build query string
If strDF = "10" Or strDF = "17" Then
'strSQL = "SELECT * from qryDF" & strDF & " where n_child_key like '*" & strChildKey & "'*" & " and batch_key = " & "'" & strBatchKey & "'" & ";"
'strSQL = "SELECT * from qryDF" & strDF & " where n_child_key like " & "*" & strChildKey & "*" & " and batch_key = " & "'" & strBatchKey & "'" & ";"
'strSQL = "SELECT * from qryDF" & strDF & " where n_child_key like '*" & Replace(strChildKey, "'", "''") & "*'" & ";"
'strSQL = "SELECT * from qryDF" & strDF & " where n_child_key like """ & "*" & strChildKey & "*" & """ and batch_key = " & "'" & strBatchKey & "'" & ";"
ElseIf strDF = "7" Or strDF = "24" Then
'strSQL = "SELECT * from qryDF" & strDF & " where child_key like '*" & strChildKey & "'*" & " and batch_key = " & "'" & strBatchKey & "'" & ";"
'strSQL = "SELECT * from qryDF" & strDF & " where child_key like " & "*" & strChildKey & "*" & " and batch_key = " & "'" & strBatchKey & "'" & ";"
'strSQL = "SELECT * from qryDF" & strDF & " where child_key like '*" & Replace(strChildKey, "'", "''") & "*'" & ";"
'strSQL = "SELECT * from qryDF" & strDF & " where child_key like """ & "*" & strChildKey & "*" & """ and batch_key = " & "'" & strBatchKey & "'" & ";"
End If
'populate recordset
cmd.CommandType = adCmdText
cmd.commandtext = strSQL
Set rstMaterials = cmd.Execute()
'If records are returned, populate appropriate field on form.
If Not rstMaterials.EOF Then
If intKeyNumloop = 1 Then
Me.txtDF1 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 2 Then
Me.txtDF2 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 3 Then
Me.txtDF3 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 4 Then
Me.txtDF4 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 5 Then
Me.txtDF5 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 6 Then
Me.txtDF6 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 7 Then
Me.txtDF7 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 8 Then
Me.txtDF8 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 9 Then
Me.txtDF9 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 10 Then
Me.txtDF10 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 11 Then
Me.txtDF11 = rstMaterials.Fields("DF")
ElseIf intKeyNumloop = 12 Then
Me.txtDF12 = rstMaterials.Fields("DF")
End If
strDFVal = 5
End If
strDFVal = strDFVal + 1
Loop While strDFVal <= 5
intKeyNumloop = intKeyNumloop + 1
Loop
intloop = intloop + 1
Loop While intloop <= 5
End Sub