renaming queries.

buggy2

Board Regular
Joined
Feb 28, 2003
Messages
69
I am designing a form and in using it the users create new queries all of which are saved as "query1", and created by createquerydef. However I need to force the user to rename the query when they create it or it will result in an error when they create their next query. Is there a "save as" function i can use in access vb.


(y)
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
If you can post the code where you are creating the querydef, I'm sure someone will be able to help you.

Thank you,

Russell
 
Upvote 0
thanks here it is!

Private Sub Bttn_Click()
Dim qdf As DAO.QueryDef
Dim db As DAO.Database

Set db = CurrentDb

If opt_count.Value = True Then
'getstrcnt contains an sql string
strSQL = getstrcnt()
End If

Set qdf = db.CreateQueryDef("qryNew", strSQL)
DoCmd.OpenQuery "qryNew"

db.QueryDefs.Delete ("qryNew")

Set qdf = Nothing
Set db = Nothing

End Sub

(y)
 
Upvote 0
I would just prompt the user for the name, and not delete the QueryDef, something like this:
Code:
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
There are other ways to do it - just use the one that seems easiest to you. Step through the code and enter an invalid name (use an "!" in the name, for example).

HTH,

Russell
 
Upvote 0
Firstly thank you, that worked perfectly. Do you have any idea how to go about exporting the query made to excel?
 
Upvote 0
Please try searching this board for that, as it has been asked many times. You can also look up TransferSpreadsheet in Access help.

If you can't find what you need after that, let me know and I'll see what I can do.

HTH,

Russell
 
Upvote 0
HI Russel,

Could you explain the point of creating querydefs? Is it nothing more than saving as SQL expression as a named query?

Just curious...

Thanks!
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,357
Members
451,642
Latest member
mirofa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top