If array is empty error - vb.net

The array is a 9 position array declared as a string, I have 9 text boxes that a user can input data into, each writes to one variable in the array.
I am trying to stop filling the array, and move to print it when the user either fills in all 9 text box's, or stops filling them in when he presses the "write to file" button. From my debugging points, it looks like I get down to the "for" loop, but the program crashes with no errors that I can make heads or tails out of (not syntax or variable)... can anyone see what i'm missing?
Thanks
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\Users\foo\test2.txt"
Dim i As Integer
Dim j As Integer
Dim aryText(9) As String
MessageBox.Show(j)
aryText(0) = "[" & TextBox1.Text & "]"
j = 0
MessageBox.Show(j)
If String.IsNullOrWhiteSpace(TextBox2.Text) Then
End
Else
aryText(1) = "*" & TextBox2.Text & "{label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
'If TextBox3.Text IsNot Nothing Then
If String.IsNullOrWhiteSpace(TextBox3.Text) Then
End
Else
aryText(2) = TextBox3.Text & " {label: " & "varchar, null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox4.Text) Then
End
Else
aryText(3) = TextBox4.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox5.Text) Then
End
Else
aryText(4) = TextBox5.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox6.Text) Then
End
Else
aryText(5) = TextBox6.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox7.Text) Then
End
Else
aryText(6) = TextBox7.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox8.Text) Then
End
Else
aryText(7) = TextBox8.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox9.Text) Then
End
Else
aryText(8) = TextBox9.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
MessageBox.Show(j)
For i = 0 To j
objWriter.WriteLine(aryText(j))
i = i + 1
Next
objWriter.Close()
MessageBox.Show("Text written to file")
End Sub

Please don't debug with MessageBoxes. Visual Studio has a lovely debugger.
VB.net arrays are declared Dim myArray(UpperBound) As String. So with your 9 text boxes you have an upper bound of 8, indexes 0 to 8
You want Exit Sub not End.
A For x = 0 to y...Next works by automatically incrementing x (that is what the Next means, Next x) the default is by one but you can change that by adding a Step. If you attempt to change y you will make a mess. Your For...Next will just write the last value in the array 9 times (aryText(j)) because j doesn't change. Don't increment x in the loop; it increments automatically when Next is called.
No need to mess with i and j; just use a For Each
Using code block for your stream writer ensures all resources are released.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FILE_NAME As String = "C:\Users\foo\test2.txt"
Dim aryText(8) As String
Dim msg As String = "Please fill in all the boxes,"
If String.IsNullOrWhiteSpace(TextBox1.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(0) = "[" & TextBox1.Text & "]"
End If
If String.IsNullOrWhiteSpace(TextBox2.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(1) = "*" & TextBox2.Text & "{label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox3.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(2) = TextBox3.Text & " {label: " & "varchar, null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox4.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(3) = TextBox4.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox5.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(4) = TextBox5.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox6.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(5) = TextBox6.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox7.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(6) = TextBox7.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox8.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(7) = TextBox8.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox9.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(8) = TextBox9.Text & " {label: " & "varchar, not null" & "}"
End If
Using objWriter As New System.IO.StreamWriter(FILE_NAME, True)
For Each s As String In aryText
objWriter.WriteLine(s)
Next
End Using
MessageBox.Show("Text written to file")
End Sub

Related

MS Access if statement on click event

I am using Ms Access forms and I have created an on click event that locates a folder location but now I want to locate the folder location based on different criteria but when I add the if statement it expects a sub,function or property. Below is some demo code. I really hope someone can explain what is missing?
Private Sub Open_Email_Click()
Dim stAppName As String
Dim stAppNameA As String
Dim stAppNameB As String
stAppName = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\B " & Me.BC & " " & Me.UC & "\"
stAppNameA = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\A\B " & Me.BC & " " & Me.UC & "\"
stAppNameB = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\B\B " & Me.BC & " " & Me.UC & "\"
If (Me.BC = "60") And Me.UC Like "REF123*" Then stAppNameA
ElseIf (Me.BC = "60") And Not Me.UC Like "REF123*" Then stAppNameB
Else: stAppName
End If
Call Shell(stAppName, 1)
End Sub
I think the logic of your function could be reduced to the following, which may be more readable with fewer repeating expressions:
Private Sub Open_Email_Click()
Dim strTmp As String
If Me.BC = "60" Then
If Me.UC Like "REF123*" Then
strTmp = " DEMO\A\B "
Else
strTmp = " DEMO\B\B "
End If
Else
strTmp = " DEMO\B "
End If
Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)
End Sub
Alternatively, using a Select Case statement:
Private Sub Open_Email_Click()
Dim strTmp As String
Select Case True
Case Me.BC <> "60"
strTmp = " DEMO\B "
Case Me.UC Like "REF123*"
strTmp = " DEMO\A\B "
Case Else
strTmp = " DEMO\B\B "
End Select
Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)
End Sub
To test the resulting path, change:
Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)
To:
Debug.Print "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\"
I think your If block is just a bit messy in terms of where you have newlines, and continuation characters (:). Try reformatting your code like this:
If (Me.BC = "60") And Me.UC Like "REF123*" Then
stAppName =stAppNameA
ElseIf (Me.BC = "60") And Not Me.UC Like "REF123*" Then
stAppName = stAppNameB
Else
stAppName =stAppName
End If
Call Shell(stAppName, 1)

Invalid qualifier Error Message in vba code

This code is designed to detect the columns of start and finish of a shape which is used and displayed onto the caption of the shape itself. The following code is the problematic code:
Sub Take_Baseline()
Dim forcast_weeksStart() As String
Dim forcast_weeksEnd() As String
Dim forcastDate As String
Dim shp As Shape
Dim split_text() As String
'cycle through all the shapes in the worsheet and enter the forcast date for all the projects into their respective boxes
For Each shp In ActiveSheet.Shapes
'initialize forcast date by parsing
forcast_weeksStart = Split(shp.TopLeftCell.Column.Text, " ")
forcast_weeksEnd = Split(shp.BottomRightCell.Column.Text, " ")
forcastDate = forcast_weeksStart(1) & "-" & forcast_weeksEnd(1)
temp = shp.OLEFormat.Object.Object.Caption
If InStr(temp, "/-/") > 0 & InStr(temp, "In Prog") Then
split_text = Split(shp.OLEFormat.Object.Caption, " ")
For i = 0 To (i = 3)
shp.TextFrame.Characters.Caption = split_text(i) & vbNewLine
Next i
ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption & vbNewLine & ActiveSheet.Cells(4, AShape.TopLeftCell.Column).Text & " - " & ActiveSheet.Cells(4, AShape.BottomRightCell.Column).Text & vbNewLine & "dates: " & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & "/" & "actualDate"
' ElseIf InStr(temp, "/-/") > 0 & InStr(temp, "In Prog") = 0 Then
'split_text = Split(shp.OLEFormat.Object.Object.Caption, " ")
' For i = 0 To (i = 2)
' shp.OLEFormat.Object.Caption = split_text(i) & vbNewLine
' Next i
'ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption & vbNewLine & "In Prog" & vbNewLine & ActiveSheet.Cells(4, AShape.TopLeftCell.Column).Text & " - " & ActiveSheet.Cells(4, AShape.BottomRightCell.Column).Text & vbNewLine & "dates: " & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & "actualDate"
End If
Next shp
'For testing purposes
Sheet4.Range("A20").Value = forcast_weeksStart(1)
Sheet4.Range("A21").Value = forcast_weeksEnd(1) End Sub
The error is an
"invalid qualifier"
message which occurs on line
forcast_weeksStart = Split(shp.TopLeftCell.Column.Text, " ")
Right on the "column" word. I don't get why this is happening since the actual drop down menu has the column operation which i can select. I have tried everything from changing it to the OLEformat.Object.Caption etc etc. But nothing has worked. I am still relatively new to vba so any help will be appreciated. Thanks

If Statement to change field.control source

I have a small If statement that that changes the controlsource of a field if another field is empty. The " just before = is incorrect, and I'm not sure what to use to ensure the entire string starting with the = is included.
Private Sub Report_Load()
If IsNull(FirstName2) Then
OwnersNames.ControlSource = "=FirstName1] & " " & [LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1] & " " & [LastName1] & " and " & [FirstName2] & " " & [LastName2]"
End If
End Sub
If I'm reading this correctly, then try this which uses more quote marks to concatenate the statement:
Private Sub Report_Load()
If IsNull(FirstName2) Then
OwnersNames.ControlSource = "=[FirstName1]" & " " & "[LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1]" & " " & "[LastName1]" & " and " & "[FirstName2]" & " " & "[LastName2]"
End If
End Sub
If you are trying to concatenate the fields (e.g. 'add' them together). You need something like:
Private Sub Report_Load()
If IsNull(FirstName2) Then
OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1] & ' ' & [FirstName2] & ' ' & [LastName2]"
End If
End Sub
Keep in mind you can concatenate text fields (e.g. text boxes) together.
I'm uncertain if combining the control sources of fields works the same way. To be sure, one option is to rename each textbox to something different from its control source.
Here's the code now, which when loaded produces 'John Smith And' even though the Firstname2 field is null.
Private Sub Report_Load()
If Me.FirstName2 = vbNullString Then
OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1] & ' and ' & [FirstName2] & ' ' & [LastName2]"
End If
End Sub
Are you really meaning to set the control source or just the value of the field?
I think this might be what you want to accomplish????
Private Sub Report_Load()
If Me.FirstName2 = "" Then
me.OwnersNames = [FirstName1] & " " & [LastName1]
Else
me.OwnersNames = [FirstName1] & " " & [LastName1] & _
" and " & [FirstName2] & " " & [LastName2]
End If
End Sub

VB.Net Correct code doesn't work?

I do not get any syntax errors in the code below, but when I compile the code I get the Msgbox("cant post status") instead of the output from main.p(x).status(0). The x and 0 can be any number and I always get the cant post status box. What bugs me most is that I have a debug form with a richtextbox that I load all the data from all seven occurrences of the array: p into when the program starts, and it works just fine. I've included that code as well at the very bottom. Before I run the logging sub, I run an initialization sub that puts a default value into every variable. When I don't have the try/catch in the main_loop, I do not get an error, but all execution stops. My computer doesn't freeze, but actions that should take place after that sub do not. Does anyone know why I can't make a call to main.p(x).status(0) inside this sub?
'Main Class'
Public p(6) as structs.player
Public Shared Sub main_loop()
For x As Integer = 0 To (Main.p.Count - 1) Step 1
If Main.check_act(x) = False Then
MsgBox("past check act")
If Main.p(x).pos >= 1 And Main.p(x).pos <= 3 Then
MsgBox("past pos; pre death")
Try
MsgBox(Main.p(x).status(0))
Catch ex As Exception
MsgBox("cant post status")
End
End Try
End If
End If
Next
End Sub
'Structs Class'
Public Structure player
Dim name As String
Dim type As String
Dim pos As Integer
Dim wait As Integer
Dim mhp As Integer
Dim chp As Integer
Dim mmp As Integer
Dim cmp As Integer
Dim map As Integer
Dim cap As Integer
Dim atk As Integer
Dim def As Integer
Dim mak As Integer
Dim mdf As Integer
Dim spd As Integer
Dim acc As Integer
Dim eva As Integer
Dim crt As Integer
Dim status() As Integer
Dim stats() As Integer
Dim statr() As Integer
Dim elems() As Integer
Dim elemr() As Integer
Dim abl() As Boolean
End Structure
'Debug Class'
Public Shared Sub log(p As player)
'Stats'
Debug.log.Text += ">>> " & p.name.ToString & " <<<" & Chr(10)
Debug.log.Text += "Type: " & p.type.ToString & Chr(10)
Debug.log.Text += "Pos: " & p.pos.ToString & Chr(10)
Debug.log.Text += "Wait: " & p.wait.ToString & Chr(10) & Chr(10)
Debug.log.Text += "HP: " & p.mhp.ToString & _
"/" & p.chp.ToString & Chr(10)
Debug.log.Text += "MP: " & p.mmp.ToString & _
"/" & p.cmp.ToString & Chr(10)
Debug.log.Text += "AP: " & p.map.ToString & _
"/" & p.cap.ToString & Chr(10) & Chr(10)
Debug.log.Text += "ATK: " & p.atk.ToString & Chr(10)
Debug.log.Text += "DEF: " & p.def.ToString & Chr(10)
Debug.log.Text += "MAK: " & p.mak.ToString & Chr(10)
Debug.log.Text += "MDF: " & p.mdf.ToString & Chr(10)
Debug.log.Text += "SPD: " & p.spd.ToString & Chr(10)
Debug.log.Text += "ACC: " & p.acc.ToString & Chr(10)
Debug.log.Text += "EVA: " & p.eva.ToString & Chr(10)
Debug.log.Text += "CRT: " & p.crt.ToString & Chr(10) & Chr(10)
'Status And Elements'
For x As Integer = 0 To (p.status.Count - 1) Step 1
Debug.log.Text += p.status(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
For x As Integer = 0 To (p.stats.Count - 1) Step 1
Debug.log.Text += p.stats(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
For x As Integer = 0 To (p.statr.Count - 1) Step 1
Debug.log.Text += p.statr(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
For x As Integer = 0 To (p.elems.Count - 1) Step 1
Debug.log.Text += p.elems(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
For x As Integer = 0 To (p.elemr.Count - 1) Step 1
Debug.log.Text += p.elemr(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
'Abilities'
For x As Integer = 0 To (p.abl.Count - 1) Step 1
Debug.log.Text += p.abl(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
End Sub
I quickly debugged the code an determined that status(0) is the issue.
The following screen image shows the error ...

Checking if index is null and providing an error?

I have the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' calculate button
If String.IsNullOrEmpty(TextBox1.Text) Then
MsgBox("Please enter a value to convert!")
End If
If currentIndex = vbNull Then
MsgBox("Please select a conversion!")
End If
Select Case currentIndex
Case 1
Label2.Text = TextBox1.Text & " Celsius = " & Math.Round(((TextBox1.Text * 1.8) + 32), 2) & " Fahrenheit"
Case 2
Label2.Text = TextBox1.Text & " Fahrenheit = " & Math.Round(((TextBox1.Text - 32) / 1.8), 2) & " Celsius"
Case 3
Label2.Text = TextBox1.Text & " Kelvin = " & Math.Round((TextBox1.Text - 273.15), 2) & " Celsius"
Case 4
Label2.Text = TextBox1.Text & " Celius = " & Math.Round((TextBox1.Text + 273.15), 2) & " Kelvin"
Case 5
Label2.Text = TextBox1.Text & " Kelvin = " & Math.Round((((TextBox1.Text - 273.15) * 1.8) + 32), 2) & " Fahrenheit"
Case 6
Label2.Text = TextBox1.Text & " Fahrenheit = " & Math.Round(((((TextBox1.Text - 32) * 5) / 9) + 273.15), 2) & " Kelvin"
Case 8
Label2.Text = TextBox1.Text & " Miles P/H = " & Math.Round((TextBox1.Text * 1.609344), 2) & " Kilometers P/H"
Case 9
Label2.Text = TextBox1.Text & " Kilometers P/H = " & Math.Round((TextBox1.Text / 1.609344), 2) & " Miles P/H"
Case 11
Label2.Text = TextBox1.Text & " Kilograms = " & Math.Round((TextBox1.Text * 2.20462), 2) & " Pounds"
Case 12
Label2.Text = TextBox1.Text & " Pounds = " & Math.Round((TextBox1.Text / 2.20462), 2) & " Kilograms"
Case 14
Label2.Text = TextBox1.Text & " Meters = " & Math.Round((TextBox1.Text * 3.2808399), 2) & " Feet"
Case 15
Label2.Text = TextBox1.Text & " Feet = " & Math.Round((TextBox1.Text / 3.2808399), 2) & " Meters"
End Select
End Sub
As you can see, I have a variable (currentIndex) and have a select case statement checking them against the various conversions, however my problem lies in the piece of code above this.
If currentIndex = vbNull Then
MsgBox("Please select a conversion!")
End If
I require it to spit an error message out if the index is null, however I cannot work out a way to do this. 0 cannot be used as this is the first entry in the index, and vbNull etc do not seem to work. Can anybody point me in the right direction? Thanks.
EDIT:
This is how current index is created:
Dim currentIndex As Integer
and this is how it is assigned:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
' Get the currently selected index of the item in the list box.
currentIndex = ListBox1.FindString(currentItem)
End Sub
I would suggest using SelectedIndex instead.
In your code:
If ListBox1.SelectedIndex = -1 Then
MsgBox("Please select a conversion!")
End If
Here is the reference for the property: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindex.aspx
If currentIndex is an index from dropdown box then value of -1 means that nothing was selected.