Trying to send three variables (startDate, endDate, division) from a chooser form to a report query. Sent the three variables through an doCommand.OpenReport command, then unpacked them in the Load event of the report. I'm not sure what to assign them to in the report to use them in the query.
In the form:
Private Sub btn_bud_sum_exp_div_Click()
Dim StrWhereCondition
Dim a As String
Dim b As String
Dim c As String
a = Me.txtStartDate.Value
b = Me.txtEndDate.Value
c = Me.lstDivision.Value
StrWhereCondition = a & "|" & b & "|" & c
'StrWhereCondition = "[accounting start date] = " & Me.txtStartDate.Text
DoCmd.OpenReport "FY15 Budget Line Sum - Expenditures - Div", , , StrWhereCondition
End Sub
In the Load event of the report:
Private Sub Report_Load()
Dim dtStartDate As Date
Dim dtEndDate As Date
Dim strDivision As String
If Not IsNull(Me.OpenArgs) Then
dtStartDate = parsetext(OpenArgs, 0)
dtEndDate = parsetext(OpenArgs, 1)
strDivision = parsetext(OpenArgs, 2)
End If
End Sub
In the report query:
SELECT
*
FROM
budget b
WHERE
(b.start_date between dtStartDate AND dtEndDate) AND d.division = strDivision
If you open the report with a filter string, you can delete all the OnLoad stuff of the report:
Private Sub btn_bud_sum_exp_div_Click()
Dim StartDate As String
Dim EndDate As String
Dim Division As String
Dim WhereCondition As String
StartDate = Format(Me!txtStartDate.Value, "yyyy\/mm\/dd")
EndDate = Format(Me!txtEndDate.Value, "yyyy\/mm\/dd")
Division = Me!lstDivision.Value
WhereCondition = "start_date between #" & StartDate & "# and #" & EndDate & "# and division = " & Division & ""
' or:
' WhereCondition = "[accounting start date] between #" & StartDate & "# and #" & EndDate & "# and division = " & Division & ""
' or, if division is text:
' WhereCondition = "start_date between #" & StartDate & "# and #" & EndDate & "# and division = '" & Division & "'"
DoCmd.OpenReport "FY15 Budget Line Sum - Expenditures - Div", , , WhereCondition
End Sub
Related
I am trying to list the dates between two given months: a) 1/1/2021; b) 6/1/2021 in format: 01.2021; 02.2021; 03.2021; 04.2021; 05.2021; 06.2021
I was able to find and use this UDF:
Function MONTHRANGE(startDate As Date, endDate As Date, _
Optional Delim As String = "; ", _
Optional dFormat As String = "MM.YYYY") As String
MONTHRANGE = Join(Evaluate("TRANSPOSE(TEXT(ROW(" & CLng(startDate) & ":" & CLng(endDate) & ")," & Chr(34) & dFormat & Chr(34) & "))"), Delim)
End Function
The output of this is repeated dates (for each day of the month) in the format I want - how can I return just the unique values (one - per month)?
Something like the following gets the job done:
Option Explicit
Private Sub Test()
Debug.Print GetMonths(CDate("1/1/2021"), CDate("6/1/2021"))
End Sub
Private Function GetMonths(ByVal StartDate As Date, ByVal EndDate As Date) As String
Do While StartDate <= EndDate
GetMonths = GetMonths & Format(Month(StartDate), "00") & "." & Year(StartDate) & "; "
StartDate = DateAdd("m", 1, StartDate)
Loop
GetMonths = Left(GetMonths, Len(GetMonths) - 2)
End Function
This is my code below. I am trying to search a database using two different dates, company name
I am getting an error when one of the date fields is empty or null. How can I solve this issue or bypass if the date search field is empty to ignore it in the search or search for an empty field?
Dim SQL As String
SQL = "SELECT * from qryRequestInternal where ([DateRequestSent] = #" & txt_Search_Sdate & "# AND [DateReceived] = #" & txt_Search_Rdate & "# AND (companyName like ""*" & txt_SCompNa & "*"") )"
Me.sfrmRequestInternal.Form.RecordSource = SQL
Me.sfrmRequestInternal.Form.Requery
Me.sfrmRequestInternal_col.Form.RecordSource = SQL
Me.sfrmRequestInternal_col.Form.Requery
End Sub
You will need to check for Null values and build the SQL string based on the controls which have a value (i.e. not null).
The example below uses a helper function to build the sql string. If nothing is inserted, it will only run the the Select * from qryRequestInternal without any criteria.
Private Function SqlWhere() As String
Dim retvalue As String
'sent date
With txt_Search_Sdate
If Not IsNull(.Value) Then
retvalue = " WHERE [DateRequestSent] = #" & Format(.Value, "mm/dd/yyyy") & "#"
End If
End With
'received date
With txt_Search_Rdate
If Not IsNull(.Value) Then
retvalue = IIf(Len(retvalue) = 0, " WHERE", retvalue & " AND") & " [DateReceived] = #" & Format(.Value, "mm/dd/yyyy") & "#"
End If
End With
'company
With txt_SCompNa
If Not IsNull(.Value) Then
retvalue = IIf(Len(retvalue) = 0, " WHERE", retvalue & " AND") & " [companyName] Like '*" & .Value & "*'"
End If
End With
SqlWhere = retvalue
End Function
To call it:
Dim sqlString As String
sqlString = "SELECT * from qryRequestInternal" & SqlWhere()
Debug.Print sqlString
I want to insert multiple rows in the table below, but I'm getting this error Run-time error 3061, too few parameters expected 1 Of course I did something wrong here, but I don't understand what I did wrong.
Table:InstructorAttendance
ID = AutoNumber (long integer)
AttnID = Number (long integer) pk
IUID = Number (long integer) not null
AttnDate = DateTime not null
AttnStatus = Number (long integer)
MS = Calculated field
MN = Calculated field
The code with which I am trying to insert data is described below:
Private Sub cmdGenerate_Click()
Dim DateExist As Integer
Dim Filter As String
Dim strDate, FD, LD As Date
Dim NextDate As Date
strDate = CDate(Me.frmMonth & "/" & Me.cboYear)
FD = DateSerial(Year(strDate), Month(strDate), 1)
LD = DateSerial(Year(strDate), Month(strDate) + 1, 1) - 1
DateExist = DCount("AttnDate", "InstructorAttendance", "AttnDate>=#" & [FD] & "# And AttnDate<=#" & [LD] & "# And IUID=" & Me.[IUID])
If DateExist > 0 Then
Debug.Print "exist"
Else
NextDate = FD
While DateDiff("d", NextDate, LD) >= 0
DoCmd.SetWarnings False
CurrentDb.Execute "INSERT INTO InstructorAttendance (AttnID, IUID, AttnDate) " & _
"Values (" & DMax("AttnID", "InstructorAttendance") + 1 & ", " & Me.IUID & ", NextDate)"
DoCmd.SetWarnings True
NextDate = DateAdd("d", 1, NextDate)
Wend
End If
End Sub
Concatenate NextDate variable with # delimiters.
& ", " & Me.IUID & ", #" & NextDate & "#)"
I have a table with Multiple Date Fields like
DOB (Date),
DOA_ASST(Date),
DOA_UDC (Date)
and also have some fields like
ID (AutoNumber),
EmpID (Integer),
EmpName (Text),
SeniorityNumber (Integer) etc.
Many of Employee Promoted Same Date and Also Have the same date of Appointment. I want to provide serial no as per their seniority according to their first date of Appointment.
Criteria are following-
if Multiple Employee Promoted on the Same date then Date of Appointment with older date will be considered, if Date of Appointment are same then Date of Birth with Older date will be considered.
I tried the following code:-
Private Sub cmdProcess_Click()
Dim rst As Recordset
Dim rst1 As Recordset
Dim LastSN As Integer
Dim str, strAsst, strUDC, strLDC As String
Dim LDCDate, UDCDate, AsstDate, BirthDate As Date
AsstDate = Nz(DMax("[DOP_ASST]", "tblDraftSeniority"), DMin("[DOP_ASST]", "tblRawSeniority"))
UDCDate = DMin("[DOP_UDC]", "tblRawSeniority", "[DOP_ASST] = #" & AsstDate & "#")
LDCDate = DMin("[DOA_ESIC]", "tblRawSeniority", "[DOP_UDC] = #" & UDCDate & "# AND [DOP_ASST] = #" & AsstDate & "#")
BirthDate = DMin("[DOB]", "tblRawSeniority", "[DOA_ESIC] = #" & LDCDate & "#")
LastSN = DLookup("[ID]", "tblRawSeniority", "[DOB] = #" & BirthDate & "#")
Set rst = CurrentDb.OpenRecordset("tblRawSeniority")
Set rst1 = CurrentDb.OpenRecordset("tblDraftSeniority")
rst.MoveLast
rst.MoveFirst
Do While rst.EOF = False
If rst!ID = LastSN Then
With rst
Me.txtEmpName1 = rst!EmpName
Me.txtEmpCatg1 = rst!Category
Me.txtEmpDOB1 = rst!DOB
Me.txtEmpDOEntry1 = rst!DOA_ESIC
Me.txtEmpDONextPromo1 = rst!DOP_UDC
Me.txtEmpDOCurrentPromo1 = rst!DOP_ASST
Me.txtEmpStateRegion1 = rst!Region
Me.txtRemark1 = rst!Remark
Me.txtSN1 = rst!SrNoHQRS
End With
End If
rst.MoveNext
Loop
Set rst = Nothing
End Sub
But there is null value in LDCDate with Two date Criteria
Since rst1 is not used, and you are only looking up one ID, you could try something like:
Private Sub cmdProcess_Click()
Dim rst As DAO.Recordset
Dim LastSN As Long
Dim LDCDate As Date
Dim UDCDate As Date
Dim AsstDate As Date
Dim BirthDate As Date
AsstDate = Nz(DMax("[DOP_ASST]", "tblDraftSeniority"), DMin("[DOP_ASST]", "tblRawSeniority"))
UDCDate = Nz(DMin("[DOP_UDC]", "tblRawSeniority", "[DOP_ASST] = #" & Format(AsstDate, "yyyy\/mm\/dd") & "#"), DMax("[DOP_UDC]", "tblRawSeniority"))
LDCDate = Nz(DMin("[DOA_ESIC]", "tblRawSeniority", "[DOP_UDC] = #" & Format(UDCDate, "yyyy\/mm\/dd") & "# AND [DOP_ASST] = #" & Format(AsstDate, "yyyy\/mm\/dd") & "#"), DMax("[DOP_ESIC]", "tblRawSeniority"))
BirthDate = DMin("[DOB]", "tblRawSeniority", "[DOA_ESIC] = #" & Format(LDCDate, "yyyy\/mm\/dd") & "#")
LastSN = Nz(DLookup("[ID]", "tblRawSeniority", "[DOB] = #" & Format(BirthDate, "yyyy\/mm\/dd") & "#"), Date)
Set rst = CurrentDb.OpenRecordset("tblRawSeniority")
rst.FindFirst "ID = " & LastSN & ""
If Not rst.NoMatch Then
Me.txtEmpName1 = rst!EmpName
Me.txtEmpCatg1 = rst!Category
Me.txtEmpDOB1 = rst!DOB
Me.txtEmpDOEntry1 = rst!DOA_ESIC
Me.txtEmpDONextPromo1 = rst!DOP_UDC
Me.txtEmpDOCurrentPromo1 = rst!DOP_ASST
Me.txtEmpStateRegion1 = rst!Region
Me.txtRemark1 = rst!Remark
Me.txtSN1 = rst!SrNoHQRS
End If
rst.Close
Set rst = Nothing
End Sub
Since you are allowing null values for AsstDate, you are most likely using a null value (or 0) in the criteria for the LDCDate DMin lookup. If your table doesn't actually have nulls in the DOP_ASST field, then your LDCDate will be null because there is no result found.
Both code snippets don't work.
My column name is Shipment End in DataGridView1
Dim az As Date = DateTimePicker3.Value
Dim bz As Date = DateTimePicker4.Value
Me.Sheet1BindingSource.Filter = "([Shipment End]<= '" & DateTimePicker3.Value & "' and [Shipment End]>= '" & DateTimePicker4.Value & ")"
and
Dim DateFrom As String = String.Format("Shipment End >= '{0:yyyy-MM-dd}' ", DateTimePicker3.Value)
Dim DateTo As String = String.Format("Shipment End <= '{0:yyyy-MM-dd}' ", DateTimePicker4.Value)