Hi everyone and thanks in advance for any help on this. I have some VBA code that I would like to use to loop through different criteria in a SQL statement. I actually pulled the SQL statement seen in the VBA from a query that I know is functioning properly.
I have three tables: tblSampleLogin, tblDataEntrySto rage and AR_tblActivePar ameters. I want to loop through the Parameter column (Short Text)in AR_tblActivePar ameters to pull out data from tblDataEntrySto rage. I feel like I am missing something, like brackets or quotes, and I have tried a various combination of them but I keep getting a Type Mismatch error.
Below is my code. I am new to VBA and its quite possible that I just can't see where the issue is. I am also new to this site, so please let me know if I need to provide more information.
I have three tables: tblSampleLogin, tblDataEntrySto rage and AR_tblActivePar ameters. I want to loop through the Parameter column (Short Text)in AR_tblActivePar ameters to pull out data from tblDataEntrySto rage. I feel like I am missing something, like brackets or quotes, and I have tried a various combination of them but I keep getting a Type Mismatch error.
Below is my code. I am new to VBA and its quite possible that I just can't see where the issue is. I am also new to this site, so please let me know if I need to provide more information.
Code:
Public Function TestLoop()
On Error GoTo TestLoop_Err
Dim rsLoop As Recordset
Dim strSQL As String
Dim db As Database
Set db = CurrentDb
Set rsLoop = db.OpenRecordset("Select * from AR_tblActiveParameters;")
rsLoop.MoveFirst
While Not rsLoop.EOF
strSQL = "SELECT DISTINCT TOP 10 tblDataEntryStorage.[Replicate Number], tblDataEntryStorage.Parameter, tblDataEntryStorage.[Date Processed], tblDataEntryStorage.[Batch ID], tblDataEntryStorage.Fraction, tblDataEntryStorage.Method, tblSampleLogin.MatrixID"
strSQL = strSQL & "FROM tblSampleLogin INNER JOIN tblDataEntryStorage ON tblSampleLogin.PhysisSampleID = tblDataEntryStorage.[Sample ID]"
strSQL = strSQL & "WHERE (((tblDataEntryStorage.[Replicate Number]) = 'bs1') AND ((tblDataEntryStorage.Parameter) = '" & [rsLoop]![Parameter] & "') AND ((tblDataEntryStorage.Method) Like " * 8270 * ") AND ((tblSampleLogin.MatrixID)=6))"
strSQL = strSQL & "ORDER BY tblDataEntryStorage.[Batch ID] DESC"
db.Execute (strSQL), dbFailOnError
rsLoop.MoveNext
Wend
rsLoop.Close
Set rsLoop = Nothing
Set db = Nothing
TestLoop_Exit:
Exit Function
TestLoop_Err:
MsgBox Err.Description & " in TestLoop"
Resume TestLoop_Exit
End Function
Comment