Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am required to provide a way for users to choose which column they want to display from a table. The user has a combo box of column names in a form which they can select one of and then hit a button which opens a report. I wrote some code which creates an SQL query based on the user's input, but it's not being called. I've tried inserting MsgBox statements, asserts, etc., but it never gets called. If I use a macro instead, that will be run, but not this sub...

Private Sub Report_Open(Cancel As Integer)
    Dim field As String
    Dim sqlQuery As String
    Dim ctl As Control
    ctl = "Forms![RuleGroupForm]![selectRuleFieldCombo]"

    field = "rules.[" & ctl & "]"

    ' E.g. if the field was "Source File", the final query should look like this:
    'SELECT overview.ID, relationship.rulesID, rules.[Source File], overview.[Rule Group], rules.RulegroupID
    'FROM (overview INNER JOIN relationship ON overview.id=relationship.id)
    'INNER JOIN rules ON relationship.rulesID=rules.ID
    'WHERE rules.[Source File] IS NOT NULL;

    sqlQuery = "SELECT overview.ID, relationship.rulesID, " & field
    sqlQuery = sqlQuery & ", overview.[Rule Group], rules.RulegroupID"
    sqlQuery = sqlQuery & "FROM (overview INNER JOIN relationship ON overview.id=relationship.id) "
    sqlQuery = sqlQuery & "INNER JOIN rules ON relationship.rulesID=rules.ID"
    sqlQuery = sqlQuery & "WHERE " & field & " IS NOT NULL;"
    Me.RecordSource = sqlQuery
    Requery

    debugText = "New SQL Query = " & sqlQuery
End Sub

Thanks.

Edit: Resolved, not sure how to respond to own question...

share|improve this question

closed as off-topic by rene, Unihedron, ItachiUchiha, Infinite Recursion, SilentKiller Sep 22 at 9:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – rene, Unihedron, ItachiUchiha, Infinite Recursion, SilentKiller
If this question can be reworded to fit the rules in the help center, please edit the question.

    
This was resolved, oddly enough, by closing Access (2007) and reopening it... –  Mitch Sep 7 '11 at 5:00
    
You can either answer your own question using the "Your Answer" box below, or - if you think the answer has little merit on the SO site, you can opt to delete your own question. –  Phil.Wheeler Sep 7 '11 at 5:19
    
Actually, you might not be able to do much without sufficient reputation, so your best option might be to either answer your own question or ask for some other action in the comments here. –  Phil.Wheeler Sep 7 '11 at 5:20
3  
For just about any computer-related problem, close and reopen should generally be tried before asking for help... –  Jean-François Corbett Sep 7 '11 at 6:39
    
@Mitch: glad it is solved, but for most operations involving your data, I would use On Load rather than On Open, since at Open time, you are not supposed to have your data available yet (at least in forms). And in this regard, I think the Requery in your code can be removed ! –  iDevlop Sep 7 '11 at 9:00

1 Answer 1

up vote 0 down vote accepted

There are a few things to check, apart from close and open, when you get odd behaviour such as this in Access. One is to see if the event on the properties sheet for the form or report, Open in this case, still contains [Event Procedure] - occasionally a form or report can have the code, but the event is not marked. When developing, that is, adding forms, reports and code, it is a good idea to run Compact and Repair on a regular basis, this also means regular back-ups - a simple copy saves a lot of grief. Finally, when developing fairly regular Decompiles are a good idea. I keep a small script that I can drop the Access file on to run decompile.

share|improve this answer
    
Thank you, Remou. Would you mind sharing your script? –  Mitch Sep 7 '11 at 22:08
    
@Mitch It needs updating, but here is the general idea: wiki.lessthandot.com/index.php/Decompile –  Fionnuala Sep 7 '11 at 22:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.