Vba DLookUp返回运行时错误424-需要对象
我是VBA新手,正在尝试获取我正在编写的宏,以检查是否已经存在与要添加的新记录的日期/班次组合相对应的预先存在的记录。为此,我尝试使用DLookup在数据库中进行搜索,只有当DLookup返回Null时才允许新记录,除非它找到一个日期和移位都与新数据匹配的记录(我使用的是使用这两个数据段的复合主键)。唉,正如标题所说,我一直在得到运行时错误424,当我调试它时,它会突出显示Vba DLookUp返回运行时错误424-需要对象,vba,ms-access,runtime-error,dlookup,Vba,Ms Access,Runtime Error,Dlookup,我是VBA新手,正在尝试获取我正在编写的宏,以检查是否已经存在与要添加的新记录的日期/班次组合相对应的预先存在的记录。为此,我尝试使用DLookup在数据库中进行搜索,只有当DLookup返回Null时才允许新记录,除非它找到一个日期和移位都与新数据匹配的记录(我使用的是使用这两个数据段的复合主键)。唉,正如标题所说,我一直在得到运行时错误424,当我调试它时,它会突出显示 lookTest = Access.DLookup("Shift", "trialTable", "[Date of Pr
lookTest = Access.DLookup("Shift", "trialTable", "[Date of Production]=#" & shiftDate & "#" & "And [Shift]='" & currentShift & "'")
作为问题线。(“Shift”和“Date of Production”是用作复合键的两列的列标题,“trialTable”是我正在使用的表,“shiftDate”是日期,“currentShift”是字符串),就我所知,我的语法是正确的,所以我不知道在这一点上哪里出了问题(这可能是一些愚蠢而明显的事情,我敢肯定)。如果有帮助,下面是导致这一点的其余代码,以防我之前把事情搞砸了,而计算机只是在它到达那一行之前才意识到。非常感谢任何帮助,谢谢
Sub DatabaseUpdate()
' exports data from the active spreadsheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Ace.OLEDB.12.0; " & "Data Source=H:\TestingDatabase.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "trialTable", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records on a table
With rs
.AddNew 'create a new record
'Ask the user which shift the report is for.
Dim currentShift As String
Dim shiftDate As Date
Dim shiftCheck As Boolean
shiftCheck = False
' Request and check if the user has entered in a valid shift. If so, continue. If not, inform them and then repeat the request for the shift identity.
Do While shiftCheck = False
currentShift = UCase(InputBox("Which shift is this entry for? Please input only A, B, or C. (not case-sensitive)"))
If currentShift = "A" Or currentShift = "B" Or currentShift = "C" Then
shiftCheck = True
Else
wrongLetterWarning = MsgBox("Sorry, that is not an accepted response (A, B, or C). Please try again.", vbOKOnly)
End If
Loop
' Request the date the shift occured on. MM/DD/YYYY format is important as the date and shift together form the database's primary key.
shiftDate = InputBox("On which date did this shift occur? Please use MM/DD/YYYY format.")
'Check to make sure that there isn't already a pre-existing record for the date/shift combination.
Dim lookTest As Variant
lookTest = Access.DLookup("Shift", "trialTable", "[Date of Production]=#" & shiftDate & "#" & "And [Shift]='" & currentShift & "'")
我在Excel中测试了DLookup,它发现该表中的数据库是否处于物理打开状态。由于您已连接到数据库,并且已经打开了一个记录集,请对记录集应用筛选器
'code to collect data
...
rs.Open "SELECT * FROM trialTable WHERE [Date of Production]=#" & shiftDate & "# And [Shift]='" & currentShift & "'", cn, adOpenKeyset, adLockOptimistic
If rs.EOF And rs.BOF Then
'code to create record
...
Else
MsgBox "Record already exists."
End If
什么是
Access
?打开选项Explicit
,看看编译它时会发生什么。@GSerg:MS Access@braX:首先它让我将变量errowlettwarning指定为变量,现在出于某种原因它将Access标记为变量(它不是一个变量)并且说它是未定义的。当然它是未定义的。在MS Access对象模型中没有Access
这样的东西。但是有应用程序
,并且它确实有DLookUp
作为它的方法。@Grahamphone是Excel项目或Access项目中包含的VBA代码吗?数据库中的代码注释eUpdate
提到了“excel电子表格”。