如何使用数据库命令“Seek”?和结果“NoMatch”?

发布于 2024-12-17 04:15:37 字数 622 浏览 2 评论 0原文

这是我的问题:

假设我有这些表:

table1

1 - "a"

2 - "b"

table2

1 -

2 -

3 -

现在,我使用以下代码来比较表:

table2.MoveFirst
Do While Not table2.EOF
 table1.Seek "=", table2!field2
  If table1.NoMatch Then
               go do a lot of things to find that information
  Else 
   table2.Edit
   table2!Field2 = table1!field2
   table2.update
  End If
 table2.MoveNext
Loop

但该行

table2!Field2 = table1!field2

不起作用,所以出色地。我很确定我在这里做错了什么,但我在寻找解决方案时遇到问题。我什至不确定我应该谷歌搜索什么...

编辑:字段 2 在表 1 中建立索引,因此“搜索”有效。

Here's my problem:

Let's say I have these tables:

table1

1 - "a"

2 - "b"

table2

1 -

2 -

3 -

Now, I'm using the following code to compare the tables:

table2.MoveFirst
Do While Not table2.EOF
 table1.Seek "=", table2!field2
  If table1.NoMatch Then
               go do a lot of things to find that information
  Else 
   table2.Edit
   table2!Field2 = table1!field2
   table2.update
  End If
 table2.MoveNext
Loop

But the line

table2!Field2 = table1!field2

Is not working so well. I'm pretty sure I'm doing something wrong here, but I'm having problems finding a solution. I'm not even sure what I should google...

EDIT: Field 2 is indexed in table 1, so the 'seek' works.

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

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

发布评论

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

评论(3

反差帅 2024-12-24 04:15:37

一些笔记。

假设您想要 Table2 中名为 Field1 的字段不匹配的所有记录:

sSQL = "SELECT Field1, FieldX FROM Table2 " _
     & "LEFT JOIN Table1 " _
     & "ON Table2.Field1 = Table1.Field1 " _
     & "WHERE Table1.Field1 Is Null"

当然,您可以在查询设计窗口中构建查询并进行调整,直到正是您想要的,然后切换到 SQL查看以获取正确的(ish)SQL 字符串。

Dim rs As DAO.Recordset

Set rs = CurrentDB.Openrecordset(sSQL)

''table2.MoveFirst
Do While Not rs.EOF ''table2.EOF

''You do not need no match, all these records are missing a match
''    table1.Seek "=", table2!field2
''    If table1.NoMatch Then
           go do a lot of things to find that information
    rs.MoveNext
Loop
''This can all be done with one update query
''    Else 
''    table2.Edit
''    table2!Field2 = table1!field2
''    table2.update
''    End If
''    table2.MoveNext
''    Loop

sSQL = "UPDATE Table2 " _
     & "INNER JOIN Table1 " _
     & "SET table2.Field2 = table1.field2 " 

CurrentDB.Execute sSQL dbFailOnerror

请将以上内容视为注释,而不是完成的代码。

A few notes.

Let us say you want all the records from Table2 where there is no match on a field called Field1:

sSQL = "SELECT Field1, FieldX FROM Table2 " _
     & "LEFT JOIN Table1 " _
     & "ON Table2.Field1 = Table1.Field1 " _
     & "WHERE Table1.Field1 Is Null"

You could, of course, build the query in the query design window and fiddle around until is is just what you want, then switch to SQL view to get the right (ish) SQL string.

Dim rs As DAO.Recordset

Set rs = CurrentDB.Openrecordset(sSQL)

''table2.MoveFirst
Do While Not rs.EOF ''table2.EOF

''You do not need no match, all these records are missing a match
''    table1.Seek "=", table2!field2
''    If table1.NoMatch Then
           go do a lot of things to find that information
    rs.MoveNext
Loop
''This can all be done with one update query
''    Else 
''    table2.Edit
''    table2!Field2 = table1!field2
''    table2.update
''    End If
''    table2.MoveNext
''    Loop

sSQL = "UPDATE Table2 " _
     & "INNER JOIN Table1 " _
     & "SET table2.Field2 = table1.field2 " 

CurrentDB.Execute sSQL dbFailOnerror

Please treat the above as notes, not finished code.

旧伤还要旧人安 2024-12-24 04:15:37

但是 table2!Field2 = table1!field2 行的效果不太好

...这并不能很好地描述问题所在。

代码是否在这一行停止/崩溃?
它运行时是否没有错误,但没有执行任何超出您预期的操作/其他操作?

我想您正在使用 DAO 记录集。
如果没有更多信息,很难给出建议,但我会尝试一下:

  1. Is table2完全是空的吗?您的描述如下所示:

    <代码>表2
    1 -
    2 -
    3 -

    如果是,则整个循环可能根本不会执行。

  2. 记录集table2可以更新吗?
    并非所有类型的记录集都支持这一点,这取决于您如何创建它。参见MSDN:Recordset Object (DAO),里面有一个Recordset的列表在开头输入类型。
    如果它不可更新,则在调用 .Update 时应该会收到错误消息。

But the line table2!Field2 = table1!field2 Is not working so well

...is not a good description of what goes wrong.

Does the code stop/crash at this line?
Does it run without errors, but do nothing / something else than you expected?

I suppose that you're using DAO Recordsets.
It's hard to give advice without more information, but I'll give it a try:

  1. Is table2 completely empty? Your description looks like this:

    table2
    1 -
    2 -
    3 -

    If yes, the whole loop is probably never executed at all.

  2. Can the Recordset table2 be updated?
    Not all types of Recordsets support this, it depends on how you create it. See MSDN: Recordset Object (DAO), there is a list of Recordset types at the beginning.
    If it's not updateable, you should get an error when you call .Update.

迷你仙 2024-12-24 04:15:37

如果您使用 DAO 记录集(按照 Christian 的建议),您可以将该行更改

table2!Field2 = table1!field2

table2.Fields("Field2").value = table1.Fields("field2").value

“我假设两个 Field2 都是文本数据类型”。

If you're using DAO recordsets (as suggested by Christian) you can change the line

table2!Field2 = table1!field2

to

table2.Fields("Field2").value = table1.Fields("field2").value

I'm presuming both Field2's are of Text data type.

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