Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Deleting MS Access queries using vb6

Can anyone tell me why the code provided below only seems to delete about 8 queries max? It then complains that the object is no longer set. (I used a reference to Microsoft DAO 3.6 Object Library)

Dim db As dao.Database
Dim qd As QueryDef

Set db = DBEngine.Workspaces(0).OpenDatabase(CurDir & "db.mdb")

For Each qd In db.QueryDefs
db.QueryDefs.Delete qd.Name
Next

db.Close


Thanks!

Comments

  • BarkeeperBarkeeper Member Posts: 335
    : Can anyone tell me why the code provided below only seems to delete about 8 queries max? It then complains that the object is no longer set. (I used a reference to Microsoft DAO 3.6 Object Library)
    :
    : Dim db As dao.Database
    : Dim qd As QueryDef
    :
    : Set db = DBEngine.Workspaces(0).OpenDatabase(CurDir & "db.mdb")
    :
    : For Each qd In db.QueryDefs
    : db.QueryDefs.Delete qd.Name
    : Next
    :
    : db.Close
    :
    :
    : Thanks!
    :
    :

    Because you're changing the Count of the QueryDefs-Collection inside your For-Next-Loop.

    Better Way:
    For i=db.Querydefs.Count to 0 Step -1
    db.Querydefs.delete db.Querydefs(i) 'Or similiar. I don't remember the exact code
    Next

    Zvoni
    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein

Sign In or Register to comment.