Hello,
I am new to VBA and have a tremendous task dropped on me. Until now I have been able to work through everything or apply code I have found online. I am trying to create a drop down list the will let me choose one or multiple items. In this case Different routes and then I want to the user to enter a begin date and end date in text boxes.
This is my code any help would be greatly appreciated.
First for the listbox
Private Function GetRoutes() As String
Dim stDocCriteria As String
Dim VarItm As Variant
For Each VarItm In ListFilter.ItemsSelected
stDocCriteria = stDocCriteria & "[Route] = """ & ListFilter.Column(0, VarItm) & """ OR "
Next
If stDocCriteria <> "" Then
stDocCriteria = Left(stDocCriteria, Len(stDocCriteria) - 4)
Else
stDocCriteria = "True"
End If
GetRoutes = stDocCriteria
End Function
Then For the Command Button:
Private Sub cmdPreview_Click()
Dim strReport As String
Dim strDateField As String
Dim strWhere As String
Dim lngView As Long
Const strcJetDate = "\#mm\/dd\/yyyy\#"
strReport = "rptShipViaGrossProfit"
strDateField = "[Date]" '
lngView = acViewPreview
If Not IsNull(GetRoutes) Then
strWhere = " ROUTE LIKE GetRoutes"
If IsDate(Me.txtStartDate) Then
strWhere = strWhere & " and (" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
End If
MsgBox "Values Are Selected", 64, "test"
End If
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
DoCmd.OpenReport strReport, lngView, , strWhere
Exit_Handler:
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open report"
End If
Resume Exit_Handler
End Sub
I am new to VBA and have a tremendous task dropped on me. Until now I have been able to work through everything or apply code I have found online. I am trying to create a drop down list the will let me choose one or multiple items. In this case Different routes and then I want to the user to enter a begin date and end date in text boxes.
This is my code any help would be greatly appreciated.
First for the listbox
Private Function GetRoutes() As String
Dim stDocCriteria As String
Dim VarItm As Variant
For Each VarItm In ListFilter.ItemsSelected
stDocCriteria = stDocCriteria & "[Route] = """ & ListFilter.Column(0, VarItm) & """ OR "
Next
If stDocCriteria <> "" Then
stDocCriteria = Left(stDocCriteria, Len(stDocCriteria) - 4)
Else
stDocCriteria = "True"
End If
GetRoutes = stDocCriteria
End Function
Then For the Command Button:
Private Sub cmdPreview_Click()
Dim strReport As String
Dim strDateField As String
Dim strWhere As String
Dim lngView As Long
Const strcJetDate = "\#mm\/dd\/yyyy\#"
strReport = "rptShipViaGrossProfit"
strDateField = "[Date]" '
lngView = acViewPreview
If Not IsNull(GetRoutes) Then
strWhere = " ROUTE LIKE GetRoutes"
If IsDate(Me.txtStartDate) Then
strWhere = strWhere & " and (" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
End If
MsgBox "Values Are Selected", 64, "test"
End If
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
DoCmd.OpenReport strReport, lngView, , strWhere
Exit_Handler:
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open report"
End If
Resume Exit_Handler
End Sub