访问VBA DLOOKUP

时间:2018-08-16 10:54:21

标签: ms-access access-vba

Teller = Nz(DLookup("[Teller]", "[Lookuptable]", ("Artikel = '" & ValueArtikel & "' " And " Lookuptable= 'G'")), 0)
Noemer = Nz(DLookup("[Noemer]", "[lookuptable]", ("Artikel = ' " & ValueArtikel & " ' " And Lookuptable= " 'G' ")), 0)

我想在acces vba中执行DLOOKUP,但是我找不到正确的语句。我查看了许多站点,这是我认为是正确的两个dlookup,但都给出了错误类型不匹配的内容。 Teller和noemer是整数,Artikel和artikelvalue和Lookuptable是字符串。抱歉,如果已经问过了,但是我找不到它。我发现了很多关于它的帖子,但我无法解决。尤其是我的英语不好

2 个答案:

答案 0 :(得分:2)

第一个关闭。使用变量和Debug.Print来帮助构建字符串。

Ctrl + g显示输出。

strCrit = "Artikel = '" & ValueArtikel & "' And Lookuptable= 'G'"
Debug.Print strCrit
Teller = Nz(DLookup("[Teller]", "[Lookuptable]", strCrit), 0)

答案 1 :(得分:1)

我将自己的函数用于查找,因为查找的性能确实很差。

' Lookups Replacements
'---------------------

Function DLook(Expression As String, Domain As String, Optional Criteria) As Variant

  On Error GoTo Err_Handler

  Dim strSQL As String

  'DCount: strSQL = "SELECT COUNT(" & Expression & ") FROM " & Domain

  'Other replacements
  'DLookup: 
   strSQL = "SELECT " & Expression & " FROM " & Domain
  'DMax: strSQL = "SELECT MAX(" & Expression & ") FROM " & Domain
  'DMin: strSQL = "SELECT SUM(" & Expression & ") FROM " & Domain
  'DFirst: strSQL = "SELECT FIRST(" & Expression & ") FROM " & Domain
  'DLast: strSQL = "SELECT LAST(" & Expression & ") FROM " & Domain
  'DSum: strSQL = "SELECT SUM(" & Expression & ") FROM " & Domain
  'DAvg: strSQL = "SELECT AVG(" & Expression & ") FROM " & Domain

  If Not IsMissing(Criteria) Then strSQL = strSQL & " WHERE " & Criteria
  DLook = DBEngine(0)(0).OpenRecordset(strSQL, dbOpenForwardOnly)(0)

Exit Function

Err_Handler:
  MsgBox "Error. Lookup couldnt be performed" & vbNewLine & Err.Description, vbCritical

End Function

致电:

If DLook("Column2", "Table1", "Column1 = " & ID) = 0 Then
  'Do stuff
End If

If DLook("Column2", "Table1") = 0 Then
  'Do other stuff
End If