Yes, well, I'm afraid you've just about hit the limit of what one can do with a macro... Can't tell you that for sure, because I haven't used macros in a long time, but since they're basically one-line programs, there's no way to do things like test for values.
That being said, as far as VBA goes, what you're trying to do is quite straightforward. Here's the code, which you can copy directly, changing the name of the query to whatever yours might be:
Public Function TestQueryOnStartup()
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("MyQueryName", dbOpenSnapshot)
If rst.RecordCount > 0 Then
DoCmd.OpenQuery "MyQuery"
Else
MsgBox "No matching records found."
End If
rst.Close
End Function
Here's what you do (I'm going to assume you're a total newbie to VBA, so take no offense, please):
1. Create a new module (DB window --> modules --> New) You should see a blank page saying Option Compare Database, maybe Option Explicit, too.
2. Paste the above code into that window, changing the query name and, if you want, the message text to show if no records are found.
3. This code uses DAO, or Data Access Objects, which we won't really go into right now. Suffice it to say that it's a collection of stuff not 100% a part of Access. So you need to set a "reference" to the code, to make it work. This is how you do that, still in the VBA Project Editor (or module design window, if you prefer): Tools --> References. Scroll down through the list of available references until you find Microsoft DAO 3.6 Object Library. Check its box. Click OK.
4. Now you need to test it to make sure the code's okay. Choose Debug --> Compile. This uses MS's programming engines to test the integrity of your code. If there's a problem, post back with the error message, but normally all should be well.
5. Now, in your AutoExec macro, you need to add the RunCode statement, with the name of the function including the parentheses, like so:
TestQueryOnStartup()
That's it.
What the code does, very briefly, is create a recordset, which is a kind of memory-only version of your query. It counts the number of records; if there's at least one, it opens the query on your screen. If not, it shows you a message so that you know the test has been run.
Hope this helps.
__________________ complicated music button |