Using checkboxes to create an array - arrays

A background to why im trying to do this.
I am creating a program for work, this is one of many way of doing this process, where users select test that they have carried out, around 50 possible test using a userform that I've made. From the selection it runs through each userform based off which checkboxes were ticked, without the need to choose another test.
i.e. if they select 1,2,5 then the program will eventually load UserForm1 -> UserForm2 -> NOT UserForm3 -> NOT UserForm4 -> UseeForm5
The idea is that the user selects the options they want then based off the options selected create an array of either 0 if they didn't select the option or (1,2,3,4...) if they did, this number depends upon which checkbox was selected. i.e CheckBox1 = 1 CheckBox2 = 2 etc.
From this array I think i'll be able to select the correct userform using the .find feature in excel with a for loop.
But I have an issue when I run my code below,
Sub List_Create()
Dim tests(5) As Integer
If CheckBox1.Value = True Then
tests(0) = 1
Else: tests(0) = 0
End If
If CheckBox2.Value = True Then
tests(1) = 2
Else: tests(1) = 0
End If
If CheckBox3.Value = True Then
tests(2) = 3
Else: tests(2)= 0
End If
If CheckBox4.Value = True Then
tests(3) = 4
Else: tests(3) = 0
End If
If CheckBox5.Value = True Then
tests(4) = 5
Else: tests(4) = 0
End If
End Sub
I get a
runtime 424 error object required.
Which when debugged is on the line If CheckBox1.Value = True Then
Where have i gone wrong with my code? Is this possible, im not sure if it is?

You need to explicitly reference the userform to evaluate the checkbox value.
If userformX.CheckBox1.Value = True Then

Related

if the same item in all four comboboxes are chosen

a newb practising more vb.net code.
I currently have four identical comboboxes named box1, box2, box3, box4.
in each of those comboboxes I have a list of words-
dog
cat
bird
fish
I've added these words through the 'properties'->'items'-->'collections' tab.
For now, I want to display a message- "you have 4 cats!" If the user selects the word 'cat' for all four comboboxes.
How would I write this code? I'm guessing I introduce a private function like 'onlycats' that sees if 'cat' is chosen for all 4 boxes... but I'm not to sure how to code based on what the user has chosen from the combobox.
So you want to count the selection of a ComboBox and present the results as a MsgBox after the User pressed a Button. You can do it like this:
Dim Selection(4) As String
Selection(0) = box1.Text
Selection(1) = box2.Text
Selection(2) = box3.Text
Selection(3) = box4.Text
MsgBox("You selected " & Selection.Where(Function(value) value = "dog").Count & " dogs!")
First of all I added everything to a Array and then search for a specific value and count it. You can count it like this:
Selection.Where(Function(value) value = "value").Count
The output is a Number. If "dog" is selected in all 4 boxes then the output is just 4 and you can write a Text around it. Hope it brings you on the right track.
============
If you just want to trigger something when 4 cats are selected then the count function also helps you. Just make a If Function
If Selection.Where(Function(value) value = "cat").Count = 4 Then
MsgBox("you have 4 cats!")
End If
Untested code but something like this:
sub CheckSame
if box2.SelectedValue = box1.SelectedValue
andalso box3.SelectedValue = box1.SelectedValue
andalso box4.SelectedValue = box1.selectedValue then
message.text = "You have 4 " & plural(box1.SelectedValue) & "!"
else
message.text = "You don't have 4 of the same animal"
end if
end sub
function plural(s as String) as String
select case s
case "fish"
return "fish"
case else
return s & "s"
end select
end function

jQuery: Each Loop Oddity

I want to go through all fields of a form and determine if the fields are populated with data. When all fields are given, I'd like a button save to appear.
This is the Fiddle with code, that works (!) ...
This is the same in JavaScript.
The jQuery-Object to iterate through:
formElements = $ 'form input[type="text"], form input[type="number"], form textarea'
The Function:
formFilled = ->
filled = true
formElements.each ->
if $(this).val().length is 0
filled = false
filled
The Event-Handler:
formElements.on 'keyup', (keyup) ->
keyup.preventDefault()
if formFilled()
save.show()
else
save.hide()
Any suggestions?
# I know the answer:
JavaScript validates the number-input fields and accepts only values that are numbers.
I stupidly tested those fields with text input ... silly.
The code is fine. Just the testing was bad. Sorry for wasting your time.
This works:
formInputs = $ 'input[type="text"], input[type="number"], textarea'
formFilled = ->
filled = true
formInputs.each ->
if $(this).val().length is 0
filled = false
filled
formInputs.on 'keyup', (keyup) ->
if formFilled()
saveButton.show()
else
saveButton.hide()

Using Variables as module/array names

