Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Run a Database Query within VB

How simple is it to Run a Database Query via VB?

I have ideas in my head to connect to an Access database, extract them to a
file and run various calculations on the Query. Haven't done anything like
this before so I am happy to do it by trial and error

Is there any good web site that give practical examples

Thanks


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Run a Database Query within VB

Thanks for the Reply. What exactly is the MDAC SDK? - looked it up on the MS
site would it be essential to have it?


"AA2e72E" wrote in message
...
It is quite simple. Here's an example:

Sub xx()
Cnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\nwind.mdb"
Sql = "SELECT * FROM customers"
Set adors = CreateObject("ADODB.RecordSet")
adors.Open Sql, Cnn
While Not adors.EOF ' loop through
MsgBox adors.fields("CompanyName").Value ' see a value
adors.MoveNext 'Next Record
Wend
adors.Close ' End connection
End Sub


You should download the MDAC SDK from Microsoft (its free): this contains

the documentation for ADO, which you would need.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Run a Database Query within VB

I recently wrote some code to do just that. I am new to VB programmin
so this code is probably a bit unwieldy and not as effecient as i
could be but here it is. To get the SQL statements i just went int
the access database that I wanted to query. Designed the query i
Access (which is relatively straight forward) then switched to SQL vie
to get the statement for the query. I broke it up into parts to mak
it a little easier to read but you could just copy and past it directl
into the VB code and it should work. Anyway, here is the code tha
goes through all the connecting, querying and the filling into excel.
I cut out some parts that weren't relevant so there may be a fe
undeclared variables here and there but I think I got the importan
bits. Hope it helps.

P.S. sorry about the lack of formatting. oh well.



Public Sub QueryWarrantyDatabase()


Dim conn As ADODB.Connection
Dim WarrantyData As ADODB.Recordset

Dim SQLSelect As String
Dim SQLFrom As String
Dim SQLWhere As String
Dim SQLQuery As String

Dim DatabasePath As String


'Set DatabasePath
DatabasePath = "G:\QUALITY\Warranty Database\Warrant
Database.mdb"



'Create the connection to the Warranty Database
Set conn = New ADODB.Connection
With conn
.Provider = "Microsoft.JET.OLEDB.4.0"
.Open DatabasePath
End With

'Define the SQL query string for Warranty returns
SQLSelect = "SELECT [Main Data Table].[Project Number], _
[Main Data Table].[Model Number], [Main Dat
Table].[Serial Number], [Main Data Table].[Customer], " _
& "[Main Data Table].[Description Of Failure], [Mai
Data Table].[Root Cause Of Failure], [Main Data Table].[Year O
Manufacture], " _
& "[Main Data Table].[Month Of Manufacture], [Main Dat
Table].[Year Of Return], [Main Data Table].[Month Of Return], [Mai
Data Table].[Product Line], " _
& "[Main Data Table].[Unit Age (months)], [Main Dat
Table].[Warranty/Non Warranty/Warranty Period But Non Warrant
(W,NW,WP)]"
SQLFrom = "FROM [Main Data Table]"
SQLWhere = "WHERE ((([Main Data Table].[Month Of Return])=" & Mont
& ") AND (([Main Data Table].[Product Line]) Like 'custo
brushless'));"

SQLQuery = SQLSelect & " " & SQLFrom & " " & SQLWhere

'Open a new Recordset to hold the Warranty Data
Set WarrantyData = New ADODB.Recordset

'Fill the Recordset with data returned from query
With WarrantyData
.ActiveConnection = conn
.Open SQLQuery, conn, adOpenDynamic, adLockBatchOptimistic
End With




'Update "Warranty Query" sheet with warranty data

WarrantyData.MoveFirst
Do While Not WarrantyData.EOF
With Worksheets("Warranty Query")
.Cells(NewRow, 1) = WarrantyData(0)
.Cells(NewRow, 2) = WarrantyData(1)
.Cells(NewRow, 3) = WarrantyData(2)
.Cells(NewRow, 4) = WarrantyData(3)
.Cells(NewRow, 5) = WarrantyData(4)
.Cells(NewRow, 6) = WarrantyData(5)
.Cells(NewRow, 7) = WarrantyData(6)
.Cells(NewRow, 8) = WarrantyData(7)
.Cells(NewRow, 9) = WarrantyData(8)
.Cells(NewRow, 10) = WarrantyData(9)
.Cells(NewRow, 11) = WarrantyData(10)
.Cells(NewRow, 12) = WarrantyData(11)
.Cells(NewRow, 13) = WarrantyData(12)
End With
NewRow = NewRow + 1
WarrantyData.MoveNext
Loo

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 599
Default Run a Database Query within VB

Corbin

"Warranty Query" sheet with warranty data

WarrantyData.MoveFirst
Do While Not WarrantyData.EOF
With Worksheets("Warranty Query")
Cells(NewRow, 1) = WarrantyData(0)
Cells(NewRow, 2) = WarrantyData(1)
Cells(NewRow, 3) = WarrantyData(2)
Cells(NewRow, 4) = WarrantyData(3)
Cells(NewRow, 5) = WarrantyData(4)
Cells(NewRow, 6) = WarrantyData(5)
Cells(NewRow, 7) = WarrantyData(6)
Cells(NewRow, 8) = WarrantyData(7)
Cells(NewRow, 9) = WarrantyData(8)
Cells(NewRow, 10) = WarrantyData(9)
Cells(NewRow, 11) = WarrantyData(10)
Cells(NewRow, 12) = WarrantyData(11)
Cells(NewRow, 13) = WarrantyData(12)
End With
NewRow = NewRow + 1
WarrantyData.MoveNext
Loop


You might take a look at the CopyFromRecordset method. It does pretty much
what your loop does, and probably a lot quicker.

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Run a Database Query within VB

Thanks Dick,

That was much quicker and only took one line of code

--
Message posted from http://www.ExcelForum.com



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 141
Default Run a Database Query within VB

Thanks, there seems to be a huge number of downloads, which is the best one
for a 'beginner


"AA2e72E" wrote in message
...
MDAC = Microsoft Data Access Component.
SDK = Software Documentation Kit.

The SDK contains, among other things, documentation for ADO (ActiveX Data

Object): you would use this to talk tio the database. Yes you would need the
SDK or will need to buy a book. "ADO 2.6 " from WROX is quite good but the
current version is 2.8 (also free).

PS: In the last post, the Access NWIND.MDB database is used, and the table

CATEGORIES is read. You can use any data source (virtually), inclusing Excel
workbooks, text files, Oracle, SQL Server , DB2 etc etc.


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
Import New Database Query (Union Query) in Spreadsheet klock Excel Discussion (Misc queries) 2 September 24th 09 01:30 AM
database query not showing foxpro database How I import data mangat New Users to Excel 1 June 24th 07 03:31 PM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 1 November 29th 05 01:44 PM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 0 November 28th 05 06:37 PM
How to use a Access Query that as a parameter into Excel database query Karen Middleton Excel Discussion (Misc queries) 1 December 13th 04 07:54 PM


All times are GMT +1. The time now is 07:42 PM.

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"