字段名称混乱

时间:2014-07-11 17:35:44

标签: sql vba ms-access access-vba

rs2.FindFirst "[aniin] ='" & strTemp & "'"

aniin是函数中SQL的别名。

也试过......

rs2.FindFirst (niin = newdata)

是我尝试将字段名niin与strSQL2中的记录值隔离开来。我所有的尝试都失败了。我试图确保用户输入的内容与SQL字符串中的列表匹配。

Private Function IsPartOfAEL(newdata) As Boolean

On Error GoTo ErrTrap
Dim db2 As DAO.Database
Dim rs2 As DAO.Recordset
Dim strTemp As String

strSQL2 = "SELECT tbl_ael_parts.master_ael_id, tbl_master_niin.niin as aniin " & vbCrLf & _
"FROM tbl_master_niin INNER JOIN tbl_ael_parts ON tbl_master_niin.master_niin_id = tbl_ael_parts.master_niin_id " & vbCrLf & _
"WHERE (((tbl_ael_parts.master_ael_id)= " & Forms!frm_qry_niin_local!master_ael_id & "));"

Set db2 = CurrentDb
Set rs2 = db2.OpenRecordset(strSQL2)
strTemp = newdata
If rs2.RecordCount <> 0 Then
    rs2.FindFirst "[aniin] ='" & strTemp & "'"
    If rs2.NoMatch Then
        IsPartOfAEL = False
    Else
        IsPartOfAEL = True
    End If
Else
    MsgBox "Query Returned Zero Records", vbCritical
    Exit Function
End If

rs.Close

Set rs2 = Nothing
Set db2 = Nothing

ExitHere:
    Exit Function
ErrTrap:
    MsgBox Err.description
    Resume ExitHere

End Function

1 个答案:

答案 0 :(得分:1)

首先:在构建查询字符串时,不应该像vbCrLf那样包含常量。查询解析器并不关心是否有换行符,实际上这有时会导致问题。

您的代码似乎什么也没做,以验证newdata中的值是否存在于tbl_ael_parts中,并且与当前在frm_qry_niin_local上显示的值master_ael_id值相关联。如果是这样,那么只需使用DCount,或将其用于您的查询:

strSQL2 = "SELECT tbl_ael_parts.master_ael_id INNER JOIN tbl_ael_parts ON
  tbl_master_niin.master_niin_id = tbl_ael_parts.master_niin_id WHERE (((tbl_ael_parts.master_ael_id)= 
  " & Forms!frm_qry_niin_local!master_ael_id & ") AND niin=" & newdata & ");"

Dim rst As DAO.Recordset
Set rst = currentdb.OPenrecordset(strsql2)

If (rst.EOF and rst.BOF) Then
   ' no records returned
Else
   ' records found
End If

如果niin是文本字段:

strSQL2 = "SELECT tbl_ael_parts.master_ael_id INNER JOIN tbl_ael_parts ON
  tbl_master_niin.master_niin_id = tbl_ael_parts.master_niin_id WHERE (((tbl_ael_parts.master_ael_id)= 
  " & Forms!frm_qry_niin_local!master_ael_id & ") AND (niin='" & newdata & "'));"

如果niin和master_ael_id都是文本字段:

strSQL2 = "SELECT tbl_ael_parts.master_ael_id INNER JOIN tbl_ael_parts ON
  tbl_master_niin.master_niin_id = tbl_ael_parts.master_niin_id WHERE (((tbl_ael_parts.master_ael_id)= 
  '" & Forms!frm_qry_niin_local!master_ael_id & "') AND (niin='" & newdata & "'));"