ZeroEffect
Junior Contributor
In my application I have timed events to play audio based on day of the week, start time and an end time. and the code below is how I have it running. I am looking for suggestions and examples of a better more effecient way, in short another set of eyes to look at it. I know it is ugly, it is from on of my first programs that I am updating/adding on to.
Last part in another post
ZeroEffect
Visual Basic:
Public Sub TodaysList(ByVal intAdvance As Integer, ByVal strDBFile As String)
Dim Itm As ListViewItem
Dim strConnect, strSql As String
Dim St, Et, D, DS, AL, Dis As String
Dim I As Integer
Dim objDA1 As New OdbcDataAdapter
Dim objDS1 As New System.Data.DataSet
Dim dbCommand As New Data.OleDb.OleDbCommand
Dim intCounter As Integer
Dim mvarSysProjPath As String = Application.StartupPath & "\"
strConnect = "Provider=MSDASQL/SQLServer ODBC;Driver={Microsoft Visual FoxPro Driver};" _
& "SourceType=DBF;SourceDB=" & mvarSysProjPath _
& ";InternetTimeout=300000;Transact Updates=True"
strSql = "SELECT * FROM " & strDBFile & ".dbf"
objDA1 = New OdbcDataAdapter(strSql, strConnect)
objDA1.Fill(objDS1, strDBFile)
Dim objDT1 As DataTable = objDS1.Tables(0)
Dim blnDayNotFound As Boolean 'was the date found
Dim strDay As String
Do
If strDBFile = "TMDENTS" Then
MainForm.lvTodaysEvents.Items.Clear() 'clear the lv of events
CurrentDayStr = Format(System.DateTime.FromOADate(Today.ToOADate + intAdvance), "dddd")
'format the date to the day of the week plus the advance number
For I = 0 To objDS1.Tables(strDBFile).Rows.Count - 1
With objDS1.Tables(strDBFile).Rows(I)
strDay = Replace(!Day, " ", "")
If strDay = CurrentDayStr Then 'was the day found
blnDayNotFound = True
LVR = 1 ' As I am going through this can't remeber what this is for
St = Replace(!StartTime, " ", "")
Et = Replace(!EndTime, " ", "")
D = Replace(!Day, " ", "")
DS = RTrim(!Descript)
Itm = MainForm.lvTodaysEvents.Items.Add(St)
Itm.ImageIndex = 6
Itm.SubItems.Add(Et)
Itm.SubItems.Add(D)
Itm.SubItems.Add(DS)
End If
End With
Next I
ElseIf strDBFile = "PADEVENT" Then
MainForm.lvTodayIboc.Items.Clear()
CurrentDayStr = Format(System.DateTime.FromOADate(Today.ToOADate + intAdvance), "dddd")
For I = 0 To objDS1.Tables(strDBFile).Rows.Count - 1
With objDS1.Tables(strDBFile).Rows(I)
strDay = Replace(!Day, " ", "")
If strDay = CurrentDayStr Then
blnDayNotFound = True
LVR = 1
St = Replace(!StartTime, " ", "")
Et = Replace(!Day, " ", "")
D = Replace(!Title, " ", "")
DS = RTrim(!Artist)
AL = RTrim(!Album)
Dis = RTrim(!Discript)
Itm = MainForm.lvTodayIboc.Items.Add(St)
Itm.ImageIndex = 6
Itm.SubItems.Add(Et)
Itm.SubItems.Add(Dis)
Itm.SubItems.Add(D)
Itm.SubItems.Add(DS)
Itm.SubItems.Add(AL)
End If
End With
Next I
End If
If blnDayNotFound = False Then
intAdvance = intAdvance + 1 'add one to the date
Else
If LVR = 1 Then 'can't remember
If intAdvance = 7 Then
NextWeek = True
End If
NextInList(strDBFile)
End If
Exit Do
End If
Loop Until intAdvance > 7
objDS1.Dispose()
End Sub
'Check the next event in the list
'and if it runs it runs another sub to set the data
'this sub is used with two different dbf files
Public Sub NextInList(ByVal strDBFile As String)
Dim TimeListDay As ListView.SelectedListViewItemCollection
Dim SelectedTime As ListViewItem
Dim strtime As Date
Dim inttime As Integer
Dim CurrentTimeStr As String
Dim CurrentTimeInt As Integer
Dim temp As Integer
Dim strDay As String
Dim intCount As Integer
CurrentTimeStr = Replace(Format(Now(), "HH:mm"), ":", "")
CurrentTimeInt = CurrentTimeStr
CurrentDayStr = Format(System.DateTime.FromOADate(Today.ToOADate), "dddd")
temp = 1
If strDBFile = "TMDENTS" Then
TimeListDay = MainForm.lvTodaysEvents.SelectedItems
intCount = MainForm.lvTodaysEvents.Items.Count
ElseIf strDBFile = "PADEVENT" Then
TimeListDay = MainForm.lvTodayIboc.SelectedItems
intCount = MainForm.lvTodayIboc.Items.Count
End If
If NextWeek = False Then
If intCount > 0 Then
Do
If strDBFile = "TMDENTS" Then
MainForm.lvTodaysEvents.Items(temp - 1).Selected = True
ElseIf strDBFile = "PADEVENT" Then
MainForm.lvTodayIboc.Items(temp - 1).Selected = True
End If
For Each SelectedTime In TimeListDay
strtime = SelectedTime.Text
inttime = Replace(Format(strtime, "HH:mm"), ":", "")
If strDBFile = "TMDENTS" Then
strDay = SelectedTime.SubItems(2).Text
ElseIf strDBFile = "PADEVENT" Then
strDay = SelectedTime.SubItems(1).Text
End If
If inttime > CurrentTimeInt And strDay = CurrentDayStr Then
If strDBFile = "TMDENTS" Then
If blnRunTimedEvents = True Then
RunTimedEvent(strDBFile)
Exit Sub
Else
RunTimedEvent(strDBFile)
End If
ElseIf strDBFile = "PADEVENT" Then
If blnRunIbocEvents = True Then
If SelectedTime.Checked = False Then
RunTimedEvent(strDBFile)
Exit Sub
ElseIf SelectedTime.Checked = True Then
SelectedTime.Checked = True
SelectedTime.ForeColor = System.Drawing.Color.Gray
SelectedTime.Font = New Font(SelectedTime.Font, FontStyle.Strikeout)
Exit For
End If
Else
If SelectedTime.Checked = False Then
RunTimedEvent(strDBFile)
End If
End If
End If
Exit Sub
ElseIf inttime <= CurrentTimeInt And strDay = CurrentDayStr Or SelectedTime.Checked = True Then
SelectedTime.Checked = True
SelectedTime.ForeColor = System.Drawing.Color.Gray
SelectedTime.Font = New Font(SelectedTime.Font, FontStyle.Strikeout)
End If
Next
temp = temp + 1
Loop Until temp > intCount
If strDay <> CurrentDayStr Then
If strDBFile = "TMDENTS" Then
MainForm.lvTodaysEvents.Items(0).Selected = True
ElseIf strDBFile = "PADEVENT" Then
MainForm.lvTodayIboc.Items(0).Selected = True
End If
'Need to add event to trigger loading of single event that happened all ready
End If
For Each SelectedTime In TimeListDay
If SelectedTime.Checked = True And SelectedTime.Index = (intCount - 1) Then
TodaysList(1, strDBFile)
RunTimedEvent(strDBFile)
Exit Sub
End If
Next
If intCount = 0 Then
TodaysList(1, strDBFile)
RunTimedEvent(strDBFile)
Exit Sub
End If
End If
Else
NextWeek = False
If strDBFile = "TMDENTS" Then
MainForm.lvTodaysEvents.Items(0).Selected = True
ElseIf strDBFile = "PADEVENT" Then
MainForm.lvTodayIboc.Items(0).Selected = True
End If
End If
End Sub
Last part in another post
ZeroEffect