Solved

SQL INSERT SELECT Query

Posted on 2014-04-16
7
257 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
7 Comments
 
LVL 22

Expert Comment

by:plusone3055
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
That was exactly what I needed, Thanks Carl...
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now