Public Function RemoveFormReference(SQLCode As String) As String
'   The purpose of this function is to take an SQL statement and convert
'   a reference to a form control into a literal value
'For example, in SQL code, we need to change this:
'WHERE (((tblAmounts.intType) = [Forms]![frmTypeSelector]![cmbType]))
'OR this:
'WHERE (((tblAmounts.intType) = Forms!frmTypeSelector!cmbType))
'...to this, if the value is a number:
'WHERE (((tblAmounts.intType) = 1))
'...or this, if the value is a string:
'WHERE (((tblAmounts.strName)='Dennis'))
'...or this, if the value is a Date:
'WHERE (((tblAmounts.DateField)=#10/1/2015#))
    Dim intFormRefPos As Integer, strFormName As String, strCtlName As String
    Dim strTemp As String
'   If no form references are found, just return the string argument
'   If a form reference IS found, manipulate the string argument first, then return it
'   Find the first type of form reference (see examples above)
    intFormRefPos = InStr(1, SQLCode, "[Forms]![")
    If intFormRefPos > 0 Then
'       Extract the form name and control name from the SQL code
        strFormName = Mid(SQLCode, intFormRefPos + 9)
        strFormName = Left(strFormName, InStr(1, strFormName, "]") - 1)
        strCtlName = Mid(SQLCode, intFormRefPos + 9 + Len(strFormName) + 3)
        strCtlName = Left(strCtlName, InStr(1, strCtlName, "]") - 1)
        
'       Insert the literal value in the place of the control reference
        strTemp = Left(SQLCode, intFormRefPos - 1)
        If IsNumeric(Forms(strFormName).Controls(strCtlName)) = False Then
            If IsDate(Forms(strFormName).Controls(strCtlName)) Then
                strTemp = strTemp & "#"
            Else
                strTemp = strTemp & "'"
            End If
        End If
        strTemp = strTemp & Forms(strFormName).Controls(strCtlName)
        If IsNumeric(Forms(strFormName).Controls(strCtlName)) = False Then
            If IsDate(Forms(strFormName).Controls(strCtlName)) Then
                strTemp = strTemp & "#"
            Else
                strTemp = strTemp & "'"
            End If
        End If
        strTemp = strTemp & Mid(SQLCode, intFormRefPos + 9 + Len(strFormName) + 3 + Len(strCtlName) + 1)
        SQLCode = strTemp
    Else
'       Find the second type of form reference (see examples above)
        intFormRefPos = InStr(1, SQLCode, "Forms!")
        If intFormRefPos > 0 Then
'           Extract the form name and control name from the SQL code
            strFormName = Mid(SQLCode, intFormRefPos + 6)
            strFormName = Left(strFormName, InStr(1, strFormName, "!") - 1)
            strCtlName = Mid(SQLCode, intFormRefPos + 6 + Len(strFormName) + 1)
            strCtlName = Left(strCtlName, InStr(1, strCtlName, ")") - 1)
'           Insert the literal value in the place of the control reference
            strTemp = Left(SQLCode, intFormRefPos - 1)
            If IsNumeric(Forms(strFormName).Controls(strCtlName)) = False Then
                If IsDate(Forms(strFormName).Controls(strCtlName)) Then
                    strTemp = strTemp & "#"
                Else
                    strTemp = strTemp & "'"
                End If
            End If
            strTemp = strTemp & Forms(strFormName).Controls(strCtlName)
            If IsNumeric(Forms(strFormName).Controls(strCtlName)) = False Then
                If IsDate(Forms(strFormName).Controls(strCtlName)) Then
                    strTemp = strTemp & "#"
                Else
                    strTemp = strTemp & "'"
                End If
            End If
            strTemp = strTemp & Mid(SQLCode, intFormRefPos + 6 + Len(strFormName) + 1 + Len(strCtlName))
            SQLCode = strTemp
        End If
    End If
    RemoveFormReference = SQLCode
End Function