Run Time Error 1004 in Query To SQL Db

  • Thread starter Thread starter QB
  • Start date Start date

QB

Board Regular
Joined
Jul 5, 2004
Messages
93
Hi

I'm using OLE DB Query in xL 2003 to query an SQL database. I keep getting a run time error when the line .Connection = Range("nConnection").Value in the code below executes. There is no problem with the definition of my named range "nConnection", the connection string is correct and the SQL syntax in cell $A$1 is correct.

Can anyone help a bemused accountant?

Regards

QB

----------------------------------------

Sub Update_05_Sum_AA_CY()
Dim LastColumn As Long
Dim LastRow As Long

Sheets("05_Sum_AA_CY").Activate

' Update Query
With Range("Sum_AA_CY").QueryTable
.Connection = Range("nConnection").Value
.CommandType = xlCmdSql
.CommandText = Range("$A$1").Value
.Refresh BackgroundQuery:=False
End With

' Set Data Range Properties
With Range("Sum_AA_CY").QueryTable
.Name = "Sum_AA_CY"
.SavePassword = False
.BackgroundQuery = False
.RefreshPeriod = 0
.RefreshOnFileOpen = False
.SaveData = False
.FieldNames = True
.RowNumbers = False
.AdjustColumnWidth = False
.PreserveColumnInfo = True
.PreserveFormatting = True
.RefreshStyle = xlInsertDeleteCells
.FillAdjacentFormulas = True
End With

....
....
....

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Have you tried prefacing with what sheet the Range is on ?

Something like this?

Code:
' Update Query
With [B][COLOR=#ff0000]YourSheetNameHere.[/COLOR][/B]Range("Sum_AA_CY").QueryTable
.Connection = [B][COLOR=red]YourSheetNameHere.[/COLOR][/B]Range("nConnection").Value
.CommandType = xlCmdSql
.CommandText = [B][COLOR=#ff0000]YourSheetNameHere.[/COLOR][/B]Range("$A$1").Value
.Refresh BackgroundQuery:=False
End With
 
Upvote 0
Hi

Thanks for the suggestion. I had tried to add the worksheet name to the connection line without success. I still get the RTE 1004 when I add the names as you suggest.

Can anyone else help me?

Regards

QB
 
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,654
Members
452,934
Latest member
mm1t1

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