问题
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.
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.
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... 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 & "';"
So how do I put my constant User Name inside the query). Would it be something like SELECT... AND VALUES...?
Regards Max
回答1:
You can just use an INSERT INTO .... SELECT ....
statement, with one column and one constant value.
Example:
sql = "INSERT INTO tblContact(txtNameStart, txtNameVC) " & _
"SELECT """ & Me.cboName & """, txtName FROM tblVC WHERE Branche ='" & Me.cboBranchen & "';"
来源:https://stackoverflow.com/questions/46219485/insert-data-from-sql-query-with-one-constant-value-in-ms-access-with-sql