Thanks for the code, but I decided to instead compare the new list of queries with the old list stored in array, because it is possible that the system date could get changed.
I'm trying to improve my coding technique, so I've listed my code below. It seems to do what I want, but any suggestions for improvement are welcome. Note that the IsObjectOpen function is a standard one that I took from somewhere, so I've not include the code for that here. I'll be adding error handler code to the CheckNewQueryName subroutine. Also, global variables are defined up at the top of the module (not shown here) and I'm not good about defining all variables. Note that I have added a single quote on blank lines, because this is the only way I know of to include blank lines so that the code is more readable. If you know of a better method please lete me know.
Finally, I have a counter, NbrNewQueries, at the end. Strangely when a new query is created, I also get back that two queries have been created. One is, in fact, the new query and the other one is always the same query that starts with ~sql. It's not really a problem as it doesn't get renamed, I just don't know why this same query pops up as being new.
CODE
Private Sub New_Query_Button_Click()
'
Dim NewlyCreatedQuery As String
'
On Error GoTo Err_New_Query_Button_Click
'First get all existing queries and put them into an array so that
'the code can find the new query afterwards.
Set db = CurrentDb()
'
For k = 0 To db.QueryDefs.Count - 1
ExistingQueries(k) = db.QueryDefs(k).Name
Next k
TotalNbrExistingQueries = k + 1
'
'Now call built-in new query wizard
RunCommand acCmdNewObjectQuery
'
'Make sure that if the dbWindow was displayed that it is closed so
'user cannot see it
DoCmd.SelectObject acTable, , True
DoCmd.RunCommand acCmdWindowHide
'
'Get the name of the newly created query, if any
Call CheckNewQueryName(NewlyCreatedQuery)
'
Exit_New_Query_Button_Click:
Exit Sub
'
Err_New_Query_Button_Click:
'
If Err = 2501 Then
Resume Next
End If
'
'Make sure that the db Window is not displayed
DoCmd.SelectObject acTable, , True
DoCmd.RunCommand acCmdWindowHide
'
MsgBox Err.Description, vbOKOnly + vbInformation
Resume Exit_New_Query_Button_Click
'
End Sub
'
'+++++++++++++++++++++++++++++++++++++++++++++++++
Public Sub CheckNewQueryName(NewUserQuery As String)
'
'Get all of the current queries
Set db = CurrentDb()
'
NbrNewQueries = 0
For k = 0 To db.QueryDefs.Count - 1
For j = 1 To TotalNbrExistingQueries
If db.QueryDefs(k).Name = ExistingQueries(j) Then
'Move to next query name
GoTo 100
End If
Next j
NewUserQuery = db.QueryDefs(k).Name
'Close the query if it is open so it can be renamed
'
If IsObjectOpen(NewUserQuery, 1) = True Then
DoCmd.Close acQuery, NewUserQuery
End If
'
'Now modify the name to remove the identifier
If InStr(1, NewUserQuery, "rjd") > 0 Then
ModifiedQueryName = Replace(NewUserQuery, "rjd", "")
DoCmd.Rename ModifiedQueryName, acQuery, NewUserQuery
'Now open the query again so the user can see it
DoCmd.OpenQuery ModifiedQueryName, , acReadOnly
End If
'
NbrNewQueries = NbrNewQueries + 1
100: Next k
'
End Sub
Edited by: rdemyan on Mon Sep 19 13:24:15 EDT 2005.
Edited by: rdemyan on Mon Sep 19 13:28:56 EDT 2005.