我有一个Access应用程序,它为每条记录使用UID,但它与SQL中的记录顺序不匹配。 (即我的UID为12845对应于Access中的记录号12834)
我有一个我创建的搜索框,它应该搜索Access数据库并提取它找到匹配的UID的记录,但是,我编写代码的方式是它将转到记录与UID匹配的数字(因此它将使用UID 12845转到记录号12845而不是记录12834)。
我已经坐了好几天了,我无法找到解决办法。搜索互联网并没有证明是有帮助的。如果有人知道如何匹配字符串并转到该记录与尝试自己解析记录信息,那就太棒了。
以下是我正在使用的代码示例。它需要一个日期字符串并在记录中查找字符串,获取UID,然后尝试转到相应的记录:
Private Sub FindBarCodeDate_Click()
Dim Barcode As String
Dim EndDate As String
If IsNull(BarcodeSearch.Value) Then
If IsNull(DateSearch.Value) Then
GoTo Done
Else
EndDate = DateSearch.Value
End If
Else
If IsNull(DateSearch.Value) Then
Barcode = BarcodeSearch.Value
Else
Barcode = BarcodeSearch.Value
EndDate = DateSearch.Value
End If
End If
Dim rs As New ADODB.Recordset
Dim strSql As String
Dim TSD As String
If Barcode <> "" Then
If EndDate <> "" Then
strSql = "SELECT [TSD ID] FROM dbo_barAdultCollectionData WHERE Barcode = '" & Barcode & "' AND [End Date] = '" & EndDate & "'"
On Error GoTo Done
rs.Open strSql, CurrentProject.Connection
TSD = rs.Fields.Item(0)
rs.Close
DoCmd.FindRecord TSD, acEntire, False, acSearchAll, False, acAll, True
Set rs = Nothing
Else
strSql = "SELECT [TSD ID] FROM dbo_barAdultCollectionData WHERE Barcode = '" & Barcode & "'"
On Error GoTo Done
rs.Open strSql, CurrentProject.Connection
TSD = rs.Fields.Item(0)
rs.Close
DoCmd.FindRecord FindWhat:=TSD, Match:=acEntire, MatchCase:=False, Search:=acSearchAll, SearchAsFormatted:=False, OnlyCurrentField:=acAll, FindFirst:=True
Set rs = Nothing
End If
ElseIf Barcode = "" Then
If EndDate <> "" Then
strSql = "SELECT [TSD ID] FROM dbo_barAdultCollectionData WHERE [End Date] = '" & EndDate & "'"
On Error GoTo Done
rs.Open strSql, CurrentProject.Connection
TSD = rs.Fields.Item(0)
rs.Close
DoCmd.FindRecord FindWhat:=TSD, Match:=acEntire, MatchCase:=False, Search:=acSearchAll, SearchAsFormatted:=False, OnlyCurrentField:=acAll, FindFirst:=True
Set rs = Nothing
End If
Else
Done:
SearchError.Caption = "Invalid Search Term!"
End If
End Sub
谢谢!
答案 0 :(得分:1)
首先,尝试将-11添加到UID:
TSD = CStr(Val(rs.Fields.Item(0).Value) - 11)
此外,您需要将日期值格式化为字符串表达式:
EndDate = Format(DateSearch.Value, "yyyy\/mm\/dd")
然后:
strSql = "SELECT [TSD ID] FROM dbo_barAdultCollectionData WHERE [End Date] = #" & EndDate & "#"