Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Query Data from a Sheet

Hi,

I need to query data from a reference table.

I have a reference sheet and users need to input certain
information, and to query certain data from this
reference sheet. The function is like a SQL. My question
is how to write a query in Excel VBA? Can I define this
reference sheet as a table and query it in VBA code?

Thanks

Ming

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Query Data from a Sheet

Hello, yes you can, here's a pertinent MS KB:

http://support.microsoft.com:80/supp.../q257/8/19.asp

And one recent example I cooked up:

http://mrexcel.com/board2/viewtopic....50393&start=31

Regards,
Nate Oliver
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Query Data from a Sheet

Dear Nate Oliver

Thanks for your code. But when I run my own code, I got the error
message as follows

.Open , , 3, 3
"On value given for one or more required parameters"

Attached is my code. What is wrong?

Thanks

Ming

Sub GetMaxLTV()

Dim cn As Object, rs As Object
Dim clcMde As Long
Dim StdMx As Object

clcMde = Application.Calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' Sheets("StdM").[a2:n81].ClearContents

Set cn = CreateObject("ADODB.Connection")
Set StdMx = Worksheets("stdM").Range("A1:N81")

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
ThisWorkbook.FullName & ";Extended Properties=Excel 8.0;" 'Create
DB connection

Set rs = CreateObject("ADODB.Recordset")

With rs
Set .ActiveConnection = cn
.Source = "Select [H]" & _
"From [StdM$A1:H81] where [a] = 1 " & _
"and [b] = 'FULL' and [c]= 'Second Home' and [d] =
'PURCHASE/REFINANCE'" & _
"and [e] < 1000000"
.Open , , 3, 3
Sheets("Pricing").[a1].CopyFromRecordset rs
.Close
End With

cn.Close
Set rs = Nothing: Set cn = Nothing

Application.Calculation = clcMde
Application.ScreenUpdating = True

End Sub





*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Query Data from a Sheet

Hello again Ming,

Instead of referring to columns (e.g., [A], [H], etc...) Fill row 1 with headers, and refer to those headers. E.g., [h] becomes the text in h1, e.g., [Header 8]. ADO can then interpret your data sheet as a table with row 1 identifying the fields.

Sql errors will error out on the open. Hope this helps.

Regards,
Nate Oliver
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 459
Default Query Data from a Sheet

Looks like you are missing a space in your SQL e.g.

'PURCHASE/REFINANCE'" & _
" and [e] < 1000000"

--

Ming Shao wrote in message ...
Dear Nate Oliver

Thanks for your code. But when I run my own code, I got the error
message as follows

.Open , , 3, 3
"On value given for one or more required parameters"

Attached is my code. What is wrong?

Thanks

Ming

Sub GetMaxLTV()

Dim cn As Object, rs As Object
Dim clcMde As Long
Dim StdMx As Object

clcMde = Application.Calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' Sheets("StdM").[a2:n81].ClearContents

Set cn = CreateObject("ADODB.Connection")
Set StdMx = Worksheets("stdM").Range("A1:N81")

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
ThisWorkbook.FullName & ";Extended Properties=Excel 8.0;" 'Create
DB connection

Set rs = CreateObject("ADODB.Recordset")

With rs
Set .ActiveConnection = cn
.Source = "Select [H]" & _
"From [StdM$A1:H81] where [a] = 1 " & _
"and [b] = 'FULL' and [c]= 'Second Home' and [d] =
'PURCHASE/REFINANCE'" & _
"and [e] < 1000000"
.Open , , 3, 3
Sheets("Pricing").[a1].CopyFromRecordset rs
.Close
End With

cn.Close
Set rs = Nothing: Set cn = Nothing

Application.Calculation = clcMde
Application.ScreenUpdating = True

End Sub





*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 / MS Query - editing existing query to another sheet Hotpepperz Excel Discussion (Misc queries) 0 June 13th 08 06:53 PM
How can I Run a query from VB macro with out affect current data in the same sheet? oafdl Excel Discussion (Misc queries) 0 May 29th 06 09:42 PM
how to extend data to next sheet automatically during sql query Asha Excel Discussion (Misc queries) 1 October 13th 05 06:26 PM
Returning data to Excel sheet from MS query SNB Excel Discussion (Misc queries) 0 April 27th 05 01:27 AM
Query Data from a Sheet Ming Shao Excel Programming 0 April 13th 04 06:53 PM


All times are GMT +1. The time now is 05:53 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"