Results 1 to 5 of 5

Thread: Why won't this work?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 1999
    Location
    Greenville, MS, Washington
    Posts
    38

    Post


    I got this code from MartinLiss and it works great! This code writes to a field called reason all the reasons(strings) that are checked from mutiple checkboxes after a _click() occurs.


    For Each MyCheckBoxes In Controls
    If TypeOf MyCheckBoxes Is CheckBox Then
    If MyCheckBoxes.Value = vbChecked Then
    strReasons = strReasons & MyCheckBoxes.Caption
    End If
    End If
    Next

    My problem is this. The event is cmdfeed_click(). Txtfeed is the amount of feed fed and typed in by the user. Well, if they don't type in anything into txtfeed, and click cmdfeed then I want this to happen.

    If txtfeed = "" And MyCheckBoxes.Value = 0 Then
    MsgBox ("Go back and check a reason for no feed."), , "Check a Reason"
    Exit Sub
    Else: txtfeed = "" And MyCheckBoxes.Value = 1
    txtfeed = 0
    End If

    What I am trying to do is this. If the user enters txtfeed = " " and no checkboxes have been checked, then I want that msgbox to make them go back and check a box (a reason). If they have a box checked and they enter txtfeed = " " I want to write 0 in the field. I get a runtime error 91.

    Thanks again.

  2. #2
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844

    Post

    Code:
    if trim(txtfeed.text) = "" then
        if MyCheck.Value = vbChecked then
            txtfeed.text = "0"
        else
            msgbox "Blah Blah Blah"
        end if
    else
        msgbox "You have a value!"
    end if

  3. #3
    Member
    Join Date
    Nov 1999
    Posts
    63

    Post

    I'm guessing the error is occuring on the 'If txtfeed = "" And MyCheckBoxes.Value = 0 Then' . This is happening because you are using a control "array" for your checkboxes. Therefore using 'MyCheckBoxes.Value' (without a subscript) would generate a runtime error. Perhaps you could use the following line of code instead.

    If len(txtFeed) = 0 And len(strReasons) = 0 Then

    Gerald

    UPDATE: After looking up the runtime error number you mentioned it looks as though there may be some other problem in your code. Are the two blocks of code you show located in two seperate subroutines? Where are you initializing the variable 'MyCheckboxes' in the second block of code?


    [This message has been edited by Gerald (edited 12-30-1999).]

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 1999
    Location
    Greenville, MS, Washington
    Posts
    38

    Post

    Bingo Gerald you called it dead on!

    If len(txtFeed) = 0 And len(strReasons) = 0

    I guess I really didn't understand what was going on behind Martin's code.

    Thanks

    By the way Tom, I've been out this morning and I really hadn't had a chance to play with your code (which I always like to do), but for heavens sake... I know I'm slow with VB, but you responded with posted code to my question in 4 minutes flat!

  5. #5
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844

    Post

    I was sitting here, waiting, for you..... all morning......i couldn't wait.....i knew you would post something.....those voices told me so......aaarrrrggggghhhhhhh the voices!!!!!!........

Posting Permissions

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



Click Here to Expand Forum to Full Width