Function CreateQuery(strSrcName As String, strQryName As String)
'************************************************************
' (c) 2007 CT WITTER *
' All rights reserved. *
' *
' You may only use this code as part of an application *
' that requires its use. You must including this *
' notice intact. You may not distribute the code *
' as your own work, nor can you distribute the *
' code on its own. *
'************************************************************
Dim catDB As ADOX.Catalog
Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Dim strSQL As String
'Set up and Open the Source Data
Set rst = New ADODB.Recordset
rst.ActiveConnection = CurrentProject.Connection
rst.CursorType = adOpenStatic
rst.Open "Select * from " & strSrcName
'Beginning SQL String
strSQL = "UPDATE Agent_Master SET Agent_Master.[Team A Prod Aux] = "
Do Until rst.EOF
'Build query string
strSQL = strSQL & "[Agent_Master]![" & rst.Fields(0) & "]+"
rst.MoveNext
Loop
rst.Close
'Clean up SQL
'Remove Last "+" and add the ";"
strSQL = Left(strSQL, Len(strSQL) - 1) & ";"
'Create the Destination Query
Set catDB = New ADOX.Catalog
' Open the Catalog object.
catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & CurrentProject.Path & "\" & CurrentProject.Name
Set cmd = New ADODB.Command
' Define SQL statement for query and set provider-specific
' properties for query type and ODBC connection string.
With cmd
.ActiveConnection = catDB.ActiveConnection
.CommandText = strSQL
End With
'Test to See if Destination Query Exists, Delete if necessary
If ExistsQuery(strQryName) Then catDB.Procedures.Delete strQryName
' Name and save query to Procedures collection.
catDB.Procedures.Append strQryName, cmd
Application.RefreshDatabaseWindow
Set rst = Nothing
Set catDB = Nothing
End Function
Function ExistsQuery(strQueryName As String)
'************************************************************
' (c) 2007 CT WITTER *
' All rights reserved. *
' *
' You may only use this code as part of an application *
' that requires its use. You must including this *
' notice intact. You may not distribute the code *
' as your own work, nor can you distribute the *
' code on its own. *
'************************************************************
Dim strTemp As String
On Error Resume Next
strTemp = CurrentDb.QueryDefs(strQueryName).Name
If Err.Number = 0 Then
ExistsQuery = True
Else
ExistsQuery = False
End If
End Function