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:
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
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
Kelly