Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL INSERT SELECT Query

Posted on 2014-04-16
7
Medium Priority
?
275 Views
Last Modified: 2014-04-16
Experts, I am trying to create an insert query. I need to insert some data from a user form and then need to get some data from two other tables. I cannot figure this out. Is what I am trying to do possible?

Please Help...
        strSql = "INSERT INTO ProjectProfile (ProjectName,QuoteNumber,LogDate,Freight,FOB,LineItems,GroupTotal,TotalList,IncludeService,Discount," & _
                 "QuoteLife,Onsite,OffSite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software) " & _
                 "VALUES (" & _
                 "'" & txtProjectName.Text & "','" & txtQuoteNumber.Text & "',#" & Today & "#),(" & _
                 "SELECT Freight,FOB,Lineitems,GroupTotal,TotalList,IncludeService,Discount,QuoteLife FROM QuoteDefaults),(" & _
                 "SELECT Onsite,Offsite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software FROM ServiceRates)"

Open in new window

0
Comment
Question by:Basicfarmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40004173
your question is a little unclear...
 are you asking for how to write the following query in VB.NET  Syntax ?
0
 
LVL 5

Expert Comment

by:jayakrishnabh
ID: 40004181
write a single select statement where you can pass first 3 values from the form values.
 
something like this..
Insert into ProjectProfile(columns...)
select txtProjectName.Text, txtQuoteNumber.Text, other columns...
FROM table
0
 

Author Comment

by:Basicfarmer
ID: 40004198
Plusone3055, I was hoping that my attempt to create this query would show what I wanted to do. This query does not work.  How should this be written so it works? I dont need any vb specifc syntax im just creating the string.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 40004210
It should be possible (depending on the database you are using).

Try something like:
strSql = "INSERT INTO ProjectProfile 
(ProjectName,QuoteNumber,LogDate,Freight,FOB,LineItems,GroupTotal,TotalList,IncludeService,Discount,QuoteLife,Onsite,OffSite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software) 
SELECT '" & txtProjectName.Text & "', '" & txtQuoteNumber.Text & "', #" & Today & "#, t1.Freight,t1.FOB,t1.Lineitems,t1.GroupTotal,t1.TotalList,t1.IncludeService,t1.Discount,t1.QuoteLife,
	t2.Onsite,t2.Offsite,t2.Training,t2.Overtime,t2.Saturday,t2.SaturdayOvertime,t2.Sunday,t2.Hold,t2.Travel,t2.WeekendTravel,t2.Mileage,t2.Software
				 FROM QuoteDefaults t1, ServiceRates t2"

Open in new window

0
 

Author Comment

by:Basicfarmer
ID: 40004214
Jayakrishnabh, this gave me an error at "Select Freight".
        'strSql = "INSERT INTO ProjectProfile (ProjectName,QuoteNumber,LogDate,Freight,FOB,LineItems,GroupTotal,TotalList,IncludeService,Discount," & _
        '         "QuoteLife,Onsite,OffSite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software) " & _
        '         "SELECT " & _
        '         "'" & txtProjectName.Text & "','" & txtQuoteNumber.Text & "',#" & Today & "#," & _
        '         "SELECT Freight,FOB,Lineitems,GroupTotal,TotalList,IncludeService,Discount,QuoteLife FROM QuoteDefaults," & _
        '         "SELECT Onsite,Offsite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software FROM ServiceRates"

Open in new window

0
 

Author Closing Comment

by:Basicfarmer
ID: 40004225
That was exactly what I needed, Thanks Carl...
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 40004226
>>I need to insert some data from a user form and then need to get some data from two other tables.

Is the data being inserted ONLY from the user form?
The get some data - is that after the INSERT have completed?

If the answer to both of the above questions is true then you need to perform the action in two (or more) distinct steps.  First run the INSERT SQL command then the SELECT command(s) to return the data.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question