Issue with blank entry in array - vba

I have an array that can either have 6 or 7 values, the code below shows how I can have 6 or 7 values:
Dim AB(1 To 7)
AB(1) = "1-P"
AB(2) = "1-L"
AB(3) = "1-E"
AB(4) = "1-I"
AB(5) = "1-O"
AB(6) = "1-T"
If TeachingTool.teacherInPlYes = True Then
AB(7) = "1-R"
End If
Once the array has been populated I search the current sheet for the calues in the array and populate a listbox with there cell positions, however because there is a blank space in the array it pulls me back alot of unwanted data for some reason and I can't figure out why. I have tried use Dim AB(1 To 6 or 7) but that didn't work. So some information on this issue would be helpful, thanks

Use ReDim. That way you can dimension the array accordingly. Example:
If TeachingTool.teacherInPlYes = True Then
ReDim AB(1 To 7)
AB(7) = "1-R"
Else
ReDim AB(1 to 6)
End If
AB(1) = "1-P"
AB(2) = "1-L"
AB(3) = "1-E"
AB(4) = "1-I"
AB(5) = "1-O"
AB(6) = "1-T"

Related

How do I collapse white space in a list of string that was randomized

I have a list of strings that hold 10 answers.
Each question has a different amount of answers 2-10.
After randomizing the list, I end up with white space or empty spaces in my list depending on the number of answers.
After randomizing the list of let's say 2 answers, i would like to shift them back to position 0 and 1 in my list keeping the size of the list at 10 and of course keeping the order randomized.
I'm not sure how to programmatically solve this problem...
I've tried to sort/reverse the list after randomize, but of course, this removes the randomization.
I've tried to remove the white space with something like
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
but I then get an out of bounds when trying to write them back to my radiobuttons.text as there are 10 of them.
This is where writing my list of...
RadioAnswer1.Text = answerlist(0)
RadioAnswer2.Text = answerlist(1)
RadioAnswer3.Text = answerlist(2)
RadioAnswer4.Text = answerlist(3)
RadioAnswer5.Text = answerlist(4)
RadioAnswer6.Text = answerlist(5)
RadioAnswer7.Text = answerlist(6)
RadioAnswer8.Text = answerlist(7)
RadioAnswer9.Text = answerlist(8)
RadioAnswer10.Text = answerlist(9)
Ideally, I want the list randomized then however many answers there are written back into the list starting at 0 going down to 10.
I hope my question is clear.
Additional info edit
So here is how I'm loading the answers into the List Of..
Dim answerlist As New List(Of String)
WEFESQLConn.ConnectionString = connectstring
WEFESQLConn.Open()
WERESQLStatment.CommandText = "SELECT * FROM [WEFE Questions] WHERE QuestionID = " & SQLQuestionNum.ToString
WERESQLStatment.Connection = WEFESQLConn
WEFESQLRead = WERESQLStatment.ExecuteReader
If WEFESQLRead.HasRows Then
WEFESQLRead.Read()
lblQuestion.Text = WEFESQLRead.Item("Question").ToString
answerlist.Add(WEFESQLRead.Item("CorrectAnswer").ToString)
answerlist.Add(WEFESQLRead.Item("Answer2").ToString)
answerlist.Add(WEFESQLRead.Item("Answer3").ToString)
answerlist.Add(WEFESQLRead.Item("Answer4").ToString)
answerlist.Add(WEFESQLRead.Item("Answer5").ToString)
answerlist.Add(WEFESQLRead.Item("Answer6").ToString)
answerlist.Add(WEFESQLRead.Item("Answer7").ToString)
answerlist.Add(WEFESQLRead.Item("Answer8").ToString)
answerlist.Add(WEFESQLRead.Item("Answer9").ToString)
answerlist.Add(WEFESQLRead.Item("Answer10").ToString)
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
WEFESQLRead.Close()
WEFESQLConn.Close()
RadioAnswer1.Text = answerlist(0)
RadioAnswer2.Text = answerlist(1)
RadioAnswer3.Text = answerlist(2)
RadioAnswer4.Text = answerlist(3)
RadioAnswer5.Text = answerlist(4)
RadioAnswer6.Text = answerlist(5)
RadioAnswer7.Text = answerlist(6)
RadioAnswer8.Text = answerlist(7)
RadioAnswer9.Text = answerlist(8)
RadioAnswer10.Text = answerlist(9)
With this code I get the out of bounds as there are not enough answers to populate the answerlist.
Without
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
I get the spaces in my pre-drawn radio buttons.
I am all ready hiding the unused buttons - the issue there are 10 positions for the buttons and with the randomization of the list.
4 spots of 10 used image
Why don't you toggle the visibility of the controls dynamically after removing the blank entries from the list? Take a look at this example:
'Store all controls in a collection
Dim answers(9) As RadioButton = {RadioAnswer1, RadioAnswer2, RadioAnswer3, RadioAnswer4, RadioAnswer5, RadioAnswer6, RadioAnswer7, RadioAnswer8, RadioAnswer9, RadioAnswer10}
'Iterate through all answers
For index As Integer = 0 To answerlist.Count - 1
'Show the control and set the text
With answers(index)
.Text = answerlist.Item(index)
.Visible = True
End With
Next
'Loop through the rest of the answer controls
For index As Integer = answerlist.Count To answers.Length - 1
'Hide the control
answers(index).Visible = False
Next
Initial setup...
Dim radioButtons As New List(Of RadioButton)
radioButtons.Add(RadioAnswer1)
radioButtons.Add(RadioAnswer2)
...
radioButtons.Add(RadioAnswer10)
After removing blank answers from the randomized list...
For i = 0 To answerList.Count - 1
radioButtons(i).Text = answerList(i)
radioButtons(i).Visible = True
Next
For i = answerList.Count to radioButtons.Count - 1
radioButtons(i).Visible = False
Next
To close this out I ended up going with removing all the spaces from my ListOF then using a DO WHILE for each radiobutton.text entries.
example portion
answerlist = RandomizeListOrder(answerlist)
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
Dim ALCount As Integer = answerlist.Count
Dim ALCounter = 0
Do
If ALCounter < ALCount Then
ALCounter += 1
RadioAnswer1.Text = answerlist(0)
ElseIf ALCounter = ALCount Then
Exit Do
End If
Maybe not very clean as there are 10 of these entries, but I'll work on that later. Just trying to get the idea out.
Thanks everyone for the suggestions they got me on the right path.

