Solved Select case error (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 07:14
Joined
Oct 14, 2019
Messages
427
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
 

Isaac

Lifelong Learner
Local time
Today, 07:14
Joined
Mar 14, 2017
Messages
8,738
What about changing:

Case intChapter = 0
to
Case 0
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:14
Joined
Aug 30, 2003
Messages
36,118
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.
 

plog

Banishment Pending
Local time
Today, 09:14
Joined
May 11, 2011
Messages
11,613
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

...
 

ClaraBarton

Registered User.
Local time
Today, 07:14
Joined
Oct 14, 2019
Messages
427
Because I intend to add some more cases. I'd just like for these to work first.
 

June7

AWF VIP
Local time
Today, 06:14
Joined
Mar 9, 2014
Messages
5,425
Use Case 0 and Case Else then when you have more cases, add them in above Case Else or eliminate Case Else altogether.
 

ClaraBarton

Registered User.
Local time
Today, 07:14
Joined
Oct 14, 2019
Messages
427
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.
 

Isaac

Lifelong Learner
Local time
Today, 07:14
Joined
Mar 14, 2017
Messages
8,738
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

Top Bottom