Conversion Date in VB.net with exact parse? - vb.net

I have an issue converting a DateTime with d/M/yyyy HH:mm:ss.
I have discovered after a long time that my script problem came from the conversion into date?
Could you help me with conversion why it doesn't give the format require
Function Calculating_billing_time(strStartDate As String, intDuration As Integer, Optional blnOVTDisabled As Boolean = False) As (intTSNormal As Integer, intTSOVT1 As Integer, intTSOVT1N As Integer, intTSOVT2 As Integer, intTSOVT2N As Integer, intTSOVT3 As Integer, intTSOVT3N As Integer)
Dim intTSNormal As Integer = 0
Dim intTSOVT1 As Integer = 0
Dim intTSOVT1N As Integer = 0
Dim intTSOVT2 As Integer = 0
Dim intTSOVT2N As Integer = 0
Dim intTSOVT3 As Integer = 0
Dim intTSOVT3N As Integer = 0
Dim strDateBorderAm As String
Dim dtDateBorderAM As Date
'Dim strDateBorder_6AM As String
'Dim dtDateBorder_6AM As Date
'Dim strDateDayAfterBorder_6AM As String
'Dim dtDateDayAfterBorder_6AM As Date
Dim strDateBorderPM As String
Dim dtDateBorderPM As Date
Dim strDateBorderNT As String
Dim dtDateBorderNT As Date
Dim strDateBorderMD As String
Dim dtDateBorderMD As Date
Dim strDateDayAfterBorderAM As String
Dim dtDateDayAfterBorderAM As Date
Dim dateformat2 As String
Dim dateFormat As String
' transformation of the string to actual date system
Dim dtStartDate As Date
Dim dtEndDate As Date
dateFormat = "M/d/yyyy HH:mm:ss"
dateformat2 = "d/M/yyy HH:mm:ss"
dtStartDate = Date.ParseExact(strStartDate, dateFormat, Globalization.CultureInfo.InvariantCulture)
dtStartDate = dtStartDate.AddSeconds(-(dtStartDate.TimeOfDay.Seconds))
dtEndDate = dtStartDate.AddSeconds(intDuration)
Console.WriteLine("Date start: " & dtStartDate)
Console.WriteLine("Date end : " & dtEndDate)
If blnOVTDisabled = True Then
intTSNormal = intDuration
Else
'strDateBorder_6AM = dtStartDate.Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 06:00:00"
'dtDateBorder_6AM = Date.ParseExact(strDateBorder_6AM, dateFormat, Globalization.CultureInfo.InvariantCulture)
strDateBorderAm = dtStartDate.Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 07:00:00"
dtDateBorderAM = Date.ParseExact(strDateBorderAm, dateformat2, Globalization.CultureInfo.InvariantCulture)
strDateBorderPM = dtStartDate.Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 18:00:00"
dtDateBorderPM = Date.ParseExact(strDateBorderPM, dateformat2, Globalization.CultureInfo.InvariantCulture)
strDateBorderNT = dtStartDate.Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 22:00:00"
dtDateBorderNT = Date.ParseExact(strDateBorderNT, dateformat2, Globalization.CultureInfo.InvariantCulture)
strDateBorderMD = dtStartDate.AddDays(1).Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 00:00:00"
dtDateBorderMD = Date.ParseExact(strDateBorderMD, dateformat2, Globalization.CultureInfo.InvariantCulture)
strDateDayAfterBorderAM = dtStartDate.AddDays(1).Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & "07:00:00"
dtDateDayAfterBorderAM = Date.ParseExact(strDateBorderAm, dateformat2, Globalization.CultureInfo.InvariantCulture)
'strDateDayAfterBorder_6AM = dtStartDate.AddDays(1).Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 06:00:00"
'dtDateDayAfterBorder_6AM = Date.ParseExact(strDateBorderAm, dateFormat, Globalization.CultureInfo.InvariantCulture)
Console.WriteLine("")
'Console.WriteLine("Date Border AM: " & dtDateBorder_6AM)
Console.WriteLine("Date Border AM: " & dtDateBorderAM)
Console.WriteLine("Date Border PM:" & dtDateBorderPM)
Console.WriteLine("Date Border Night:" & dtDateBorderNT)
Console.WriteLine("Date Border Midnight:" & dtDateBorderMD)
'Console.WriteLine("Date Day After Border 6 AM:" & dtDateDayAfterBorder_6AM)
Console.WriteLine("Date Day After Border AM:" & dtDateDayAfterBorderAM)
Console.WriteLine("")
' Start before 7h00 (AM)
If (dtStartDate <= dtDateBorderAM) Then
Console.WriteLine("Start before 7h00 (AM) for:" & dtStartDate.DayOfWeek)
If dtStartDate.DayOfWeek.ToString = "Sunday" Then
intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderAM).ToString)
Else
intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderAM).ToString)
End If
Console.WriteLine("TSNormal " & intTSNormal)
Console.WriteLine("TSOVT1 " & intTSOVT1)
Console.WriteLine("TSOVT1N " & intTSOVT1N)
Console.WriteLine("TSOVT2 " & intTSOVT2)
Console.WriteLine("TSOVT2N " & intTSOVT2N)
Console.WriteLine("TSOVT3 " & intTSOVT3)
Console.WriteLine("TSOVT3N " & intTSOVT3N)
Console.WriteLine("")
Console.WriteLine("start date " & dtStartDate)
Console.WriteLine("end date " & dtEndDate)
End If
'Start After 7h00 (AM)
If dtStartDate >= dtDateBorderAM Then
Console.WriteLine("Start After 7h00 (AM) for " & dtStartDate.DayOfWeek)
If dtStartDate.DayOfWeek.ToString = "Saturday" Then
intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderPM).ToString)
ElseIf dtStartDate.DayOfWeek.ToString = "Sunday" Then
intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderPM).ToString)
Else
intTSNormal += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderPM).ToString)
Console.WriteLine("TSNormal " & intTSNormal)
Console.WriteLine("TSOVT1 " & intTSOVT1)
Console.WriteLine("TSOVT1N " & intTSOVT1N)
Console.WriteLine("TSOVT2 " & intTSOVT2)
Console.WriteLine("TSOVT2N " & intTSOVT2N)
Console.WriteLine("TSOVT3 " & intTSOVT3)
Console.WriteLine("TSOVT3N " & intTSOVT3N)
Console.WriteLine("")
Console.WriteLine("start date " & dtStartDate)
Console.WriteLine("end date " & dtEndDate)
End If
End If
' Stop before 18h
If dtEndDate <= dtDateBorderPM Then
Console.WriteLine("Stop before 18h for" & dtEndDate.DayOfWeek)
If dtEndDate.DayOfWeek.ToString = "Saturday" Then
intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtEndDate, dtDateBorderPM).ToString)
ElseIf dtEndDate.DayOfWeek.ToString = "Sunday" Then
intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtEndDate, dtDateBorderPM).ToString)
Else
intTSNormal += CInt(DateDiff(DateInterval.Second, dtEndDate, dtDateBorderPM))
Console.WriteLine("TSNormal " & intTSNormal)
Console.WriteLine("TSOVT1 " & intTSOVT1)
Console.WriteLine("TSOVT1N " & intTSOVT1N)
Console.WriteLine("TSOVT2 " & intTSOVT2)
Console.WriteLine("TSOVT2N " & intTSOVT2N)
Console.WriteLine("TSOVT3 " & intTSOVT3)
Console.WriteLine("TSOVT3N " & intTSOVT3N)
Console.WriteLine("")
Console.WriteLine("start date " & dtStartDate)
Console.WriteLine("end date " & dtEndDate)
End If
End If
' Stop After 18h and Before 22h
If (dtEndDate > dtDateBorderPM) And (dtEndDate <= dtDateBorderNT) Then
Console.WriteLine("Stop After 18h and Before 22h for " & dtEndDate.DayOfWeek)
If (dtEndDate.DayOfWeek.ToString = "Saturday") Then
intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtEndDate).ToString)
ElseIf dtEndDate.DayOfWeek.ToString = "Sunday" Then
intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtEndDate).ToString)
Else
intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtEndDate).ToString)
Console.WriteLine("TSNormal " & intTSNormal)
Console.WriteLine("TSOVT1 " & intTSOVT1)
Console.WriteLine("TSOVT1N " & intTSOVT1N)
Console.WriteLine("TSOVT2 " & intTSOVT2)
Console.WriteLine("TSOVT2N " & intTSOVT2N)
Console.WriteLine("TSOVT3 " & intTSOVT3)
Console.WriteLine("TSOVT3N " & intTSOVT3N)
Console.WriteLine("")
Console.WriteLine("start date " & dtStartDate)
Console.WriteLine("end date " & dtEndDate)
End If
End If
'Stop After 22h and Before 07h00 (AM) Day After
If dtEndDate >= dtDateBorderNT And dtEndDate <= dtDateDayAfterBorderAM Then
Console.WriteLine("Stop After 22h and Before 07h00 (AM) day After for" & (dtEndDate.DayOfWeek))
If dtEndDate.DayOfWeek.ToString = "Saturday" Then
intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtDateBorderNT).ToString)
intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtEndDate).ToString)
ElseIf dtEndDate.DayOfWeek.ToString = "Sunday" Then
intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtDateBorderNT).ToString)
intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtEndDate).ToString)
Else
intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateBorderPM).ToString)
intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtEndDate, dtDateBorderNT).ToString)
End If
Console.WriteLine("TSNormal " & intTSNormal)
Console.WriteLine("TSOVT1 " & intTSOVT1)
Console.WriteLine("TSOVT1N " & intTSOVT1N)
Console.WriteLine("TSOVT2 " & intTSOVT2)
Console.WriteLine("TSOVT2N " & intTSOVT2N)
Console.WriteLine("TSOVT3 " & intTSOVT3)
Console.WriteLine("TSOVT3N " & intTSOVT3N)
Console.WriteLine("")
Console.WriteLine("start date " & dtStartDate)
Console.WriteLine("end date " & dtEndDate)
End If
'Stop After 07h00 (AM) Day After
If dtEndDate > dtDateBorderNT And dtEndDate > dtDateDayAfterBorderAM Then
Console.WriteLine("Stop After 07h00 (AM) Day After for " & dtEndDate.DayOfWeek)
If dtEndDate.DayOfWeek.ToString = "Saturday" Then
intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateDayAfterBorderAM).ToString)
intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateDayAfterBorderAM, dtEndDate).ToString)
ElseIf dtEndDate.DayOfWeek.ToString = "Sunday" Then
intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateBorderMD).ToString)
intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtDateBorderMD, dtDateDayAfterBorderAM).ToString)
intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtDateDayAfterBorderAM, dtEndDate).ToString)
ElseIf dtEndDate.DayOfWeek.ToString = "Monday" Then
intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtDateBorderNT).ToString)
intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateBorderMD).ToString)
intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtDateBorderMD, dtDateDayAfterBorderAM).ToString)
intTSNormal += CInt(DateDiff(DateInterval.Second, dtDateDayAfterBorderAM, dtEndDate).ToString)
Else
intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtDateBorderNT).ToString)
intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateDayAfterBorderAM).ToString)
intTSNormal += CInt(DateDiff(DateInterval.Second, dtDateDayAfterBorderAM, dtEndDate).ToString)
End If
Console.WriteLine("")
Console.WriteLine("TSNormal:" & intTSNormal)
Console.WriteLine("intTSOVT1:" & intTSOVT1)
Console.WriteLine("intTSOVT1N:" & intTSOVT1N)
Console.WriteLine("TSOVT2:" & intTSOVT2)
Console.WriteLine("TSOVT2N:" & intTSOVT2N)
Console.WriteLine("intTSOVT3N:" & intTSOVT3N)
Console.WriteLine("")
End If
Return (intTSNormal, intTSOVT1, intTSOVT1N, intTSOVT2, intTSOVT2N, intTSOVT3, intTSOVT3N)
End If 'le generale
End Function
End Module
Do you Believe, it impact the rest of the script?
Could you tell me how to convert it?

