This previous question with a solution by resident expert "Zorvek" may provide you with an alternate approach to your question:
[ http://www.experts-exchang
---
This is my most common tool: an Excel front-end with a SQL Server back end to store and share the data. All you need is some ADO skills to push and pull the data to and from the database. Here is an overview of using ADO in Excel VBA...
The following text and sample code illustrates how to:
-> open a database connection
-> use a stored procedure to perform a query
-> execute any SQL command against a database
-> open a recordset using a custom query
-> open a recordset using a table name
-> check for an empty recordset
-> read all records in a recordset
-> add a record to a recordset
-> delete a record from a recordset
-> copy a recordset with headers to a worksheet
-> close a recordset and database
This sample code, except for Open database method, can be used with any database such as Access, SQL Server, or Oracle. When using a database, most interaction happens via a recordset. Data is manipulated almost entirely using Recordset objects. Any number of Recordset objects can be created and used at the same time - each representing a different query or the same query. Different Recordset objects can access the same tables, queries, and fields without conflicting.
After opening a Recordset the Recordset can contain zero or more records. One record in the Recordset is always the current record except when the Recordset BOF or EOF property is true in which case no record is the current record. The current record is the record that is affected by any record-specific methods. To move amongst the records in a Recordset use the MoveNext, MovePrevious, MoveLast, and MoveFirst Recordset methods. A specific record can be made the current record by setting the AbsolutePosition property to the index number of the desired record. Fields in the current record are access as illustrated below.
Value = MyRecordset!Field1
MyRecordset!Field2 = Value + 1
When the current record is changed use the Update Recordset method to apply the changes to the database. Use the Add method to add a new record and the Delete method to delete the current record.
Before writing any ADODB code the data objects library "Microsoft ActiveX Data Objects x.x Library" must be referenced in the VBA project (Tools->References).
For additional information on the ADODB interface see the MSDN pages at:
http://msdn.microsoft.com/
Sample code:
Dim MyDatabase As ADODB.Connection
Dim MyCommand As ADODB.Command
Dim MyRecordset As ADODB.RecordSet
Dim Column As Long
' Open database connection
Set MyDatabase = New ADODB.Connection
MyDatabase.CursorLocation = adUseClient
MyDatabase.Open "Provider=Microsoft.Jet.OL
' For more information about Open syntax:
' http://msdn.microsoft.com/
' and navigate to Open Method (ADO Connection)
' Additional help constructing connection strings can be found at http://www.connectionstrin
' Query database using stored procedure (requires command object)
Set MyCommand = New ADODB.Command
Set MyCommand.ActiveConnection
MyCommand.CommandText = "qrySomeQuery" ' <- name of procedure
MyCommand.CommandType = adCmdStoredProc
With MyCommand
.Parameters.Refresh
.Parameters.Append .CreateParameter("QueryTex
.Parameters.Append .CreateParameter("QueryLon
.Parameters.Append .CreateParameter("QueryDat
.Parameters.Append .CreateParameter("QueryDat
.Parameters.Append .CreateParameter("BooleanP
' For more information about CreateParameter syntax:
' http://msdn.microsoft.com/
' and navigate to CreateParameter Method
End With
' Open recordset using command object
Set MyRecordset = New ADODB.Recordset
MyRecordset.Open MyCommand, , adOpenDynamic, adLockPessimistic
' For more information about Open method syntax:
' http://msdn.microsoft.com/
' and navigate to Open Method (ADO Recordset)
' Build a custom query using command object
Set MyCommand = New ADODB.Command
With MyCommand
Set .ActiveConnection = MyDatabase
.CommandType = adCmdText
.CommandText = "SELECT * From tblMyTable WHERE (tblMyTable.MyID = 1)"
End With
MyRecordSet.Open MyCommand, , adOpenDynamic, adLockReadOnly
' Execute any SQL statement
MyDatabase.Execute "INSERT INTO TableName (Field1, Field2) VALUES ('" & Range("A1").Value & "','" & Range("A2").Value & "')"
' Open a recordset by specifying specific table (no query)
MyRecordset.Open "TableName", MyDatabase, adOpenDynamic, adLockPessimistic
' Open a recordset by specifying query without using command object
MyRecordset.Open "SELECT * FROM MyTable", MyDatabase, adOpenDynamic, adLockPessimistic
' Test for no records
If MyRecordset.BOF And MyRecordset.EOF Then
MsgBox "No records in table"
End If
' Determine total records
MsgBox "Total records: " & MyRecordset.RecordCount
' Look at all records in record set
While Not MyRecordset.EOF
MsgBox "Record number: " & MyRecordset.AbsolutePositi
MyRecordset.MoveNext
Wend
' Copy the entire recordset to a worksheet (this technique does not copy field names)
Sheets("Sheet1").[A2].Copy
' Create headers and copy data
With Sheets("Sheet1")
For Column = 0 To MyRecordset.Fields.Count - 1
.Cells(1, Column + 1).Value = MyRecordset.Fields(Column)
Next
.Range(.Cells(1, 1), .Cells(1, MyRecordset.Fields.Count))
.Cells(2, 1).CopyFromRecordset MyRecordset
End With
' Update current record
MyRecordset!Field1 = "Some data"
MyRecordset!Field2 = "Some more data"
MyRecordset.Update
' Move specific fields from current record to worksheet
With Sheets("Sheet1")
Cells(Row, "A") = MyRecordset!Field1
Cells(Row, "B") = MyRecordset!Field2
End With
' Add new record and set field values
MyRecordset.AddNew
MyRecordset!Field1 = "Some data"
MyRecordset.Update
' Delete current record
MyRecordset.Delete
' Close recordset
MyRecordset.Close
' Close database
MyDatabase.Close
Kevin
---
BFN,
fp.
Main Topics
Browse All Topics





by: vsudipPosted on 2008-06-13 at 20:55:30ID: 21784012
here u go.. nter.com/B est/excel- odbc-sql.h tml
http://www.freedownloadsce