MS Access DLookUp故障

时间:2018-03-09 15:28:48

标签: vba ms-access

我在MS Access中发生了一个非常奇怪的问题,我似乎无法弄清楚。

总结:我有一个来自Sharepoint的表,它连接到我的MS Access数据库和Ms Access数据库中的Person表。我从Sharepoint表中逐行提取信息并将其添加到Person Table。

但是,在添加新数据之前,我必须检查我的表中是否已存在该特定人员。我使用DLookup函数检查'Lastname','Firstname'和'Date created'。

这里所有事情都有所不同。 DLookup 为Person Table中已经存在的几乎一半记录返回NULL。

在使用 DLookup 语句中的条件玩了很多我的结论是'Date created'参数存在问题,但我尝试使用“#”和CDate甚至格式,没有任何作用。

我无法共享数据,因为它很敏感,但我使用的DLookup的语法如下:

    sqlStr = "LastName=" & Chr(34) & rs![Last Name] & Chr(34) 
    & " AND FirstName=" & Chr(34) & rs![First Name] & Chr(34) 
    & " AND DateLastModified=" & Format(dateVar, "dd/mm/yyyy") 

    DLookup("LastName", "table_Person", sqlStr)       
P.S:我尝试过DCount,同样的事情发生了。 DCount返回0但我知道记录在那里。

4 个答案:

答案 0 :(得分:2)

建立标准BuildCriteria是你的朋友。

Sub TestBuildCriteria()
Dim strCriteria As String 
strCriteria = BuildCriteria("OrderDate", dbDate, [Date created])
MsgBox strCriteria
End Sub

Sub YourCode()
sqlStr = BuildCriteria("LastName", dbText, "=" & rs![Last Name]) & _
   " AND " & BuildCriteria("FirstName", dbText, "=" & rs![First Name]) & _
   " AND " & BuildCriteria("DateLastModified", dbDate, "=" & dateVar)
End Sub

这与正确的格式化日期相呼应。对其他数据类型也很有用。例如。它逃脱了Quotation Marks in Strings。也请阅读Custom Filters using BuildCriteria()

但是有一个更容易的选择。

在表格中的LastName,FirstName和DateLastModified上创建唯一的composite index。现在你不能插入副本,因为它必须是唯一的。如果您尝试,您将收到错误消息。请注意事务回滚(例如,多次插入,一次因密钥违规而失败 - >如果您使用db.Execute SQL, dbFailOnError,则会因事务回滚而恢复所有操作。)

答案 1 :(得分:1)

要检查日期,请使用:

ArrayList<Person> duplicates = new ArrayList<Person>

duplicate.add(new Person())
... 
...
HashSet<Person> filter = new HashSet<duplicates>
ArrayList<Person> persons = new ArrayList<Person>(filter)

如果"DateLastModified=#" & FormatDateTime(dateVar, vbShortDate) & "#" 可以为null,则需要以下内容:

dateVar

当然,只检查日期部分。如果您的FormatDateTime(Nz(dateVar,CDate("1/1/2000")), vbShortDate) 也有时间部分,那么您必须使用

dateVar

答案 2 :(得分:0)

您的语法不正确。您应该在字段名称周围放置方括号,如MSDN

中记录的示例中所指出的那样
sqlStr = "[LastName]=" & Chr(34) & rs![Last Name] & Chr(34) 
& " AND [FirstName]=" & Chr(34) & rs![First Name] & Chr(34) 
& " AND [DateLastModified]=#" & Format(dateVar, "dd/mm/yyyy") & "#" 

DLookup("[LastName]", "table_Person", sqlStr)  

答案 3 :(得分:0)

In this situation, my advice would be to simplify the criteria part of the DLOOKUP/DCOUNT until you get something that works, and only then start to make the criteria more complex. I call this 'sanity checking'.

Date/Time criteria often cause problems, so first check that you can make it work without the Date part of the criteria.

For example, in your case, check that this works. Use the Debug Window (Ctril+G) to test this:

? DCount("*", "table_Person", "LastName=" & Chr(34) & rs![Last Name] & Chr(34))

Then try:

? DCount("*", "table_Person", "LastName=" & Chr(34) & rs![Last Name] & Chr(34) & " AND FirstName=" & Chr(34) & rs![First Name] & Chr(34))    

Once you have that working, add in the Date criteria. Building the criteria up in stages like this, allows you to confirm which part is actually causing the problem.

I'm in the UK, and so I have my dates displayed in UK format - 'DD/MM/YYYY'. However, when specifying a date criteria for a DLOOKUP/DCOUNT, I always have to format the date to US format. I've often used a simple function to swap the digits into the correct order for the criteria:

Function HashDate(dD As Date) As String
  HashDate = "#" & Format$(dD, "MM/DD/YYYY") & "#"
End Function

In the the Debug Window:

? Date
09/03/2018 

? HashDate(Date)
#03/09/2018#
相关问题