A better way to work with dates
Dim strDateBorderAm As DateTime = dtStartDate.Date.AddHours(7)
Dim strDateBorderPM As DateTime = dtStartDate.Date.AddHours(18)
' etc

I don't get any error.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim strStartDate = "9/7/2021 07:00:00"
Dim DateFormat = "M/d/yyyy HH:mm:ss"
Dim dtStartDate = Date.ParseExact(strStartDate, DateFormat, Globalization.CultureInfo.InvariantCulture)
Debug.Print(dtStartDate.ToString) 'Immediate window shows 9/7/2021 7:00:00 AM
End Sub

Related

Additional information: Unable to cast object of type 'System.Int32' to type 'System.String'. - VB.NET

I have this error,
Unable to cast object of type 'System.Int32' to type 'System.String'.
The code was working, but after rebuilding, the code no longer works
My Code
Private Sub KodePembelian()
Dim Rd As SqlClient.SqlDataReader
Dim Cmd As SqlClient.SqlCommand
Dim bulan As String
bulan = Format(Now, "MM")
Dim tahun As String
tahun = Format(Now, "yy")
Dim Urutan As String
Dim Hitung, Cari As String
Cmd = New SqlClient.SqlCommand("SELECT * FROM BY_PEMBELIANHEADER WHERE NOPEMBELIAN IN " & "(SELECT MAX (NOPEMBELIAN) FROM BY_PEMBELIANHEADER)", MyConn)
Rd = Cmd.ExecuteReader
Rd.Read()
If Not Rd.HasRows Then
Urutan = "PO/" & bulan & tahun & "/" & "000001"
Else
Cari = Microsoft.VisualBasic.Right(Rd.GetString(0), 6)
If Microsoft.VisualBasic.Left(Rd.GetString(0), 8) <> "PO/" & bulan & tahun & "/" Then
Urutan = "PO/" & bulan & tahun & "/" & "000001"
Else
Hitung = Microsoft.VisualBasic.Right(Rd.GetString(0), 6) + 1
Urutan = "PO/" & bulan & tahun & "/" & Microsoft.VisualBasic.Right("000000" & Hitung, 6)
End If
End If
Rd.Close()
txtpo.SelectedText = Urutan
End Sub
probably the return value is null. you have to call IsDBNull to check for null values before calling GetString method.
this snippet should fix the problem.
Rd = Cmd.ExecuteReader
Rd.Read()
If Not Rd.HasRows Then
Urutan = "PO/" & bulan & tahun & "/" & "000001"
Else
If Rd.IsDBNull(0) Then
Cari = "default" ' do something if its null
Else
Cari = Microsoft.VisualBasic.Right(Rd.GetSqlValue(0).ToString(), 6)
End If
Note : No conversions are performed on GetString method; therefore, the data retrieved must already be a string.
sqldatareader.getstring documentation
Solved thank you mr alireza, i just add getsqlvalue(0).tostring in my code
Private Sub KodePembelian()
Dim Rd As SqlClient.SqlDataReader
Dim Cmd As SqlClient.SqlCommand
Dim bulan As String
bulan = Format(Now, "MM")
Dim tahun As String
tahun = Format(Now, "yy")
Dim Urutan As String
Dim Hitung, Cari As String
Cmd = New SqlClient.SqlCommand("SELECT * FROM BY_PEMBELIANHEADER WHERE NOPEMBELIAN IN " & "(SELECT MAX (NOPEMBELIAN) FROM BY_PEMBELIANHEADER)", MyConn)
Rd = Cmd.ExecuteReader
Rd.Read()
If Not Rd.HasRows Then
Urutan = "PO/" & bulan & tahun & "/" & "000001"
Else
Cari = Microsoft.VisualBasic.Right(Rd.GetSqlValue(0).ToString(), 6)
If Microsoft.VisualBasic.Left(Rd.GetSqlValue(0).ToString(), 8) <> "PO/" & bulan & tahun & "/" Then
Urutan = "PO/" & bulan & tahun & "/" & "000001"
Else
Hitung = Microsoft.VisualBasic.Right(Rd.GetSqlValue(0).ToString(), 6) + 1
Urutan = "PO/" & bulan & tahun & "/" & Microsoft.VisualBasic.Right("000000" & Hitung, 6)
End If
End If
Rd.Close()
txtpo.SelectedText = Urutan
End Sub

