根据 MS-Access 中其他表的查询创建包含值的新记录

发布于 2024-10-15 02:38:01 字数 2000 浏览 4 评论 0原文

我想在 mailers 表中创建一条新记录,其中三个字段将包含值:contacts_first_filter_id、mailer_states_id、created_at。其中两个值基于其他表的查询,最后一个值只是当前时间。

我尝试了多种方法来实现这一目标,但没有一个有效。首先,我尝试直接在 sql 设计中创建查询:

INSERT INTO mailers ( contacts_first_filter_id, mailer_states_id, created_at )
VALUES (DLookup("id","update_mailer_step_two"), DLookup("id","mailer_states" & "mailer_state = 'sent'"), Now());

这给出了错误“未知”。

我还尝试将其放入 VBA 编辑器中并在单击按钮时触发它:

Private Sub Command6_Click()


        Dim CFF_ID As String, MS_ID As String, strSQL As String

        CFF_ID = "SELECT update_mailer_step_two.id FROM update_mailer_step_two"

        MS_ID = "SELECT mailer_states.id FROM mailer_states WHERE mailer_states.mailer_state = 'sent'"

        strSQL = "INSERT INTO mailers ( contacts_first_filter_id, mailer_states_id, created_at )VALUES ((" & CFF_ID & "),(" & MS_ID & "),NOW())"

        DoCmd.RunSQL strSQL

End Sub

这会出现错误: “查询输入必须至少包含一个表或查询”。

我使用 INNER JOIN 尝试了下面的建议,但是虽然这不会产生错误,但它会附加 0 行,大概是因为 update_mailer_step_two 和 mailer_states 之间没有 INNER JOIN 链接:

INSERT INTO mailers ( contacts_first_filter_id, mailer_states_id, created_at )
SELECT update_mailer_step_two.id, mailer_states.id, Now()
FROM update_mailer_step_two INNER JOIN mailer_states ON update_mailer_step_two.id = mailer_states.ID
WHERE mailer_states.mailer_state = 'sent';

mailer_states 表只是一个包含带有文本的 mailer_state 字段的表。它几乎只是一个永远不会改变的查找表。

update_mailer_step_2 包含一个名为 id 的字段,其中包含一堆与数据库中的联系人关联的 id。 因此,mailer_states 和 update_mailer_step_2 之间根本没有关系。

** 我认为我能够做的是,当我创建 update_mailer_step_two 时,添加一个新列并默认使用与 mailer_states 对应的值,以便 INNER JOIN 可以工作。不幸的是,您似乎无法在 Access 中执行此操作!

另一篇 stackoverflow 帖子提到您可以:

SQL 添加具有默认值的列- Access 2003

但它对我不起作用。

我已经没有关于如何将基于其他表的查询作为旨在在表中创建新记录的查询的值的想法了。

感谢您的回复。

I want to create a new record in the mailers table, where three fields would contain values: contacts_first_filter_id, mailer_states_id, created_at. And two of those values are based off queries from other tables and the last one is just the current time.

I've tried multiple ways to achieve this but none have worked. First I tried creating a query directly within sql design:

INSERT INTO mailers ( contacts_first_filter_id, mailer_states_id, created_at )
VALUES (DLookup("id","update_mailer_step_two"), DLookup("id","mailer_states" & "mailer_state = 'sent'"), Now());

This gives error 'unknown'.

I also tried putting this in VBA editor and triggering it on a button click:

Private Sub Command6_Click()


        Dim CFF_ID As String, MS_ID As String, strSQL As String

        CFF_ID = "SELECT update_mailer_step_two.id FROM update_mailer_step_two"

        MS_ID = "SELECT mailer_states.id FROM mailer_states WHERE mailer_states.mailer_state = 'sent'"

        strSQL = "INSERT INTO mailers ( contacts_first_filter_id, mailer_states_id, created_at )VALUES ((" & CFF_ID & "),(" & MS_ID & "),NOW())"

        DoCmd.RunSQL strSQL

End Sub

This gives error:
"Query input must contain at least one table or query".

I tried the recommendation below using INNER JOIN but while this produces no error, it appends 0 rows, presumably because there is no INNER JOIN link between update_mailer_step_two and mailer_states:

