Solved Select case error

ClaraBarton

Registered User.
Local time
Today, 15:16
Joined
Oct 14, 2019
Messages
661
I've messed and messed with this.
When I debug, the intChapter is 0 but it still jumps to the 2nd case and then throws an error that the element isn't found. It isn't found because the node belongs on a book (case 1) and not a chapter (case 2).
I know you always find my logic errors... What am I doing wrong here?

Code:
 strSql = "SELECT t_recipe.cookbookid, t_recipe.recipeid, " & _
        "t_recipe.recipename, t_recipe.cookbookchapterid " & _
        "FROM t_recipe " & _
        "ORDER BY t_recipe.cookbookchapterid"
    Set rsRecipes = db.OpenRecordset(strSql)
        Dim intChapter As Integer
        intChapter = Nz(rsRecipes!cookbookchapterid, 0)
        
        Do Until rsRecipes.EOF
            Select Case intChapter
                Case intChapter = 0
                    Me.tvCookbooks.Nodes.Add _
                        Relative:="CB" & rsRecipes!cookbookid, _
                        Relationship:=tvwChild, _
                        key:="R" & rsRecipes!recipeid, _
                        Text:=rsRecipes!recipename
                
                Case intChapter <> 0
                     Me.tvCookbooks.Nodes.Add _
                        Relative:="CH" & rsRecipes!cookbookchapterid, _
                        Relationship:=tvwChild, _
                        key:="R" & rsRecipes!recipeid, _
                        Text:=rsRecipes!recipename
            End Select
           rsRecipes.MoveNext
       Loop
 
What about changing:

Case intChapter = 0
to
Case 0
 
I'd also change the second section to

Case Else

An integer is either equal to 0 or it isn't, there's no need to test once you know it isn't.
 
If you have 2 just mutually exclusive options, then why not use an IF instead of a SELECT? Further if the code inside those options is virtually the same except for 1 character, then just set that 1 character to a variable and put it inside 1 IF statement:

Code:
...
CBCH="CH"
IF intChapter = 0 Then CBCH="CB"

Me.tvCookbooks.Nodes.Add _
  Relative:=CBCH & rsRecipes!cookbookid, _
  Relationship:=tvwChild, _
  key:="R" & rsRecipes!recipeid, _
  Text:=rsRecipes!recipename

...
 
Because I intend to add some more cases. I'd just like for these to work first.
 
Use Case 0 and Case Else then when you have more cases, add them in above Case Else or eliminate Case Else altogether.
 
Well I shouldn't rejoice quite yet. All landed under the cookbooks instead of the chapters but at least they landed. Back to my joy. Thank you so much.
 
I DO NOT BELIEVE IT! IT WORKS! Why didn't mine work?
Because the beginning of the construct is case + expression. by the time you're beyond that line, the only thing that needs to be tested is the value, you can't test another full expression. It is different than SQL Server Case, where you can structure it either way, you can start out with just the word Case and then use full expressions in the middle, or you can start out with a case plus an expression and then test values in the middle.

in Access you can only do it one way
 

Users who are viewing this thread

Back
Top Bottom