How to get the value of a cell in VBA MS Project

I need a code that gives a message depending the different values of some particular cells in a Project, I have already used VBA in Excel, but never in Project, so I don´t know how to do it
I did a code in VBA Excel that do the same thing that I need to do in Project, but I need the code but with the infomation of a Project, but the functions that I use in Excel, are not defined in Project, so I don´t know how to addecuate the code
Option Explicit
Private Sub Avance()
Dim PtjR As Double
Dim PtjP As Double
Dim FechaRI As Date
Dim FechaRF As Date
Dim FechaPI As Date
Dim FechaPF As Date
Dim ListaT As String
Dim i As Integer
Dim ListaTT As String
For i = 3 To 40
FechaPI = Round(Cells(i, 2).Value, 2)
FechaPF = Cells(i, 3).Value
FechaRI = Cells(i, 4).Value
FechaRF = Cells(i, 5).Value
PtjR = Round(Cells(i, 6).Value, 2)
PtjP = Round(Cells(i, 7).Value, 2)
If PtjR < PtjP Then
ListaT = ListaT & vbNewLine & Chr(13) & "La tarea" & " " & Cells(i, 1).Value & " " & "se encuentra en DELATE, lleva" & " " & PtjR * 100 & "% y debería llevar" & " " & PtjP * 100 & "%"
If FechaPF - DateValue(Now) < 0 Then
ListaT = ListaT & ". Esta tarea debió terminar hace " & -(FechaPF - DateValue(Now)) & " días."
ElseIf FechaPF - DateValue(Now) <= 7 Then
ListaT = ListaT & ". Esta tarea termina en " & FechaPF - DateValue(Now) & " días."
End If
End If
Next i
MsgBox ListaT, vbCritical, "Advertencia"
End Sub
I am not sure if the way how I get the values of the cells are the only thing that will change about the code, but knowing how to do that, will be a big help
To get the values of a task, use the Task object. In this case you'll want to loop through all of the tasks using the Tasks object (collection of all tasks). It is unclear what task fields you need, but this should get you started:
Private Sub Avance()
Dim PtjR As Double
Dim PtjP As Double
Dim FechaRI As Date
Dim FechaRF As Date
Dim FechaPI As Date
Dim FechaPF As Date
Dim ListaT As String
Dim t As Task
For Each t In ActiveProject.Tasks
FechaPI = t.Start
FechaPF = t.Finish
FechaRI = IIf(t.BaselineStart = "NA", 0, t.BaselineStart)
FechaRF = IIf(t.BaselineFinish = "NA", 0, t.BaselineFinish)
PtjR = t.PercentComplete
PtjP = t.PhysicalPercentComplete
If PtjR < PtjP Then
ListaT = ListaT & vbNewLine & Chr(13) & "La tarea" & " " & t.Name & " " & "se encuentra en DELATE, lleva" & " " & PtjR * 100 & "% y debería llevar" & " " & PtjP * 100 & "%"
If FechaPF - DateValue(Now) < 0 Then
ListaT = ListaT & ". Esta tarea debió terminar hace " & -(FechaPF - DateValue(Now)) & " días."
ElseIf FechaPF - DateValue(Now) <= 7 Then
ListaT = ListaT & ". Esta tarea termina en " & FechaPF - DateValue(Now) & " días."
End If
End If
Next t
MsgBox ListaT, vbCritical, "Advertencia"
End Sub
Note that the Percent Complete and Physical Percent Complete properties return a value from 0 to 100, so don't multiply by 100 later on.

