Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create query at runtime

Status
Not open for further replies.

KellyK

Programmer
Mar 28, 2002
212
US
hi tek tippers!

I am attempting to create a query at runtime. I am using code from Alison Balter's Mastering Microsoft Access 2000 Development which follows:
Code:
Sub CreateQuery(strQueryNam As String, strCommandText As String)
    Dim cat As ADOX.Catalog
    Dim cmd As ADODB.Command
    Dim strSQL As String
    
    Set cat = New ADOX.Catalog
    cat.ActiveConnection = CurrentProject.Connection
    
    Set cmd = New ADODB.Command
    cmd.CommandText = strCommandText
    cat.Views.Append strQueryNam, cmd
    cat.Views.Refresh
    'DoCmd.OpenQuery "qselWeeklyDetail"
    
    Set cat = Nothing
    Set cmd = Nothing
    
End Sub
I modified her code a little by adding strQueryNam and strCommandText so I can reuse the sub to build different queries. In one instance, I pass the values of 'qselWeeklyDetail' for the strQueryNam and 'SELECT * from tblSalesDetail' for the strCommandText. However, when the sub is finished, I cannot see the query in my query container. If I attempt to rerun the code, I get an error saying "Object 'qselWeeklyDetail' already exists". Yet when I add the line DoCmd.OpenQuery "qselWeeklyDetail", I get an error "Microsoft Access cannot find the object 'qselWeeklyDetail'. I have shut down and reentered the database and this problem continues. I've also tried showing Hidden Objects. If this query already exists, how come I don't see it? Am I missing something? Any suggestions are appreciated!

Kelly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top