Thread: SQL query
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
PvtMadness PvtMadness is offline
external usenet poster
 
Posts: 4
Default SQL query

Here's the code. Right now it doesn't have SIZE in the SELECT. When
it's in there I get a runtime error: 'Method 'Open' of
object'_'Recordset' failed.

Regarding the other responses to this thread:
What does OP an abbreviation for?
Are we really using MS Query here or is this SQL in an ADO wrapper?
I'm a novice here so any info on where to learn more about this (so I
can speak intelligently) would be appreciated.

Thanks



Public Const gszConnection As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=R:\main.mdb;"

Public Sub PlainTextQuery()

Dim rsData As ADODB.Recordset
Dim szConnect As String
Dim szSQL As String



'Create the connection string
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=R:\main.mdb;"

'Create the SQL Statement
szSQL = " SELECT ID, Property_Name, Year_Built, Occupancy,
Effective_Rent, Load_Factor, Concession, Alteration_Costs_Renewals,
Alter_Costs_New_Releases, Pro_Rata_Charges, Escalators,
Pct_Brokerage_Renewals, Pct_Brokerage_New_Leases " & _
" FROM rentoffc " & _
" WHERE (ID = 11504 OR ID = 11337 OR ID = 10808 OR ID = 11571
OR ID = 11568 OR ID = 11569 OR ID = 11632 OR ID = 11572)"


'Create the Recordset Object and run the query
Set rsData = New ADODB.Recordset
rsData.Open szSQL, szConnect, adOpenForwardOnly, _
adCmdText

'Make sure we got records back
If Not rsData.EOF Then
'Dump the contents of the recordset onto the worksheet.
Sheet1.Range("A2:Z2").CopyFromRecordset rsData
'Close the recordset
rsData.Close
'Add headers to the worksheet.
With Sheet1.Range("A1:Z1")
.Value = Array("ID", "Property Name", "YearBlt", "Occ",
"EffRent", "Load", "Concession", "TI Renew", "TI New", "ProRata",
"Escalator", "Broker Renew", "Broker New", "SF", "QRent")
.Font.Bold = True
End With
'Fit the column width to the data
Sheet1.UsedRange.EntireColumn.AutoFit
Else
MsgBox "Error; No records returned.", vbCritical
End If

'Close the recordset if still open.
If CBool(rsData.State And adStateOpen) Then rsData.Close
Set rsData = Nothing

End Sub