VB to Move Named Ranges to Access Database

hackley01

New Member
Joined
Nov 13, 2013
Messages
2
I'm writing my first "advanced" VBA code to move Named Ranges from Excel to Access. The Named Ranges are named the same as the Access Database field names.

When I run my code, I receive the following error: "Object required". When I click "Debug", the debugger highlights DoCmd.RunSQL strSql.

The VALUES are the names of the Named Ranges. I have a feeling that's the problem, but I can't find the proper syntax to reference the Named Ranges in VBA. I really appreciate any help you can provide.

Code:
Sub toSpreadsDB()

Set adoCn = New ADODB.Connection

adoCn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=I:\REI\Loan Origination\_Loan Origination Tools\Calculators\Spread Database.accdb"

Dim strSql As String
strSql = "INSERT INTO SpreadData([CurrentDate], [DealName], [PropertyType], [MSA], [LoanAmount], [LoanTerm], [Amortization], [IOPeriod], [MarketLTV], [AverageLife], [MosForward], [GrossSpread], [ThreeYrTsy], [FiveYrTsy], [SevenYrTsy], [TenYrTsy], [ThirtyYrTsy], [InterpTsy], [USTRiskAdjSpread], [BPIRiskAdjSpread], [VarBPISprUSTSpr], [WgtAvgBenchSpread], [NetSpreadOverBmark]) " _
& " VALUES('" & CurrentDate & " ', '" & DealName & "', '" & PropertyType & "', '" & MSA & "', '" & LoanAmount & "', '" & LoanTerm & "', '" & Amortization & "', '" & IOPeriod & "', '" & MarketLTV & "', '" & AverageLife & "', '" & MosForward & "', '" & GrossSpread & "', '" & ThreeYrTsy & "', '" & FiveYrTsy & "', '" & SevenYrTsy & "', '" & TenYrTsy & "', '" & ThirtyYrTsy & "', '" & InterpTsy & "', '" & USTRiskAdjSpread & "', '" & BPIRiskAdjSpread & "', '" & VarBPISprUSTSpr & "', '" & WgtAvgBenchSpread & "', '" & NetSpreadOverBmark & "');"
DoCmd.RunSQL strSql

adoCn.Close
Set adoCn = Nothing

MsgBox ("New spread information added.")

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I managed to get everything working. I defined a variable for each Named Range and was able to get data moving into the Access database. Unfortunately, only the last field moved. Everything else came in as 0 or NULL.

Here is the INSERT INTO statement:

Code:
sSQL = "INSERT INTO SpreadData([CurrentDate], [DealName], [PropertyType], [MSA], [LoanAmount], [LoanTerm], [Amortization], [IOPeriod], [MarketLTV], [AverageLife], [MosForward], [GrossSpread], [ThreeYrTsy], [FiveYrTsy], [SevenYrTsy], [TenYrTsy], [ThirtyYrTsy], [InterpTsy], [USTRiskAdjSpread], [BPIRiskAdjSpread], [VarBPISprUSTSpr], [WgtAvgBenchSpread], [NetSpreadOverBmark]) VALUES('" & currentdate & " ', '" & dealname & "', '" & propertytype & "', '" & msa & "', '" & loanamount & "', '" & loanterm & "', '" & amortization & "', '" & ioperiod & "', '" & marketltv & "', '" & averagelife & "', '" & mosforward & "', '" & grossspread & "', '" & threeyrtsy & "', '" & fiveyrtsy & "', '" & sevenyrtsy & "', '" & tenyrtsy & "', '" & thirtyyrtsy & "', '" & interptsy & "', '" & ustriskadjspread & "', '" & bpiriskadjspread & "', '" & varbpisprustspr & "', '" & wgtavgbenchspread & "', '" & netspreadoverbmark & "')"


Set qdf = db.CreateQueryDef("", sSQL)


qdf.Execute dbFailOnError
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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