+ Post New Thread
Results 1 to 4 of 4
Coding Thread, VB6 Help in Coding and Web Development; I have to code something in VB6 (don't ask why VB6 but I just do ). Here is the code. ...
  1. #1

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241

    VB6 Help

    I have to code something in VB6 (don't ask why VB6 but I just do ). Here is the code. Can somebody help me diagnose the problems I am getting?

    Code:
    Private Sub Form_Activate()
        ' Connect to the database and open users table
        Set db = DBEngine.OpenDatabase(App.Path & "\db\mon_bud.mdb")
        Set rs = db.OpenRecordset("tbl_users", dbOpenDynaset)
        
        ' Check to see if users exist in db
        On Error GoTo err_hand1
        rs.MoveFirst
        lblUser1.Caption = rs!Name & "'s Income"
        
        On Error GoTo err_hand2
        rs.MoveNext
        lblUser2.Caption = rs!Name & "'s Income"
        
        Set rs = Nothing
        Exit Sub
        
    err_hand1:
        If Err.Number <> 0 Then
            lblUser1.Caption = "Please define users"
            lblUser2.Caption = "Please define users"
            Set rs = Nothing
            Exit Sub
        End If
            
    err_hand2:
        If Err.Number <> 0 Then
            lblUser2.Caption = "User 2 not in use"
            Set rs = Nothing
            Exit Sub
        End If
            
    End Sub
    I have two labels. I want to work it so if no users exist in the db the labels show Please define users. This works when no users are in db. I also want it where if two user exist it will show the name of those users in either label - this works when two users are in the db.

    Finally, I want it so that when only one user is in the db it will show this name in the first label and change the second label to User 2 not in use. This is the part that doesn't work. It doesn't come up with an error or anything, but instead the whole program (including VB6) crashes and closes when only one user is in the db.

    Any help VB6 guru's?

  2. #2

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    I don't have VB6 installed at the moment but if you're just checking for 0,1 or 2 users (and there will never be more) then I think you want to just look at the count of records and use a case statement based on that.

    Try something like this:
    Code:
    Private Sub Form_Activate()
        ' Connect to the database and open users table
        Set db = DBEngine.OpenDatabase(App.Path & "\db\mon_bud.mdb")
        Set rs = db.OpenRecordset("tbl_users", dbOpenDynaset)
    
        iCount=rs.recordcount
        select case iCount
           case 0
            lblUser1.Caption = "Please define users"
            lblUser2.Caption = "Please define users"
    
           case 1
            lblUser1.Caption = rs!Name & "'s Income"
            lblUser2.Caption = "User 2 not in use"
    
           case 2
            lblUser1.Caption = rs!Name & "'s Income"
            rs.movenext
            lblUser1.Caption = rs!Name & "'s Income"
        end select
        
        Set rs = Nothing
        Exit Sub
    End Sub

  3. Thanks to srochford from:

    Hightower (20th November 2009)

  4. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Thanks for that. I've used to code and thought it was going to work but it iCount either equals 0 if no records exist or 1 if 1 or more.

    I have two records and it still returns 1. 3 records and it still returns 1.

  5. #4

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Scratch that:

    Code:
    rs.movelast
    iCount = rs.recordcount
    rs.movefirst
    That code above sorted it.

SHARE:
+ Post New Thread

Similar Threads

  1. Adding components in VB6
    By iking in forum How do you do....it?
    Replies: 1
    Last Post: 15th July 2009, 10:37 AM
  2. More VB6 Help
    By Hightower in forum Coding
    Replies: 4
    Last Post: 27th April 2009, 01:39 PM
  3. VB6 Properties
    By Hightower in forum Coding
    Replies: 7
    Last Post: 27th April 2009, 10:54 AM
  4. VB6 Help
    By Hightower in forum Coding
    Replies: 8
    Last Post: 18th April 2009, 02:30 PM
  5. Vb6 components on CC3
    By iking in forum Network and Classroom Management
    Replies: 2
    Last Post: 26th October 2008, 05:39 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •