VBA 代码在 Access 2007 中工作,但在 Access 2010 中不起作用。有什么想法吗?

发布于 2024-11-18 18:10:44 字数 1223 浏览 3 评论 0原文

我的妻子编写了以下代码,当她的组织使用 Access 2007 时,它对她来说工作得很好。他们刚刚更新到 Access 2010,它就不再工作了。我对 Access 一点也不熟悉,但我建议我将其呈现给 Stack,看看你们是否可以直接看到在 Access 2010 中无法使用的任何内容。提前感谢您提供任何见解。

Private Sub Originating_Zone_AfterUpdate()

Dim EscortDB As DAO.Database

Dim rstBldgs As DAO.Recordset

Set EscortDB = CurrentDb()
Set rstBldgs = EscortDB.OpenRecordset("SELECT BuildingName FROM" & _
" ZoneBldgLookup WHERE ZoneLocation = '" & _
Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
"' ORDER BY BuildingName", [dbOpenDynaset])


rstBldgs.MoveLast

rstBldgs.MoveFirst

Do Until rstBldgs.EOF
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].AddItem rstBldgs!BuildingName
rstBldgs.MoveNext
Loop

rstBldgs.Close


End Sub

更新:她使用以下代码使其正常工作。感谢您的帮助!

Private Sub Originating_Zone_AfterUpdate()

Dim sBuildList As String

sBuildList = ("SELECT BuildingName FROM" & _
" ZoneBldgLookup WHERE ZoneLocation = '" & _
Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
"' ORDER BY BuildingName")

Forms!DateID!EscortIDSubform.Form.[Pick Up Location].RowSource = sBuildList
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Requery

End Sub

My wife wrote the following code and it used to work fine for her when her organization used Access 2007. They just updated to Access 2010 and it no longer works. I am not familiar with Access at all but I suggested I'd present it to Stack to see if you guys can see anything straight off that won't work in Access 2010. Thanks in advance for any insights.

Private Sub Originating_Zone_AfterUpdate()

Dim EscortDB As DAO.Database

Dim rstBldgs As DAO.Recordset

Set EscortDB = CurrentDb()
Set rstBldgs = EscortDB.OpenRecordset("SELECT BuildingName FROM" & _
" ZoneBldgLookup WHERE ZoneLocation = '" & _
Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
"' ORDER BY BuildingName", [dbOpenDynaset])


rstBldgs.MoveLast

rstBldgs.MoveFirst

Do Until rstBldgs.EOF
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].AddItem rstBldgs!BuildingName
rstBldgs.MoveNext
Loop

rstBldgs.Close


End Sub

Update: She got it working using the following code. Thanks for your help!

Private Sub Originating_Zone_AfterUpdate()

Dim sBuildList As String

sBuildList = ("SELECT BuildingName FROM" & _
" ZoneBldgLookup WHERE ZoneLocation = '" & _
Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
"' ORDER BY BuildingName")

Forms!DateID!EscortIDSubform.Form.[Pick Up Location].RowSource = sBuildList
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Requery

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呢古 2024-11-25 18:10:44

这是可怕的代码。通过遍历记录集和 .AddItem 来填充下拉列表或列表框的效率非常低。只需将 SQL 字符串分配给组合框/列表框的 Rowsource 属性,无需代码即可完成整个操作。

现在,显然,列表根据此 AfterUpdate 事件所附加的控件中的选择而变化,但这意味着您在此事件中分配 Rowsource。也许,上面的所有代码都可以用这个替换:

  Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Rowsource = "SELECT BuildingName FROM" & _
     " ZoneBldgLookup WHERE ZoneLocation = '" & _
     Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
     "' ORDER BY BuildingName"

我不能说代码不工作有什么问题(我怀疑存在沙箱模式/宏安全问题),但它的代码行数比需要的要多。

It's terrible code. Populating a dropdown list or listbox by walking a recordset and .AddItem is terribly inefficient. The whole thing can be done without code by simply assigning a SQL string to the Rowsource property of the combobox/listbox.

Now, clearly, the list changes based on the choices in the control to which this AfterUpdate event is attached, but all that means is that you assign the Rowsource in this event. Probably, all the above code can be replace with this:

  Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Rowsource = "SELECT BuildingName FROM" & _
     " ZoneBldgLookup WHERE ZoneLocation = '" & _
     Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
     "' ORDER BY BuildingName"

I can't say what's wrong with the code not working (I suspect there's a sandbox mode/macro security issue going on), but it's way more lines of code than are needed.

空城缀染半城烟沙 2024-11-25 18:10:44

除了@David-W-Fenton 的建议之外,我认为您应该使用字符串变量来保存 SELECT 语句。然后您可以将其调试。打印到立即窗口,将其复制到新查询(在 SQL 视图中),并确保它实际返回行。

Dim strSql As String
strSql = "SELECT BuildingName FROM" & _
    " ZoneBldgLookup WHERE ZoneLocation = '" & _
    Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
    "' ORDER BY BuildingName"
Debug.Print strSql
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Rowsource = strSql

另外,如果这是名为 DateID 的表单模块中的代码,您可以将 Forms!DateID 替换为关键字 Me (这是“此表单”的简写...包含您正在运行的代码的表单)。这并没有显着缩短,但如果表单被重命名,则不需要更改 Me。仍然没什么大不了的……只是少了一个细节,你以后就不用再去摆弄了。

In addition to @David-W-Fenton's suggestions, I think you should use a string variable to hold the SELECT statement. Then you can Debug.Print it to the Immediate Window, copy it to a new query (in SQL View), and make sure it actually returns rows.

Dim strSql As String
strSql = "SELECT BuildingName FROM" & _
    " ZoneBldgLookup WHERE ZoneLocation = '" & _
    Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
    "' ORDER BY BuildingName"
Debug.Print strSql
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Rowsource = strSql

Also if this is code in the module of a form named DateID, you can replace Forms!DateID with the keyword Me (which is shorthand for "this form" ... the form which contains the code you're running). That's not dramatically shorter, but Me will not need to be changed if the form is ever re-named. Still not a big deal ... just one less detail you won't have to fiddle with down the road.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文