View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
ed ed is offline
external usenet poster
 
Posts: 59
Default Excel Driven Access Queries

You're the man! Thanks!

-----Original Message-----
Ed

The following code should get you started. It is

slightly different from your request in that it creates a
temporary query rather than updates an existing MS Access
query. Generation of the sql from the variables in your
spreadsheet should be reasonably easy. The output only
has one heading as the table has one field. This will
have to be modified.

When you build the code, you will have to reference the

DAO 2.5/3.5 compatibility library. To do this in the
VBE, go Tools, References, scroll down to the library and
check.

Tony

Sub ccc()
Dim wrkjet As workspace
Dim dbs As database
Dim qdef As QueryDef
Dim rst As Object


Dim wks As Worksheet, cl As Integer, rw As Integer
Dim mydatabase As String
Dim myquery As String
mydatabase = "full\path\and\databasename.mdb"

Set wrkjet = CreateWorkspace("", "admin", "", dbUseJet)
Set dbs = wrkjet.OpenDatabase(mydatabase)

myquery = "select * from tablename where field like 'b*'"

Set qdef = dbs.CreateQueryDef("", myquery)

With qdef
Set rst = .OpenRecordset()
End With


Set ws = Worksheets("sheet2")
With ws
.Range("a1").Value = rst.Fields(0).Name
.Range("a2").CopyFromRecordset rst
End With


rst.Close
dbs.Close
wrkjet.Close

Set ws = Nothing
Set wrkjet = Nothing
Set rst = Nothing
Set dbs = Nothing
Set qdef = Nothing

End Sub

.