Hi, All.
A bit unusual question. The code below works and does what it needs to do, but I just don't like to repeat same code over and over again, is there any suggestions, how to get some bits shorter (in function, separate routine). As only thing changes is the "If" statement and variable in each case. Haven't found a way to pass whole "If" statement to a function.
Thank You for answers.
A bit unusual question. The code below works and does what it needs to do, but I just don't like to repeat same code over and over again, is there any suggestions, how to get some bits shorter (in function, separate routine). As only thing changes is the "If" statement and variable in each case. Haven't found a way to pass whole "If" statement to a function.
Thank You for answers.
Code:
Dim strMyPath As String, strDBName As String, strDB As String, sql As String, cbv As String, t As String, t2 As String
Dim daoDB As DAO.Database
Dim recSet As DAO.Recordset
Dim i As Long
cbv = cb_time.Value
lb_lines.Clear
strDBName = "Database.mdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
Set daoDB = DBEngine.Workspaces(0).OpenDatabase(strDB)
sql = "SELECT * FROM NLD WHERE Released=False"
Set recSet = daoDB.OpenRecordset(sql, dbOpenDynaset)
i = 0
recSet.MoveLast
recSet.MoveFirst
Select Case cbv
Case Is = "Any Time"
Do While Not recSet.EOF
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
recSet.MoveNext
Loop
Case Is = "This Year"
t = Year(Date)
Do While Not recSet.EOF
[COLOR=#0000cd][B] If Mid(recSet("TimeAdded").Value, 7, 4) = t Then[/B][/COLOR]
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
End If
recSet.MoveNext
Loop
Case Is = "Last 6 Months"
t = Format(DateAdd("m", -6, Date), "mm/yyyy")
Do While Not recSet.EOF
[B][COLOR=#0000cd] If Mid(recSet("TimeAdded").Value, 4, 7) >= t Then[/COLOR][/B]
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
End If
recSet.MoveNext
Loop
Case Is = "Last 3 Months"
t = Format(DateAdd("m", -3, Date), "mm/yyyy")
Do While Not recSet.EOF
[COLOR=#0000cd][B] If Mid(recSet("TimeAdded").Value, 4, 7) >= t Then[/B][/COLOR]
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
End If
recSet.MoveNext
Loop
Case Is = "This Month"
t = Format(Date, "mm/yyyy")
Do While Not recSet.EOF
[B][COLOR=#0000cd] If Mid(recSet("TimeAdded").Value, 4, 7) = t Then[/COLOR][/B]
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
End If
recSet.MoveNext
Loop
Case Is = "This Week"
t = Format(Date, "ww")
Do While Not recSet.EOF
[B][COLOR=#0000cd] If Format(Mid(recSet("TimeAdded").Value, 1, 10), "ww") = t Then[/COLOR][/B]
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
End If
recSet.MoveNext
Loop
Case Is = "Yesterday"
t = DateAdd("d", -1, Format(Date, "dd/mm/yyyy"))
Do While Not recSet.EOF
[B][COLOR=#0000cd] If Mid(recSet("TimeAdded").Value, 1, 10) = t Then[/COLOR][/B]
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
End If
recSet.MoveNext
Loop
Case Is = "Today"
t = Format(Date, "dd/mm/yyyy")
Do While Not recSet.EOF
[COLOR=#0000cd][B] If Mid(recSet("TimeAdded").Value, 1, 10) = t Then[/B][/COLOR]
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
End If
recSet.MoveNext
Loop
Case Is = "Last 2h"
t = Format(Date, "dd/mm/yyyy")
t2 = Format(Now - TimeValue("02:00:00"), "h")
Do While Not recSet.EOF
[COLOR=#0000cd][B] If Mid(recSet("TimeAdded").Value, 1, 10) = t And Format(recSet("TimeAdded").Value, "h") >= t2 Then[/B][/COLOR]
With lb_lines
.AddItem
.List(i, 0) = recSet("ItemCode").Value
.List(i, 1) = recSet("Description").Value
.List(i, 2) = recSet("Supplier").Value
.List(i, 3) = Left(recSet("TimeAdded").Value, 10)
.List(i, 4) = recSet("AddedBy").Value
End With
i = i + 1
End If
recSet.MoveNext
Loop
End Select
Last edited: