Array out of bounds error Visual basic forms - vb.net

Hi im new to this but my code keeps giving me the same error after ive entered three countries and their details. Once i click Ok on my third data entry i get an out of bounds error message.
here is my code if anyone can help
Dim countries(2) As employee
Dim row As Integer
For row = 0 To 2
countries(row).CountryName = InputBox("row" & row & " Enter name of country")
countries(row).population = InputBox("row" & row & " Enter population")
countries(row).Capital = InputBox("row" & row & " enter capital city")
countries(row).GDP = InputBox("row" & row & " Enter GDP value ")
countries(row).worldRanking = InputBox("row" & row & " GDP world rank")
countries(row).Democracy = InputBox("row" & row & " Democracy based? (1 = yes 2 = no")
Next row
For row = 0 To 4
If countries(row).worldRanking < 100 Then
ListBox1.Items.Add(countries(row).CountryName)
End If
Next row

You've defined a 3-element array here:
Dim countries(2) As employee
(2) = 0 to 2 = three elements.
So countries(row) where row > 2 isn't defined.
Arrays aren't always the best choice for collections. Have a look at IEnumerable, List, etc which have more flexibility (IMHO).

You could use GetUpperBound() in your loops, instead of hard-coding a number:
For row = 0 To countries.GetUpperBound(0)

Related

Compare Employee Names To Master List

I am comparing a list of employees to a master list, and getting a count. This works as it should, but now I need to add a caveat to ignore certain employee names (like Manager, Supervisor, Mgr, Sup) from the counts.
The list received has location in column A and the names in column C - so I would need to ignore the names in the array above that would exist in Column C from the counts.
...
This is what I am using to return the count -> how can it be modified to ignore the array of names listed above?
Const shLookup As String = "Master List"
Const shSetup As String = "Received"
With Worksheets(shLookup)
aData = .Range("a1").CurrentRegion
End With
ReDim aOutput(1 To UBound(aData), 1 To 3)
For i = 2 To UBound(aData)
iCount = Evaluate("=COUNTIF('" & shSetup & "'!A:A,'" & shLookup & "'!A" & i & ")")
If aData(i, 2) <> iCount Then
n = n + 1
aOutput(n, 1) = aData(i, 1)
aOutput(n, 2) = aData(i, 2)
aOutput(n, 3) = iCount
End If
Next
EDIT
Sample worksheet data would look like this:
Master List sheet
Store EmpCount
Memphis 23
Houston 13
Phili 10
Received List Sheet
Store Region EmpName
Memphis East Joe
Memphis East James
Memphis East Jane
Memphis East Supervisor
Memphis East Manager
What I am after is to ensure that on the received list sheet there are 23 employees for Memphis, excluding the names above from the count.
EDIT 2
Per the suggestion in comments by #PKatona - it looks like I can use the CountIF() function to count the "ignore" values then subtract that count from my whole count. This syntax seems to work for one by one, however, how would I adapt it for an array?
I.E. this would give me the count for Manager but there are other buzz words I need to search for:
iIgnore = Evaluate("=COUNTIF(Range('" & shSetup & "'!A:A,'" & shLookup & "'!A" & i & "),"Manager")")
Off the top of my head without syntax checking, here's how you'd create the array of ignore values and then count them:
dim ignore() as String
ignore = Split("Manager,Supervisor,Mgr,Sup", ",")
dim ignoreCnt as integer
ignoreCnt = 0
dim aIdx as integer
for aIdx = 0 to UBound(ignore) - 1
ignoreCnt = ignoreCnt + Evaluate("=COUNTIF(Range('" & shSetup & _
"'!A:A,'" & shLookup & "'!A" & i & "),ignore(aIdx))")
next aIdx
ignoreCnt now has the total of the values you want to ignore. If you have more, just add them to the string in the Split line, the UBound function automatically gets the upper limit for the loop.

VBA ACCESS Comparing String as they are integer