If array is empty error

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

sql,vb error "no row at position 0"

I get the message "there is no row at position 0" in the form with the following code:
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim i As Integer
Dim xid As Integer
Dim xQTY(0) As Integer
Dim xQTY_ID(0) As Integer
Dim xCount As Integer
Dim xCounter_ID(0) As Integer
'Dim sqlstrx(5) As String
xid = 0
**If lstitems.Items.Count > 0 Then**
If Split(Me.Text, " - ")(1) = "Add" Then
sqlSTR = "INSERT INTO orders (CustID, Cust_Name, order_date) " & _
"VALUES (" & txtcustid.Text & ", " _
& "'" & txtcustname.Text & "', " _
& "'" & Format(dttoday.Value, "MM/dd/yyyy") & "')"
ExecuteSQLQuery(sqlSTR)
sqlSTR = "SELECT * FROM orders ORDER BY order_no DESC"
ExecuteSQLQuery(sqlSTR)
xid = sqlDT.Rows(0)("order_no")
For i = 0 To lstitems.Items.Count - 1
sqlSTR = "INSERT INTO orders_detail (order_no, idDrug, DrugName, Unit_Cost, qty, totalcost) " & _
"VALUES (" & xid & ", " _
& lstitems.Items(i).Text & ", " _
& "'" & lstitems.Items(i).SubItems(1).Text & "', " _
& "'" & lstitems.Items(i).SubItems(2).Text & "', " _
& lstitems.Items(i).SubItems(3).Text & ", " _
& lstitems.Items(i).SubItems(4).Text & ", " _
& lstitems.Items(i).SubItems(5).Text & ")"
ExecuteSQLQuery(sqlSTR)
Error highlights the bold line.
your code missing fill for your dataTable
Try this :
Dim sqlAdapter As New SqlDataAdapter(sqlSTR, yourconnctionstring)
sqlAdapter.Fill(sqlDT)
If sqlDT.Rows.Count > 0 Then
xid = sqlDT.Rows(0)("order_no")
End If
Try like this
Dim sqlAdapter As New SqlDataAdapter(sqlSTR, yourconnctionstring)
sqlAdapter.Fill(sqlDT)
If sqlDT.Rows.Count > 0 Then
xid = sqlDT.Rows(0)("order_no")
End If
'write your code here.....

