简体   繁体   English

Access 2010 SELECT字符串

[英]Access 2010 SELECT string

I am having difficulty building a proper 'SELECT' statement in Access 2010 VBA. 我在Access 2010 VBA中构建正确的“SELECT”语句时遇到了困难。 I am trying to pull 3 fields from a table using an ADODB.Recordset. 我试图使用ADODB.Recordset从表中提取3个字段。 I have done this numerous times before with no problems, but this time I am trying to accomplish it based on a user entered number that is part of one of the field values. 我之前已经做了很多次没有任何问题,但这次我试图根据用户输入的数字来完成它,该数字是其中一个字段值的一部分。 So whereas the full field may be T6825LZ, the user should be able to enter 6825 and the SELECT statement find the correct record. 因此,虽然完整字段可能是T6825LZ,但用户应该能够输入6825并且SELECT语句找到正确的记录。

My code thus far is: 到目前为止我的代码是:

 Dim rsTID As New ADODB.Recordset
 Dim searchTID As String
 Dim selectString As String

 searchTID = "T" & TID
 selectString = "SELECT * FROM Agents WHERE TID LIKE " & searchTID & ""

 rsTID.Open selectString, CurrentProject.Connection, adOpenForwardOnly, adLockOptimistic

 If Not rsTID.EOF Then
       TID = rsTID.Fields("TID")
       LastName = rsTID.Fields("LastName")
       FirstName = rsTID.Fields("FirstName")
 End If

In the code above, 'TID' in the line searchTID = "T" & TID refers to the TextBox on the Access Form where the user enters the 4 digit number. 在上面的代码中,行searchTID = "T" & TID指的是访问表单上用户输入4位数字的TextBox。 TID in the selectString refers to the Field in the Agents table. selectString TID是指Agents表中的Field。 A bit confusing I know, but it's what I've been given to work with :) 我知道有点混乱,但这是我给予的工作:)

What is ultimately happening is that I'm getting an error on the rsTID.Open line stating No value given for one or more required parameters . 最终发生的是我在rsTID.Open行上出现错误,指出没有给出一个或多个必需参数的值 This doesn't make any sense as according to MSDN all the parameters of the ADODB.RecordSet.Open statement are optional, and even if they were not, they are all present. 这没有任何意义,因为根据MSDN, ADODB.RecordSet.Open语句的所有参数都是可选的,即使它们不是,它们也都存在。

Can anyone please help identify the issue here, this is getting quite frustrating. 任何人都可以帮助确定这里的问题,这是非常令人沮丧的。 :) :)

Thanks in advance... 提前致谢...

您的搜索字词需要引用,并且您需要包含LIKE搜索的通配符:

"SELECT * FROM Agents WHERE TID LIKE '*" & searchTID & "*'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM