使用SQL在MS Access中使用一个常量值从sql查询插入数据 [英] INSERT data FROM sql query with one constant value in MS Access with SQL

查看:244
本文介绍了使用SQL在MS Access中使用一个常量值从sql查询插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为MS Access创建一个表单,在该表单中,您首先会根据条件在表中搜索公司.返回的数据只是匹配公司的名称.然后,我想取这些名称并将它们添加到另一个表中.

I am trying to create a form for MS Access where you at first search for companies in a table based on criteria. The returned data is simply the names of the matching companies. I then want to take these names and add them to a different table.

到目前为止,共有3张表:一张存储用户名(tblStartup),一张存储公司名(tblVC),一张用于保存匹配项(tblContact).

So far there are 3 tables: one stores the User Names (tblStartup), one Stores the Company Names (tblVC) and one shall be used to save the matches (tblContact).

我遇到的问题是我想在查询数据旁边添加一个恒定的用户名.

The problem I have is that I want to add a constant user name alongside the data from the query.

例如"MAX"搜索来自汽车行业的公司.他获得了匹配的公司列表,例如宝马,戴姆勒和奥迪. 因此应添加到表tblContact的数据将是:

So e.g. "MAX" searches for companies that are from Automotive. He gets a list of matching companies e.g BMW, DAIMLER and AUDI. So the data which should be added to the table tblContact would be:

MAX           BMW
MAX           DAIMLER
MAX           AUDI  

INSERT INTO ... SELECT ...不起作用,因为我不仅需要从查询中添加信息,还需要用户从组合框选择的常量(例如,用户选择"MAX",然后使用单击查询"按钮以找到MAX的匹配公司,然后单击添加到联系人"按钮.

INSERT INTO...SELECT... doesn´t work because I not only need to add the Info from the query but also a constant which the User selects from a Combobox(e.g. the user selects "MAX" and then uses the button "query" to find matching companies for MAX, then the button "add to Contacts".

这是我到目前为止的代码:

This is the code I have so far:

Dim sql As String
sql = "INSERT INTO tblContact(txtNameStart, txtNameVC) " & _
      "SELECT txtName FROM tblVC WHERE Branche ='" & Me.cboBranchen & "';"

因此,如何将我的常量用户名放在查询中).会像SELECT ... AND VALUES ...这样吗?

So how do I put my constant User Name inside the query). Would it be something like SELECT... AND VALUES...?

问候 最高

推荐答案

您可以仅使用INSERT INTO .... SELECT ....语句,该语句具有一列和一个常量值.

You can just use an INSERT INTO .... SELECT .... statement, with one column and one constant value.

示例:

sql = "INSERT INTO tblContact(txtNameStart, txtNameVC) " & _
      "SELECT """ & Me.cboName & """, txtName FROM tblVC WHERE Branche ='" & Me.cboBranchen & "';"

这篇关于使用SQL在MS Access中使用一个常量值从sql查询插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