PDA

Click to See Complete Forum and Search --> : Why won't this work?


gvirden
Dec 29th, 1999, 09:14 PM
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.

Clunietp
Dec 29th, 1999, 09:18 PM
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

Gerald
Dec 29th, 1999, 10:22 PM
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).]

gvirden
Dec 30th, 1999, 01:45 AM
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!

Clunietp
Dec 30th, 1999, 06:27 AM
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!!!!!!........