访问VBA在OpenRecordset

时间:2017-04-28 17:26:20

标签: vba ms-access access-vba

Dim recvar
Dim addrvar
Dim grpvar
Dim rst As Recordset
Dim db As dao.Database
Dim findExisting

recVar = Forms!locspecificinfofrm.ActiveDataSubfrm!Record_ID.Value
grpVar = Forms!locspecificinfofrm.Location!Grp_ID.Value

findExisting = "SELECT [Addr_ID] FROM [Prov_Address_Bridge] WHERE 
[Record_ID] = " & recVar & " AND [Grp_ID] = " & grpVar & " AND [Addr_Type] = 'Remit'"

Set rst = db.OpenRecordset(findExisting)

我收到以下错误:

  

运行时错误91:对象变量或未设置块变量

最初我认为这是对待我的最后一行:

set rst = nothing

但我用来测试查询的变量应该是某些,我只是不断收到此错误。奇怪的是,我在同一表单上的其他事件中使用了相同的代码片段,并且工作正常。

2 个答案:

答案 0 :(得分:2)

您需要设置db

Exception in thread "main" java.lang.UnsupportedOperationException
    at org.codehaus.groovy.runtime.ConvertedMap.invokeCustom(ConvertedMap.java:52)
    at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:124)
    at com.sun.proxy.$Proxy0.getString(Unknown Source)
    at IStringHolder$getString.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at Example.doSomething(Example.groovy:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:46)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:206)
    at Example.main(Example.groovy:21)

答案 1 :(得分:0)

Pelase,指定引发错误的行。我假设错误与代码一致:

Set rst = db.OpenRecordset(findExisting)

正如我所看到的,在您的代码中,您没有像这样设置db的实例:

Set db = CurrentDb 

或者,例如

Set db = wrkAcc.OpenDatabase("YourDatabase")  

使用您的调试哟查看变量db是否不在Nothing中。如果是,您必须在db变量中设置dao.Database的correcto实例。