SQL Help!

ExcelKid_10

Board Regular
Joined
Mar 17, 2004
Messages
87
Hi all-

I need some expert advice on what I am doing wrong on my SQL/VBA code below....

Sub FundName()
Dim db As Database
Dim strSQL As String
Dim rs As Recordset
Dim FName As Variant

strSQL = "INSERT INTO tblTopExposures ( [Date], Port, FundOrAcct, Cusip, Description, [%MV] )" & _
"SELECT TOP 10 tblHoldings.Date, tblHoldings.Port, tblHoldings.FundOrAcct, tblHoldings.Cusip, tblHoldings.Description, [qry%MV].[%MV]" & _
"FROM [qry%MV] INNER JOIN tblHoldings ON ([qry%MV].Cusip = tblHoldings.Cusip) AND ([qry%MV].FundOrAcct = tblHoldings.FundOrAcct) AND ([qry%MV].Port = tblHoldings.Port) AND ([qry%MV].Date = tblHoldings.Date)" & _
"Where Port = FName;"

Set rs = CurrentDb.OpenRecordset(strSQL)

Do Until rs.EOF
FName = rs
DoCmd.RunSQL strSQL
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set db = Nothing
Debug.Print strSQL
End Sub

It keeps bombing out on me when the code hits this:

Set rs = CurrentDb.OpenRecordset(strSQL)

The end product of my SQL is simply to append the data using a loop that passes a Port name through as the criteria.

Any help is much appreciated!

Thanks!

EK
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi

Have you tried running the query without using VBA? If you can get that to work using a query, then copy the SQL into your VBA routine. I haven't looked at your code in detail but check you have spaces between various words where you are using the ampersand (&) symbol to join strings together - it looks like you are missing a few.

Andrew
 
Upvote 0
Thanks Andrew!

I've made the changes you recommended but still cannot get my SQL to work. Here's the simple breakdown of my data....

[Date]-Date
Port-Text
FundOrAcct-Text
Cusip-Text
Description-Text
[%MV]-Number

In my tblFundsOrAccts I have a unique list of Port names. What I would like to do is go through each one and use it as the criteria for the INSERT query.

Any more thoughts on how I could make this work?

Thanks!!

EK
 
Upvote 0
OK. I've been playing with it and I am almost there....what I have is this....

Sub FundName()
Dim db As Database
Dim strSQL As String
Dim rs As Recordset
Dim FName As Variant

strSQL = "INSERT INTO tblTopExposures ( [Date], Port, FundOrAcct, Cusip, Description, [%MV] ) SELECT TOP 10 tblHoldings.Date, tblHoldings.Port, tblHoldings.FundOrAcct, tblHoldings.Cusip, tblHoldings.Description, [qry%MV].[%MV]FROM [qry%MV] INNER JOIN tblHoldings ON ([qry%MV].Cusip = tblHoldings.Cusip) AND ([qry%MV].FundOrAcct = tblHoldings.FundOrAcct) AND ([qry%MV].Port = tblHoldings.Port) AND ([qry%MV].Date = tblHoldings.Date)WHERE tblHoldings.Port='FName';"

Set rs = CurrentDb.OpenRecordset("SELECT Port FROM tblFundsOrAccts")


With rs
Do Until rs.EOF
FName = rs!Port
DoCmd.RunSQL strSQL
rs.MoveNext
Loop
End With

rs.Close
Set rs = Nothing
Set db = Nothing
Debug.Print strSQL
End Sub


The query works. And it looks as if the FName is being passed through as a variable in the query. However, it is not appending any data (and there is data there). Any thoughts??

Thanks!

EK
 
Upvote 0
How about this


strSQL = "INSERT "
strSQL = strSQL & " INTO tblTopExposures ( [Date], Port, FundOrAcct, Cusip, Description, [%MV] ) "

strSQL = strSQL & " SELECT TOP 10 tblHoldings.Date, tblHoldings.Port, tblHoldings.FundOrAcct, tblHoldings.Cusip, tblHoldings.Description, [qry%MV].[%MV] "

strSQL = strSQL & " FROM [qry%MV] "

strSQL = strSQL & " INNER JOIN tblHoldings ON ([qry%MV].Cusip = tblHoldings.Cusip) "

strSQL = strSQL & " AND ([qry%MV].FundOrAcct = tblHoldings.FundOrAcct) "

strSQL = strSQL & " AND ([qry%MV].Port = tblHoldings.Port) "

strSQL = strSQL & " AND ([qry%MV].Date = tblHoldings.Date)WHERE tblHoldings.Port='FName';"

<<

But are you sure that you can do an INSERT via that conn connection?
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,080
Members
448,943
Latest member
sharmarick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top