jo15765
06-23-2015, 07:06 PM
This is my code, and it does not present any errors however, it doesn't delete the query or import the query. I put this into my masterdatabase that has the fully functioning and working query that I need to export to all databases listed in the table. Can someone point out what my issue is, and how to remedy?
Option Compare Database
Option Base 1
Private Sub formulaOne()
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rstTableName As DAO.Recordset
Dim myArray() As String
Dim intArraySize As Integer
Dim iCounter As Integer
Dim qryLoop As QueryDef
Dim exists As String
Dim dbs As Database
Const badqueryname As String = "qry_mailmerge"
Set rstTableName = CurrentDb.OpenRecordset("Information")
If Not rstTableName.EOF Then
rstTableName.MoveFirst
intArraySize = rstTableName.RecordCount
iCounter = 1
ReDim myArray(intArraySize)
Do Until rstTableName.EOF
myArray(iCounter) = rstTableName.Fields("DatabaseName")
iCounter = iCounter + 1
rstTableName.MoveNext
Loop
End If
If IsObject(rstTableName) Then Set rstTableName = Nothing
Set qd = CurrentDb.QueryDefs("qry_mailmerge")
Set ws = DBEngine(0)
For l = LBound(myArray) To UBound(myArray)
Set db = ws.OpenDatabase("L:\\" & myArray(l) & ".mdb")
For Each qryLoop In CurrentDb.QueryDefs
If qryLoop.Name = badqueryname Then
exists = "Yes"
DoCmd.DeleteObject acQuery, badqueryname
Exit For
End If
Next
On Error Resume Next
db.CreateQueryDef qd.Name, qd.SQl
db.Close
Set db = Nothing
Next l
End Sub
Option Compare Database
Option Base 1
Private Sub formulaOne()
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rstTableName As DAO.Recordset
Dim myArray() As String
Dim intArraySize As Integer
Dim iCounter As Integer
Dim qryLoop As QueryDef
Dim exists As String
Dim dbs As Database
Const badqueryname As String = "qry_mailmerge"
Set rstTableName = CurrentDb.OpenRecordset("Information")
If Not rstTableName.EOF Then
rstTableName.MoveFirst
intArraySize = rstTableName.RecordCount
iCounter = 1
ReDim myArray(intArraySize)
Do Until rstTableName.EOF
myArray(iCounter) = rstTableName.Fields("DatabaseName")
iCounter = iCounter + 1
rstTableName.MoveNext
Loop
End If
If IsObject(rstTableName) Then Set rstTableName = Nothing
Set qd = CurrentDb.QueryDefs("qry_mailmerge")
Set ws = DBEngine(0)
For l = LBound(myArray) To UBound(myArray)
Set db = ws.OpenDatabase("L:\\" & myArray(l) & ".mdb")
For Each qryLoop In CurrentDb.QueryDefs
If qryLoop.Name = badqueryname Then
exists = "Yes"
DoCmd.DeleteObject acQuery, badqueryname
Exit For
End If
Next
On Error Resume Next
db.CreateQueryDef qd.Name, qd.SQl
db.Close
Set db = Nothing
Next l
End Sub