INSERT INTO mailers ( contacts_first_filter_id, mailer_states_id, created_at )
SELECT update_mailer_step_two.id, mailer_states.id, Now()
FROM update_mailer_step_two INNER JOIN mailer_states ON update_mailer_step_two.id = mailer_states.ID
WHERE mailer_states.mailer_state = 'sent';

mailer_states table is just a table that contains a mailer_state field with text. It's pretty much just a lookup table that never changes.

update_mailer_step_2 contains one field called id, which contains a bunch of ids associated with contacts in database.
Hence, there's no relationship at all between mailer_states and update_mailer_step_2.

** What I thought I would be able to do is when I create update_mailer_step_two, add a new column and default it with a value corresponding to mailer_states so that the INNER JOIN would work. Unfortunately, it appears you cannot do this in Access!

Another stackoverflow post mentioned you can:

SQL to add column with default value - Access 2003

But it is not working for me.

I have run out of ideas of how to place a query based off other table as a value to a query that is intended to create a new record in a table.

Thanks for response.

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

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

发布评论

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

评论(4

青巷忧颜 2024-10-22 02:38:02

您正在尝试将 select 语句的结果插入到表中。
但是,select 语句独立地从两个不同的表中进行选择。

我认为这行不通。

主要是它如何处理这两个独立的结果。它是否创建一个包含交叉、内连接等的列表。

我将创建一个从两者中进行选择的查询,然后将该查询插入到您的表中。或者您可以内部连接选择并将连接转发到您的值中。

You're trying to insert the results of a select statement into a table.
However, the select statement independently selects from two different tables.

I don't think this will work.

Mainly because how does it handle the two independent results. Does it create a list with the cross, inner join, etc.

I would create a query that selects from both, and then insert that query into your table. Or you could inner join the selects and forward that join into your values.

围归者 2024-10-22 02:38:02

我认为您的按钮单击示例在构建字符串时也存在语法错误。这应该工作(与代码的其余部分一起,假设您的 DLoookups 正常工作,如 HansUp 所指出的):

strSql = "INSERT INTO mailers (contacts_first_filter_id,  mailer_states_id," & _
       " created_at) VALUES (" & CFF_ID & ", " & MS_ID & ", #" & Now() & "#)"

我不确定我删除的额外括号是否会产生任何影响,空格差异应该没有区别,但我的版本让 VBA 计算 Now() 并将值构建到字符串中。 Access 使用 # 来引用日期。

I think your button click sample also has syntax errors when you build the string. This should work (with the rest of the code, assuming that your DLoookups work correctly, as noted by HansUp):

strSql = "INSERT INTO mailers (contacts_first_filter_id,  mailer_states_id," & _
       " created_at) VALUES (" & CFF_ID & ", " & MS_ID & ", #" & Now() & "#)"

I'm not sure whether or not the extra parens that I removed would have any effect, and the whitew space differencwe should be no difference, but my version lets VBA evaluate Now() and build the value into the string. Access uses # to quote dates.

一人独醉 2024-10-22 02:38:01

您的第一种方法(使用包含对 DLookup 的调用的查询)原则上应该有效,但它有一个语法错误。 @HansUp 单独运行每个 DLookup 的建议是一个很好的建议,并且可以帮助您识别这一点(在 Visual Basic 编辑器中,按 Ctrl-G 显示中间窗口;@HansUp 示例中每个表达式之前的问号 < em>是必需的)。

具体来说,第二个 DLookup 调用中的 & 是字符串连接字符,这意味着您当前的调用相当于:

DLookup("id","mailer_statesmailer_state = 'sent'")

相反,请将其更改为:

DLookup("id","mailer_states", "mailer_state = 'sent'")

EXTRA

要为 update_mailer_step_two 中的每一行插入一个新行,请尝试以下操作:

INSERT INTO mailers ( contacts_first_filter_id, mailer_states_id, created_at )
SELECT update_mailer_step_two.id,
       DLookup("id","mailer_states", "mailer_state = 'sent'"),
       Now()
FROM update_mailer_step_two

Your first approach (using a query containing calls to DLookup) should in principle work, but it has a syntax error. The suggestion by @HansUp to run each DLookup individually is a good one, and would help you to identify this (in the Visual Basic Editor, press Ctrl-G to show the intermediate window; the question mark before each expression in @HansUp's example is required).

Specifically, the ampersand in the second DLookup call is the string concatenation character, meaning that your current call is equivalent to:

DLookup("id","mailer_statesmailer_state = 'sent'")

Instead, change it to:

DLookup("id","mailer_states", "mailer_state = 'sent'")

EXTRA

To insert one new row for each row in update_mailer_step_two, try this:

INSERT INTO mailers ( contacts_first_filter_id, mailer_states_id, created_at )
SELECT update_mailer_step_two.id,
       DLookup("id","mailer_states", "mailer_state = 'sent'"),
       Now()
FROM update_mailer_step_two
英雄似剑 2024-10-22 02:38:01

在立即窗口 (Ctrl+g) 中尝试 2 个 DLookup 语句。

? DLookup("id","update_mailer_step_two")
? DLookup("id","mailer_states", "mailer_state = 'sent'")

如果它们工作(不要抛出错误)并返回您想要的内容,请尝试将它们合并到您的子例程中。

Private Sub Command6_Click()
    Dim CFF_ID As Long, MS_ID As Long, strSQL As String
    Dim db As DAO.Database
    CFF_ID = DLookup("id","update_mailer_step_two")
    MS_ID = DLookup("id","mailer_states", "mailer_state = 'sent'")
    strSQL = "INSERT INTO mailers (contacts_first_filter_id, mailer_states_id, created_at) " & _
        "VALUES (" & CFF_ID & ", " & MS_ID & ", NOW())"
    'DoCmd.RunSQL strSQL '
    Debug.Print strSQL 'look in Immediate Window to see the completed INSERT statement '
    Set db = CurrentDb
    db.Execute strSQL, dbFailOnError
    Set db = Nothing
End Sub

该未经测试的代码假定邮件程序中的 CFF_ID、MS_ID、contacts_first_filter_id 和 mailer_states_id 字段以及 update_mailer_step_two 和 mailer_states 表中的 id 字段采用数字数据类型。如果其中任何一个实际上是文本,您就必须对代码进行调整或告诉我们哪个是哪个。

编辑: Debug.Print 行的目的是允许您查看要求数据库执行的完整 INSERT 语句。如果失败,您可以复制语句文本并将其粘贴到新查询的 SQL 视图中。您可以在此处对其进行调整,以便 Access 接受它。然后调整您的代码以生成相同的工作 INSERT 语句。

Try your 2 DLookup statements in the Immediate Window (Ctrl+g).

? DLookup("id","update_mailer_step_two")
? DLookup("id","mailer_states", "mailer_state = 'sent'")

If they work (don't throw errors) and return what you want, try incorporating them into your subroutine.

Private Sub Command6_Click()
    Dim CFF_ID As Long, MS_ID As Long, strSQL As String
    Dim db As DAO.Database
    CFF_ID = DLookup("id","update_mailer_step_two")
    MS_ID = DLookup("id","mailer_states", "mailer_state = 'sent'")
    strSQL = "INSERT INTO mailers (contacts_first_filter_id, mailer_states_id, created_at) " & _
        "VALUES (" & CFF_ID & ", " & MS_ID & ", NOW())"
    'DoCmd.RunSQL strSQL '
    Debug.Print strSQL 'look in Immediate Window to see the completed INSERT statement '
    Set db = CurrentDb
    db.Execute strSQL, dbFailOnError
    Set db = Nothing
End Sub

That untested code assumes numeric data types for CFF_ID, MS_ID, the contacts_first_filter_id and mailer_states_id fields in mailers, and the id fields in the update_mailer_step_two and mailer_states tables. If any of those are actually text, you'll have to make adjustments to the code or tell us which is which.

Edit: The intent of the Debug.Print line is to allow you to view the completed INSERT statement you're asking the db to execute. If it fails, you can copy the statement text and paste it into SQL View of a new query. There you can tweak it so that Access will accept it. Then adjust your code to produce the same working INSERT statement.

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