Private Sub Bttn_Click()
On Error GoTo HandleErr
Dim qdf As DAO.QueryDef
Dim db As DAO.Database
Dim fOk As Boolean
Dim strQryName As String
Set db = CurrentDb
If opt_count.Value = True Then
'getstrcnt contains an sql string
strSQL = getstrcnt()
End If
Do While Not fOk
fOk = True
strQryName = InputBox("Please name query") 'you'll be more creative here
Set qdf = db.CreateQueryDef(strQryName, strSQL)
DoCmd.OpenQuery strQryName
Loop
RefreshDatabaseWindow
ExitHere:
Set qdf = Nothing
Set db = Nothing
Exit Sub
HandleErr:
Select Case Err.Number
' The following error numbers are:
' 3012 - Object already exists (duplicate query name),
' 3125 - Not a valid name, and
' 7874 - Can't find object (query does not exist - due to invalid or duplicate name)
Case 3012, 3125, 7874
fOk = False
Resume Next
Case Else
MsgBox Err.Description
GoTo ExitHere
End Select
End Sub