Loop through subfolders by name

The folder path is "Mailbox - IT Support Center"\"Onshore - Josh"\"Completed".
I want to know the total number of emails in each person's completed mailbox from yesterday.
There are about 25 mailboxes but they all follow the same path starting at Mailbox - IT Support Center.
The script works except for the loop. It iterates through to the second folder but it still searches the first mailbox. Most likely something wrong with objfolder.
Sub CompletedEmailCount()
Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder
Dim MailItem
Dim EmailCount As Integer
Dim strFolderName
Dim FolderName() As Variant
Dim i As Integer
Dim objFolder1
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objnSpace.GetDefaultFolder(olFolderInbox)
strFolderName = ("Mailbox - IT Support Center")
ReDim FolderName(3) 'change this to how many folders you have
'assign each folder name on the array elements
FolderName(1) = ("Onshore - Josh")
FolderName(2) = ("OnShore - Ashton")
FolderName(3) = ("OnShore - Beth")
'loop through each foldername
For i = 1 To 3
On Error Resume
Next
Set objFolder = objnSpace.Folders(strFolderName).Folders(FolderName(i)).Folders ("completed")
On Error GoTo 0
If objFolder Is Nothing Then GoTo skip
For Each MailItem In objFolder.Items
If DatePart("d", Date - 1) = DatePart("d", MailItem.ReceivedTime) Then EmailCount = EmailCount + 1
Next
skip:
Next
MsgBox "Completed Email Totals from Yesterday: " & EmailCount
End Sub
Found this out through trial and error, but returns what I need.
Sub Tester()
'Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder
Dim MailItem
Dim EmailCount() As Integer, arrNames
Dim completed, x As Long, num As Long
Dim DayTotal
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
arrNames = Array("Ashton", "Beth", "Bree", "Brittany", "Cecilia", "Chance", "Christina", "Christine", "Dustin", "James", "Jeff", "Jenni", "Jennifer", "Jeromy", "Josh", "Josie", "Lisa", "Misti", "Nathan", "Paul", "Robert", "Sam", "Shane", "Shawna") 'add other names here...
ReDim EmailCount(LBound(arrNames) To UBound(arrNames))
For x = LBound(arrNames) To UBound(arrNames)
On Error Resume Next
Set objFolder = objnSpace.Folders("Mailbox - IT Support Center"). _
Folders("Onshore - " & arrNames(x)).Folders("completed")
On Error GoTo 0
num1 = 0
num2 = 0
num3 = 0
num4 = 0
num5 = 0
num6 = 0
num7 = 0
Dim Totalmsg
If Not objFolder Is Nothing Then
For Each MailItem In objFolder.Items
If DateValue(Date - 1) = _
DateValue(MailItem.ReceivedTime) Then num1 = num1 + 1
If DateValue(Date - 2) = _
DateValue(MailItem.ReceivedTime) Then num2 = num2 + 1
If DateValue(Date - 3) = _
DateValue(MailItem.ReceivedTime) Then num3 = num3 + 1
If DateValue(Date - 4) = _
DateValue(MailItem.ReceivedTime) Then num4 = num4 + 1
If DateValue(Date - 5) = _
DateValue(MailItem.ReceivedTime) Then num5 = num5 + 1
If DateValue(Date - 6) = _
DateValue(MailItem.ReceivedTime) Then num6 = num6 + 1
If DateValue(Date - 7) = _
DateValue(MailItem.ReceivedTime) Then num7 = num7 + 1
Next
Dim DayTotal1 As String
Dim DayTotal2 As String
Dim DayTotal3 As String
Dim DayTotal4 As String
Dim DayTotal5 As String
Dim DayTotal6 As String
Dim DayTotal7 As String
DayTotal1 = arrNames(x) & " " & (Date - 1) & " " & num1
DayTotal2 = arrNames(x) & " " & (Date - 2) & " " & num2
DayTotal3 = arrNames(x) & " " & (Date - 3) & " " & num3
DayTotal4 = arrNames(x) & " " & (Date - 4) & " " & num4
DayTotal5 = arrNames(x) & " " & (Date - 5) & " " & num5
DayTotal6 = arrNames(x) & " " & (Date - 6) & " " & num6
DayTotal7 = arrNames(x) & " " & (Date - 7) & " " & num7
DayTotal = DayTotal1 & vbNewLine _
& DayTotal2 & vbNewLine _
& DayTotal3 & vbNewLine _
& DayTotal4 & vbNewLine _
& DayTotal5 & vbNewLine _
& DayTotal6 & vbNewLine _
& DayTotal7 & vbNewLine _
DayTotals = DayTotals & DayTotal
msg = arrNames(x) & " " & (Date - 1) & " " & num1 & vbNewLine _
& arrNames(x) & " " & (Date - 2) & " " & num2 & vbNewLine _
& arrNames(x) & " " & (Date - 3) & " " & num3 & vbNewLine _
& arrNames(x) & " " & (Date - 4) & " " & num4 & vbNewLine _
& arrNames(x) & " " & (Date - 5) & " " & num5 & vbNewLine _
& arrNames(x) & " " & (Date - 6) & " " & num6 & vbNewLine _
& arrNames(x) & " " & (Date - 7) & " " & num7
' Totalmsg = msg
End If
Debug.Print arrNames(x), num
Next x
MsgBox DayTotals
Set OutApp = CreateObject("outlook.Application")
Set OutMail = OutApp.CreateItem(o)
With OutMail
.Subject = "Total Completed Tickets"
.To = "plotnerj#schneider.com"
.Body = DayTotals
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
Set objFolder = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
End Sub