Do-While loop (VBA) not looping

so I thought this would be a simple logical problem, but for the life of me I cannot find the issue with this code block. I have checked around on Stack for a solution, but all other do/while loop problems appear to be primarily with other languages.
What I am trying to do is simply loop through an array & add a new worksheet for each element in the array that is not null. Pretty simple right? Yet for some reason it simply loops through once and thats it.
Here is the code block:
Dim repNames() As String
Dim x As Integer
x = 25
ReDim repNames(1 To x)
repNames(1) = "Ahern"
repNames(2) = "Castronovo"
repNames(3) = "Glick"
repNames(4) = "Fields"
repNames(5) = "Murphy"
repNames(6) = "Sleeter"
repNames(7) = "Vivian"
repNames(8) = "Walschot"
repNames(9) = "Wilson"
Dim i As Integer
i = 1
Do 'Loop keeps creating only 1 new sheet. Should create 9.
Worksheets.Add.Name = repNames(i)
i = i + 2
Loop While repNames(i) <> Null
I believe the problem is with this line: Loop While repNames(i) <> Null,
but obviously the logical test seems to hold up.
Any help would be hugely appreciated!
As others note, Null is not the comparison you want to make. Testing anything for equivalence with Null will return Null -- even ?Null = Null returns Null, which is why your loop is exiting early. (Note: To test for a Null, you need to use the IsNull function which returns a boolean, but that is NOT how you test for an empty string.)
In VBA, to test for a zero-length string or empty string, you can use either "" or vbNullString constant, or some people use the Len function to check for zero-length.
Rectifying that error, as originally written in your code, your logical test should abort the loop if any item is an empty string, but none of the items are empty strings (at least not in the example data you've provided) so you end up with an infinite loop which will error once i exceeds the number of items in the repNames array.
This would be probably better suited as a For Each loop.
Dim rep as Variant
For Each rep in repNames
Worksheets.Add.Name = rep
Next
If you need to skip empty values, or duplicate values, you can add that logic as needed within the loop:
For Each rep in repNames
If rep <> vbNullString 'only process non-zero-length strings
Worksheets.Add.name = rep
End If
Next
Etc.
Firstly, you should be comparing to vbNullString. This loops multiple times:
' Declare variables
Dim repNames() As String
Dim x As Integer
Dim i As Integer
' Set data
x = 25
ReDim repNames(1 To x)
repNames(1) = "Ahern"
repNames(2) = "Castronovo"
repNames(3) = "Glick"
repNames(4) = "Fields"
repNames(5) = "Murphy"
repNames(6) = "Sleeter"
repNames(7) = "Vivian"
repNames(8) = "Walschot"
repNames(9) = "Wilson"
' Loop through items
i = 1
Do
Worksheets.Add.Name = repNames(i)
i = i + 2
Loop While repNames(i) <> vbNullString
There is one more problem – why i = i + 2 ? In your question you say you wanted the loop to execute 9 times, but using i = i + 2 skips every other item. If you indeed want to loop through every item:
Do
Worksheets.Add.Name = repNames(i)
i = i + 1
Loop While repNames(i) <> vbNullString
Here you go, I have changed the loop conditional, and changed i=i+2 to i=i+1. A regular while loop would be better than a do while encase the first element is empty
Dim repNames()
Dim x As Integer
x = 25
ReDim repNames(1 To x)
repNames(1) = "Ahern"
repNames(2) = "Castronovo"
repNames(3) = "Glick"
repNames(4) = "Fields"
repNames(5) = "Murphy"
repNames(6) = "Sleeter"
repNames(7) = "Vivian"
repNames(8) = "Walschot"
repNames(9) = "Wilson"
Dim i As Integer
i = 1
Do While repNames(i) <> ""
Worksheets.Add.Name = repNames(i)
i = i + 1
Loop

Submitchanges after a loop - only last record saved

The following code works, but it only saves the last record in the loop, and I can't figure out why. I think the Submitchanges() is in the right place, at the end of the loop. Can someone please show me what's wrong? Thanks.
Sub POPULATE_CHAIN()
Dim newChain As New CHAIN
Dim dpSTRIKE As Integer
'get list of Options Contracts
Dim lstOPT = From Z In DATA.OPTIONs, X In DATA.UDLies
Where X.UDLY_SYM = Z.UDLY_SYM
Select Z.CONTRACT, Z.STRIKE_GAP, X.UDLY_LAST
Dim dctOPT = lstOPT.ToDictionary(Function(Z) Z.CONTRACT)
For Each key In dctOPT.Keys
For COUNT = 1 To 5
dpSTRIKE = 1850 + 5 * COUNT
Dim lkup = From Z In DATA.CHAINs
Select Z
Dim RCD_EXISTS As Boolean = lkup.Any(Function(Z) Z.CONTRACT = dctOPT(key).CONTRACT And Z.P_C = "C" And Z.STRIKE = dpSTRIKE)
If RCD_EXISTS = False Then
newChain.CONTRACT = dctOPT(key).CONTRACT
newChain.P_C = "C"
newChain.STRIKE = dpSTRIKE
DATA.CHAINs.InsertOnSubmit(newChain)
Else
newChain.CONTRACT = dctOPT(key).CONTRACT
newChain.P_C = "C"
newChain.STRIKE = dpSTRIKE
End If
Next
Next
DATA.SubmitChanges()
End Sub
Dim newChain As New CHAIN
should be inside Fore Each, exactly inside second for.
Since it is declared outside the loop, it will be detached from table and attached again to table. So it will be inserted only in last row.

When loading from string to checked listview, the foreach skips characters

I have a string, My.settings.prohibitions, full of 1's and 0's; they correspond to which items of a checked listview should be checked and which should not. I tried using the following code to check items on the listview according to the values in the string, but it didn't work; the even numbered items were always unchecked, but the odd numbered items did get checked correctly.
For Each setting As Char In My.Settings.Prohibitions
If setting = "0" Then
ListView1.Items(My.Settings.Prohibitions.IndexOf(setting)).Checked = False
Else
ListView1.Items(My.Settings.Prohibitions.IndexOf(setting)).Checked = True
End If
Next
Edit: This does not work either:
Dim x As String = My.Settings.Prohibitions
For y As Integer = 0 To 7
If x(y).ToString = "1" Then
ListView1.Items(y).Checked = True
Else
ListView1.Items(y).Checked = False
End If
y += 1
Next
You need something like this:
For index As Integer = 0 To My.Settings.Prohibitions.Length - 1
ListView1.Items(index).Checked = My.Settings.Prohibitions(index) = "1"
Next

VB.NET DataGridView Not Showing Data

I have looked around quite a bit, and nothing seems to answer my question in particular. I have DataGridView in which I add rows and columns dependent on a certain record count of geospatial data that is held in ArcGIS. The data is then passed through to the gridview for editing.
So I do not use any databinding methods, rather it is a very manual process. I store data into a two-dimensional arrays, which indexes are based off of those certain data record counts.
After the loops are complete, the data should be stored in each cell dependent on index. The very last row that was populated is populated perfectly, yet the other 72 (or so) rows are blank.
Here is the code as follows:
This populates column headers (works great):
For i_FieldNum = 0 To pFields_1.FieldCount - 1
str_FieldName = UCase(pFields_1.Field(i_FieldNum).Name)
str_FieldAliasName = pFields_1.Field(i_FieldNum).AliasName
ReDim Preserve m_array_str_FieldNames(i_FieldNum)
ReDim Preserve m_array_str_FieldAliasNames(i_FieldNum)
m_array_str_FieldNames(i_FieldNum) = str_FieldName
m_array_str_FieldAliasNames(i_FieldNum) = str_FieldAliasName
i_FieldCount = i_FieldCount + 1
i_ColNum = i_FieldCount - 1
'If this is the "SHAPE", "FID", or "OBJECTID" field, remember it:
If (str_FieldName = "SHAPE") Then
m_i_FieldNum_Shape = i_FieldCount - 1
ElseIf (str_FieldName = "FID") Then
m_i_FieldNum_FID = i_FieldCount - 1
ElseIf (str_FieldName = "OBJECTID") Then
m_i_FieldNum_OBJECTID = i_FieldCount - 1
End If
'Store the field name in the top row:
grid_Data.ColumnCount = i_FieldCount
grid_Data.Columns.Item(i_ColNum).HeaderText = str_FieldName
ReDim Preserve m_array_l_FieldColLengths(i_ColNum)
m_array_l_FieldColLengths(i_ColNum) = pFields_1.Field(i_FieldNum).Length
Next i_FieldNum
This populates the two-dimensional array:
Do Until pFeat_1 Is Nothing
'Until we run out of features ...
i_FeatCount = i_FeatCount + 1
For i_FieldNum = 0 To pFields_1.FieldCount - 1
'If the field is recorded as a shape
If (i_FieldNum = m_i_FieldNum_Shape) Then
array_Data(i_FeatCount - 1, i_FieldNum) = "<Shape>"
Else
'If the value of the field is null
If IsNothing(pFeat_1.Value(i_FieldNum)) Or pFeat_1.Value(i_FieldNum).ToString = "" Then
array_Data(i_FeatCount - 1, i_FieldNum) = "<Null>"
Else
array_Data(i_FeatCount - 1, i_FieldNum) = pFeat_1.Value(i_FieldNum).ToString
End If
End If
Next i_FieldNum
'End If
'Move to the next feature (if any).
pFeat_1 = pFeatCursor_1.NextFeature
Loop
This is suppose to display the data:
For i_RowNum = 0 To i_FeatCount - 1
'Add a new row to the grid:
grid_Data.RowCount = i_RowNum + 2
For i_ColNum = 0 To pFields_1.FieldCount - 1
grid_Data.Item(i_ColNum, i_RowNum + 1).Value = array_Data(i_RowNum, i_ColNum)
Next i_ColNum
Next i_RowNum
Everything runs and compiles perfectly, although as I stated before, only the last row's data is shown (which is populated properly).
If anyone can help figure out why only one row is populated, it would be more than appreciated.
Thank You,
Logan
Check your updated section -This is suppose to display the data: If you data is correctly set then as per my guess it making grid to show wrong data. Modify it as per your columns in your grid that you have added. Or you can create column names string array to put data in particular cells. as dgvr.Cells(columnsArray[index]).Value = array[,]
For i_RowNum = 0 To i_FeatCount - 1
'Add a new row to the grid:
DataGridViewRow dgvr = new System.Windows.Forms.DataGridViewRow();
dgvr.CreateCells(grid_Data)
For i_ColNum = 0 To pFields_1.FieldCount - 1
dgvr.Cells(i_ColNum).Value = array_Data(i_RowNum, i_ColNum)
Next i_ColNum
grid_Data.Rows.Add(dgvr)
Next i_RowNum
Ref: Adding a New Row to DataGridView and How to: Manipulate
Rows in the Windows Forms DataGridView Control
See here, each time you increment the row count its clearing out the data:
DataGridView1.RowCount += 1
DataGridView1.Item(0, 0).Value = "a value"
DataGridView1.RowCount += 1
DataGridView1.Item(0, 1).Value = "the previous value has been wiped"
To fix your code without any major changes, just use this method to add rows:
grid_Data.Rows.Add(paramArray)
or even easier put this line above the for loop and set the number of rows before you fill them in:
grid_Data.RowCount = i_FeatCount - 1