Hello,
I'm fairly new to the world of VBA and I'm struggling with a current macro. The macro was originally written in using VBA in Excel 2003, however I want to start using in Excel 2010. I thought that it would work without making any changes, however this has not been the case and I've come to a point where I don't know what else to do. Here's the code:
Public Cancelled As Boolean
Public LoggedIn As Boolean
Public SkipRunReports As Boolean
Public sConn As String
Public Sub RunReport(SQL)
Dim dStartDate As Date
Dim sStartDate As String
Dim sEndDate As String
Dim oQt As QueryTable
If Not LoggedIn Then
LoginForm.Show
If Cancelled Then Exit Sub
sConn = "ODBC;DSN=ora" & LCase(LoginForm.SchemaTextbox.Value) & ";UID=" & LoginForm.UsernameTextbox.Value & ";PWD=" & LoginForm.PasswordTextbox.Value & ";DBQ=" & LoginForm.SchemaTextbox.Value & ";DBA=W;APA=T;EXC=F;XSM=Default;FEN=T;QTO=F;FRC=10;FDL=10;LOB=T;RST=T;GDE=F;FRL=T;BAM=IfAllSucce"
End If
SelectAllData
Selection.ClearContents
Set oQt = ActiveSheet.QueryTables.Add( _
Connection:=sConn, _
Destination:=ActiveSheet.Range("$B$7"), _
SQL:=SQL)
oQt.FieldNames = False
oQt.RowNumbers = False
oQt.FillAdjacentFormulas = False
oQt.PreserveFormatting = True
oQt.RefreshOnFileOpen = False
oQt.BackgroundQuery = True
oQt.RefreshStyle = xlOverwriteCells
oQt.SavePassword = False
oQt.SaveData = True
oQt.AdjustColumnWidth = False
oQt.RefreshPeriod = 0
oQt.PreserveColumnInfo = True
oQt.Refresh BackgroundQuery:=False
//error occurs in the line above//
Conn = oQt.Connection
oQt.Delete
Set oQt = Nothing
ActiveSheet.Range("B7").Select
If ActiveSheet.Range("B7") = "" Then ActiveSheet.Range("B7") = 0
End Sub
From stepping through the macro and surfing the net, I've come to narrow down the problem to the indicated line. As far as I can tell, the error is happening becuase my query inputs are either invalid or malformed. When using Excel 2003, the program works fine; but when using Excel 2010 I get the following error:
Run-time error '-2147217842(80040e4e)': Method 'Refresh' of object '_QueryTable' failed.
As far as I can tell, my driver is able to connect me to the appropiate database (though I may be wrong). Does anyone have any feedback that could help me? It would be greatly appreciated.
Many thanks in advance,
DJ05
Bookmarks