We're replacing an old AS 400 database with a new SQL database. I've recreated the view in the SQL database but now I have an Excel spreadsheet referencing the AS 400 via DSN connection through a macro. Here is the current connection string:
What I am trying to get to work is:
But when I run the macro I get a Run-time error '1004: Application-defined or object-defined error. Just for more reference here's the rest of the original connection and it's sql statement:
I have altered the FROM statement in the query and hope it works with SQL but haven't gotten past the connection for testing:
Thanks for any help!
Code:
With ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=AS 400;", _
Destination:=Sheets("temp").Range("A1"))
What I am trying to get to work is:
Code:
With ActiveSheet.QueryTables.Add(Connection:="Provider=SQLOLEDB;Data Source=EnertiaProd;Initial Catalog=EnertiaValidation;Integrated Security=SSPI;", _
Destination:=Sheets("temp").Range("A1"))
But when I run the macro I get a Run-time error '1004: Application-defined or object-defined error. Just for more reference here's the rest of the original connection and it's sql statement:
Code:
With ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=AS 400;", _
Destination:=Sheets("temp").Range("A1"))
.CommandText = Array( _
"SELECT digits(ENGOPF.PRODYR)||'-'||digits(ENGOPF.PRODMO)||'-'||digits(ENGOPF.PRODDA), SUM(ENGOPF.H2OBBL) FROM S1021663.LIB003.ENGOPF ENGOPF " _
, _
"WHERE (ENGOPF.WELL = '" & wellnum & "') AND (ENGOPF.LEASE=" _
, _
leasenum & ") AND (digits(ENGOPF.PRODYR)||'-'||digits(ENGOPF.PRODMO)||'-'||digits(ENGOPF.PRODDA)>='" _
& startdate & "') AND (digits(ENGOPF.PRODYR)||'-'||digits(ENGOPF.PRODMO)||'-'||digits(ENGOPF.PRODDA)<='" & enddate & "') " _
, _
"GROUP BY (ENGOPF.LEASE),(ENGOPF.WELL),digits(ENGOPF.PRODYR)||'-'||digits(ENGOPF.PRODMO)||'-'||digits(ENGOPF.PRODDA)" _
, _
"ORDER BY digits(ENGOPF.PRODYR)||'-'||digits(ENGOPF.PRODMO)||'-'||digits(ENGOPF.PRODDA)" _
)
.Name = "download"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = False
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.Refresh BackgroundQuery:=False
End With
I have altered the FROM statement in the query and hope it works with SQL but haven't gotten past the connection for testing:
Code:
"SELECT digits(ENGOPF.PRODYR)||'-'||digits(ENGOPF.PRODMO)||'-'||digits(ENGOPF.PRODDA), SUM(ENGOPF.H2OBBL) FROM dbo.vwExtProducedWater ENGOPF " _
Thanks for any help!