My ultimate goal is to load a comboBox with elements from different arrays (vba coded). I have 8 different arrays and 6 option buttons. The first 2 optionButtons are grouped to eliminate 4 arrays which leaves the last 4 grouped optionButtons to determine the actual input for the combo box.
It works like this:
'first grouped option buttons
maleOptionButton
femaleOptionButton
'second grouped option buttons
basketballOptionButton
footballOptionButton
soccerOptionButton
hockeyOptionButton
The array's as you can guess are filled with student names that play the sports. So when the user clicks the first grouped buttons of Male/Female the click even does nothing.. however when they click the second group of option buttons of sports, it calls the same sub procedure within a module that has IF's and Else Ifs to determine the combination of buttons that were selected.
sub maleInitArray()
'declaration section:
Public maleSoccerArray(1 to 6) as string
'sub section
dim mike as student 'calls the class student
set mike = new student
with mike
.name = "Michael"
.age = 14
.so on and so on = something
end with
maleSoccerArray(1) = mike.name
End Sub
What I'm trying to do is this:
dim i as integer
dim l as integer
dim gender as string
dim sport as string
if inputForm.soccerOptionButton.value = true then
if inputForm.maleOptionButton.value = true then
gender = "male"
sport = "maleSoccerArray"
call male.maleInitArray ' inits the array thats hard coded.
else
gender = "female"
sport = "femaleSoccerArray"
call female.femaleInitArray
Else If...
' the list goes on to assign variables depending on the combo boxes.
' doesn't work, but it beats using this every time
l = UBound(sport) ' Doesn't recognize "sport" as an Array
for i = 1 to l
' .AddItem(gender.sport(i)) will not work as well.
inputForm.studentComboBox.AddItem(gender.sport(i))
next i
Seems as though UBound(variable) and AddItem(Variable.Variable)
will not work...
I found a few things so far, but none of them have worked.. such as the application.run method and assigning the actual "male.maleSoccerArray" method.
Any help would be greatly appreciated.. thanks

get_by_id() not returning values

I am writing an application that shows the user a number of elements, where he has to select a few of them to process. When he does so, the application queries the DB for the rest of the data on these elements, and stacks them with their full data on the next page.
I made an HTML form loop with a checkbox next to each element, and then in Python I check for this checkbox's value to get the data.
Even when I'm just trying to query the data, ndb doesn't return anything.
pitemkeys are the ids for the elements to be queried. inpochecks is the checkbox variable.
preqitems is the dict to save the items after getting the data.
The next page queries nothing and is blank.
The comments are my original intended code, which produced lots of errors because of not querying anything.
request_code = self.request.get_all('rcode')
pitemkeys = self.request.get_all('pitemkey')
inpochecks = self.request.get_all('inpo')
preqitems = {}
#idx = 0
#for ix, pitemkey in enumerate(pitemkeys):
# if inpochecks[ix] == 'on':
# preqitems[idx] = Preqitems.get_by_id(pitemkey)
# preqitems[idx].rcode = request_code[ix]
# idx += 1
for ix, pitemkey in enumerate(pitemkeys):
preqitems[ix] = Preqitems.get_by_id(pitemkey)
#preqitems[ix].rcode = request_code[ix]
Update: When trying
preqitems = ndb.get_multi([ndb.Key(Preqitems, k) for k in pitemkeys])
preqitems returns a list full of None values, as if the db couldn't find data for these keys.. I checked the keys and for some reason they are in unicode format, could that be the reason? They look like so.
[u'T-SQ-00301-0002-0001', u'U-T-MT-00334-0007-0002', u'U-T-MT-00334-0008-0001']
Probably you need to do: int(pitemkey) or str(pitemkey), depending if you are using integer or string id

how to save multiple checkbox values in one while or for loop in vb.net

i have 50 checkboxes for 50 american states. The user can choose all 50 or only 1( so basically any number he wants). Based on his choice, I want to insert or update the table in sql server 2008. e.g-
Color = blue and chk1=check, chk2= check and chk3 = check (chk = checkbox). now the user wants to ad 10 more states to this or remove these 3 and add 5 more. so u basically get the idea. the table in database looks like this - ID Color State_id there is a table called states, so stateid shall come from there. so how do i do a loop insert or update in vb.net?
I would use a data source and a checkboxlist. You already have your states in a datatable, populate a checkboxlist with a databound selection from a SqlDataSource (or datatable of your own choosing). Then when you click your button just iterate through the following loop:
Dim dt as New myTypedDataTable ' constructed from datasource
Dim color as String = "Blue" ' Filled however you set color
For Each item As ListItem In Me.CheckBoxList1.Items
If item.Selected Then
Dim row as myTypedDataTableRow = dt.NewmyTypedDataTableRow
row.Color = color
row.State_id = item.Value
dt.Rows.Add(row)
End If
Next
Once you've got the datatable filled with the rows in question you could either use the SqlDataSource to perform an insert operation or perform the insert operation atomically. There are several ways to accomplish it, but this is probably the simplest way to iterate through the items given the data structure you described.
In this context, I sometime go the easy way and delete all the user items store in the database and then only do insert.
This can be a problem if you have for example a insert_date. In this case, you'll need to list of user selected options. Loop in the new list, if the item is not found in the old list then it's an insert. Loop in the old list, if the item is not found in the new list then it's a delete.
I would use bitwise operation and a long variable in .net (mixed with an enum for the flag)
one field in the db and way easier to play with what the user select
small sample
Enum state As Long '64 enum maxium since long = 64 bits
ALABAMA = 1
ALASKA = 2
NEVADA = 4
ARIZONA = 8
ARKANSAS = 16
CALIFORNIA = 32
COLORADO = 64
CONNECTICUT = 128
DELAWARE = 256
'etc etc etc
End Enum
Module Module1
Sub Main()
Dim userselect As state = 0
Console.WriteLine("your checked box state")
Console.WriteLine("in this case im using the order of the enum for selecting")
Dim checkbox = New Boolean() {True, False, False, True, False, False, True, False, False}
For i = 0 To checkbox.Length - 1
userselect = CType(userselect + If(checkbox(i), (2 ^ (i + 1)), 0), state)
Next
For Each s As state In [Enum].GetValues(GetType(state))
If (userselect And s) > 0 Then
Console.WriteLine("selected " & s.ToString)
End If
Next
Console.WriteLine("Value of userselect is " & userselect.ToString)
Console.ReadKey()
End Sub
End Module
OUTPUT:
selected NEVADA
selected ARIZONA
selected COLORADO
Value of userselect is 76

Resources