I am trying to prompt the user to input a range and display all the instruments that are within that range in a subform.
Problem: The upper and lower range is a text field (because some of the range cannot be expressed in integer). As seen in the screenshot, the comparison only compare the first character of the field.
User's input: 5 - 3
On the subform: 36 - 4
It compares 5 and 3 instead of 36
I know vba is doing what it has been told but how can I achieve the result I want?
Here is my code for requering the subform:
Dim Up As Integer
Dim Low As Integer
If Me.Text_L = "" Or IsNull(Me.Text_L) Or Me.Text_U = "" Or IsNull(Me.Text_U) Then
MsgBox ("Please choose a valid range!")
Else
Up = Me.Text_U
Low = Me.Text_L
SQL = SQL_Origin & " WHERE [qry_View_Search].[Upper_Range] <= '" & Up & "' " _
& "AND [qry_View_Search].[Lower_Range] >= '" & Low & "';"
subform_View_Search.Form.RecordSource = SQL
subform_View_Search.Form.Requery
End If
so what i did is made a new column in the query for
IIf(IsNumeric([Upper]), Val([Upper]), Null)
to get all the numeric result.
Then in the vba, I re query the subform as below
SQL = SQL_Origin & " WHERE [qry_View_Search].[Upper] <= cint(Forms![frm_View_Search]![Text_U]) " _
& "AND [qry_View_Search].[Lower] >= cint(Forms![frm_View_Search]![Text_L]);"
Thanks #HansUp !
I have successfully for those cases used Val only:
Value: Val([FieldName])
or:
Value: Val(Nz([FieldName]))

How to tell what row a db table is reading from

Is there a way to determine the row a set of data has been collected from in a DataTable?
I currently have this code
expression = "date = '" & numericYear & "-" & doubleMonth & "-20'"
foundRows = dbTable.Select(expression)
For count = 0 To foundRows.Length - 1
tableRow =
MessageBox.Show(dbTable.Rows(count).Item(5).ToString())
Next count
The issue is the messagebox shows row 0 then 1 then 2 etc. How can i get it to show the row being read from the Data Table? For example if the event being read is on row 52 of the data table how would i tell that?
you can use the indexof method:
foundRows = dbTable.Select(expression)
For count = 0 To foundRows.Length - 1
rowIndex = dbTable.Rows.IndexOf(foundRows(count))
MessageBox.Show("Row " & cstr(rowIndex) & " " & foundRows(count).Item(5).ToString())
Next count

Double For Loop in VB.NET

Dim ssi(11) As String
For i = 0 To 10
If ssi(i) = "" Then ssi(i) = "0"
For j = 0 To Val(ssi(11)) + i
ssi(i) = xuh(Val(ssi(i)))
Next
Next
If ssi(11) = "2" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10)))
ElseIf ssi(11) = "3" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10))) * (-1)
End If
I am new here and new to VB as well.
I am trying to understand this double loop in vb code.
ssi(i) is defined as a String variable. and each element is assigned to a specific number in a String. Hope I told it clearly.
My problem with this loop is below.
Since i ranges from 0 to 10, what does this j mean? Does j mean the new ssi(1-10) or another whatever number?
I think the best way to answer your question about understanding a double loop is to try looking at something simpler.
The first program I always write in each new version of BASIC that comes along is a 12 times table.
I've modified it a bit below to be a 12 x 10 table for the purpose of illustrating for you how a double loop works ... hope it helps:
For x As Integer = 1 To 12
For y As Integer = 1 To 10
Console.Write(x * y)
Console.Write(vbTab)
Next
Console.WriteLine()
Next

Append text to existing row in datatable

I'm trying to make a calendar in vb.net and I have come across this problem. I want to append some text into an existing datatable row. When I watch my debugger it says:"In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.".
Dim aantalRijen As Integer = 1
For x = 0 To 6
Dim dttopdrachten As New DataTable
dttopdrachten = opdrachtendao.getOpdrachtenByDate(Today.AddDays(x))
If dttopdrachten.Rows.Count > aantalRijen Then
aantalRijen = dttopdrachten.Rows.Count
End If
Next
For z = 0 To aantalRijen - 1
Dim r As DataRow
r = dttAgenda.NewRow()
dttAgenda.Rows.InsertAt(r, z)
Next
For i = 0 To 6
Dim aantalItems As Integer = 0
Dim dttopdrachten As New DataTable
dttopdrachten = opdrachtendao.getOpdrachtenByDate(Today.AddDays(i))
aantalItems = dttopdrachten.Rows.Count
For j = 0 To aantalItems - 1
Dim info As String = dttopdrachten.Rows(j).Item(0).ToString & vbCrLf & dttopdrachten.Rows(j).Item(2).ToString & vbCrLf & dttopdrachten.Rows(j).Item(3).ToString & vbCrLf & dttopdrachten.Rows(j).Item(4).ToString & vbCrLf & dttopdrachten.Rows(j).Item(5).ToString & vbCrLf & dttopdrachten.Rows(j).Item(6).ToString
dttAgenda.Rows(j).Item(i) = info
Next
Next
dgvAgenda.DataSource = dttAgenda
In the code above, I first count how many rows I have to make. Afterwards I add the amount of rows to the datatable (columns are added before). Until here it works, but then when I keep debugging I get the error. I tried googling but nothing could help me so far.
Seem problem has been solved without changing anything. So if someone want to make a calendar. Here's the solution ;)