Note: SMonth is date to and SYear is date from. I just haven't changed the names but the format has already been changed to short date.
I've created a code in Microsoft Access Visual Basic to create a date range but when I type the date range in both text boxes SMonth and SYear, it only shows the records with dates from txtYear and not the range date itself. This is the code.
'Date Range
ElseIf Me.SEmployeeName = "" And Me.Soo = False And Me.Scc = False And
Me.SMonth <> "" And Me.SYear <> "" Then
Me.tbl_ALL_Query_subform.Form.RecordSource = "Select * from tbl_ALL where
((((tbl_ALL.NTEDate ) <= '" & Me.SMonth.Value & "') AND (tbl_ALL.NTEDate) >=
'" & Me.SYear.Value & "')) "
Your txtTo and txtFrom don't seem to come into play, so hard to tell.
However, you compare date with text, so adjust your query:
ElseIf Me.SEmployeeName = "" And _
Me.Soo = False And Me.Scc = False And _
Me.SMonth <> "" And Me.SYear <> "" Then
Me!tbl_ALL_Query_subform.Form.RecordSource = _
"Select * from tbl_ALL where Month(tbl_ALL.NTEDate) <= " & Me!SMonth.Value & " AND Year(tbl_ALL.NTEDate) >= " & Me!SYear.Value & ""
If start date and end date:
"Select * From tbl_ALL Where tbl_ALL.NTEDate Between #" & Format(Me!StartDate.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!EndDate.Value, "yyyy\/mm\/dd") & "#"
Related
Aim is to reset Month Value and Year value every month and every year, however, it is not resetting. Please help.
Private Sub Proforma_Number_Generator_Command_Click()
Dim vLastM As Variant
Dim accM As Integer
Dim vLastY As Variant
Dim accY As Integer
'Sets the date of the Proforma Invoice Number to Today'
'Me.Proforma_Invoice_Date = Format(Date, "yyyy-mm-dd")
vLastM = DMax("[Month Value]", "[Proforma Invoice Form Table]", _
"PI_Month='" & Me.PI_Month.Value & "' AND PI_Year ='" & _
Me.PI_Year.Value & "'")
If IsNull(vLastM) Then
accM = 1
Else
accM = vLastM + 1
End If
Me.Month_Value = accM
'Year'
vLastY = DMax("[Year Value]", "[Proforma Invoice Form Table]", _
"PI_Year='" & Me.PI_Year.Value & "'")
If IsNull(vLastY) Then
accY = 1
Else
accY = vLastY + 1
End If
Me.Year_Value = accY
Me.Order_No = Format("ON" & "-" & Format(Date, "yyyy") & "-" & Me.Year_Value)
End Sub
It is confusing what your goal is, as month isn't used, but try this reduced code:
Private Sub Proforma_Number_Generator_Command_Click()
' Month. Not used?
Me!Month_Value.Value = Nz(DMax("[Month Value]", "[Proforma Invoice Form Table]", _
"PI_Month=" & Me!PI_Month.Value & " AND PI_Year ='" & _
Me.PI_Year.Value & ""), 0) + 1
' Year.
Me!Year_Value.Value = Nz(DMax("[Year Value]", "[Proforma Invoice Form Table]", _
"PI_Year=" & Me!PI_Year.Value & ""), 0) + 1
Me!Order_No.Value = Format("ON" & "-" & Format(Date, "yyyy") & "-" & Me!Year_Value.Value & "-" & )
End Sub
I Have this code, and I can't figure out what is wrong with it. It does not return any error but field Date_Returned is not getting updated. Please help.
Private Sub txtbxret_Click()
Dim query As String
query = "UPDATE Rent SET Date_Returned = '" & Date & "' WHERE Date_Rent = " & txtrented.Value & " AND Customer_ID = " & txtbxcustID.Value & " AND Movie_ID = " & txtbxmovID.Value
DoCmd.RunSQL (query)
End Sub
I've double and triple checked all the field names and thay are ok by the way...
You must use proper formatting of string expressions of date values in SQL:
query = "UPDATE Rent SET Date_Returned = Date() WHERE Date_Rent = #" & Format(txtrented.Value, "yyyy\/mm\/dd") & "# AND Customer_ID = " & txtbxcustID.Value & " AND Movie_ID = " & txtbxmovID.Value
Sub Main()
StartTime = Now()
TDate = CDate(Format(Now(), "MM/DD/YYYY"))
My = Environ("Username")
Set db = CurrentDb
Set rs = db.OpenRecordset("Emp", dbOpenDynaset)
Criteria = "ID = '" & My & "' And From_Date >= '" & TDate & "' And To_Date <= '" & StartTime & "' "
rs.FindFirst Criteria
If rs.NoMatch Then
MsgBox "Record not found"
else
MsgBox "Record found"
end if
Please help to run this code as I would like to find first record between start date & end date.
For a date you should use a "#" as enclosing character:
Criteria = "ID = '" & My & "' And From_Date >= #" & TDate & "# And To_Date <= #" & StartTime & "# "
You are mixing up date and time formatting:
Sub Main()
' Dim StartTime As Date
' Dim TDate As Date
StartTime = Now
TDate = DateValue(StartTime)
My = Environ("Username")
Set db = CurrentDb
Set rs = db.OpenRecordset("Emp", dbOpenDynaset)
Criteria = "ID = '" & My & "' And From_Date >= #" & Format(TDate, "yyyy\/mm\/dd") & "# And To_Date <= #" & Format(StartTime, "yyyy\/mm\/dd") & "#'"
rs.FindFirst Criteria
If rs.NoMatch Then
MsgBox "Record not found"
Else
MsgBox "Record found"
End If
End Sub
However, it could be reduced to:
Sub Main()
My = Environ("Username")
Set db = CurrentDb
Set rs = db.OpenRecordset("Emp", dbOpenDynaset)
Criteria = "ID = '" & My & "' And From_Date >= Date() And To_Date <= Now()"
rs.FindFirst Criteria
If rs.NoMatch Then
MsgBox "Record not found"
Else
MsgBox "Record found"
End If
End Sub
I use a DatePicker and textfield on a form for the user to select a date and by default it displays in the textfield as dd/mm/yyyy. Therefore, when I wrote my code I used this format to stay consistent. But when I save a date like 03/10/2015 (which is the 3rd day of October) it gets saved as March 10th. Given the following code below, what do I need to change to make the save to the database correctly?
Private Sub cmdSave_Click()
...
Dim StartDate As String
Dim EndDate As String
Dim SDate As Date
Dim EDate As Date
...
StartDate = Me.txtStartDate.Value & " " & Me.txtStartTime.Value
EndDate = Me.txtEndDate.Value & " " & Me.txtEndTime.Value
SDate = CDate(Format(StartDate, "dd\/mm\/yyyy hh:mm"))
EDate = CDate(Format(EndDate, "dd\/mm\/yyyy hh:mm"))
If Me.txtOtherDetails.Value = "" Then
query1 = "INSERT INTO Shifts (Schedule_ID,Start_Date_Time,End_Date_Time,Location)" & _
" VALUES (" & ScheduleID & ",#" & SDate & "#,#" & EDate & "#," & LocationID & ")"
Else
query1 = "INSERT INTO Shifts (Schedule_ID,Start_Date_Time,End_Date_Time,Location,Other_Details)" & _
" VALUES (" & ScheduleID & ",#" & SDate & "#,#" & EDate & "#," & LocationID & ",'" & Me.txtOtherDetails.Value & "')"
End If
'Debug.Print query1
ShiftID = ExecuteInsert(query1)
End Sub
You should change the format of the date in the query to mm/dd/yyyy, since this is the format used in the MS Access queries.
So you should change:
SDate = CDate(Format(StartDate, "mm\/dd\/yyyy hh:mm"))
EDate = CDate(Format(EndDate, "mm\/dd\/yyyy hh:mm"))
This has been completely mixed up.
If your textboxes have been applied a date/time format, they will hold valid date expressions for date values, and these have to be formatted to valid string expressions to be concatenated with the SQL code.
Also, concatenating a date/time value as is with SQL will initially force a cast of the value to a string using the default Windows settings which will fail in a non-US environment for dates of the 1th to the 12th.
Thus, this is all you need:
Private Sub cmdSave_Click()
...
Dim StartDate As String
Dim EndDate As String
...
StartDate = Format(Me!txtStartDate.Value & " " & Me!txtStartTime.Value, "yyyy\/mm\/dd hh\:nn")
EndDate = Format(Me!txtEndDate.Value & " " & Me!txtEndTime.Value, "yyyy\/mm\/dd hh\:nn")
If Me!txtOtherDetails.Value = "" Then
query1 = "INSERT INTO Shifts (Schedule_ID,Start_Date_Time,End_Date_Time,Location)" & _
" VALUES (" & ScheduleID & ",#" & StartDate & "#,#" & EndDate & "#," & LocationID & ")"
Else
query1 = "INSERT INTO Shifts (Schedule_ID,Start_Date_Time,End_Date_Time,Location,Other_Details)" & _
" VALUES (" & ScheduleID & ",#" & StartDate & "#,#" & EndDate & "#," & LocationID & ",'" & Me!txtOtherDetails.Value & "')"
End If
'Debug.Print query1
ShiftID = ExecuteInsert(query1)
End Sub
I'm trying to get a date in my database but when the day is less than 12, the month and the day are switched
Example: In the database 2012-02-10 (2 october 2012), the value I get when I do that:
lastDateMill = Nz(DLookup("LastContactDate", "Mills", "MillID = " & lstMills.Column(0, i)), 0)
is
lastDateMill = "10/02/2012"
So I thought that was only a format thing but when I do
Format(lastDateMill, "Long Date")
it equals "February-10-12"
This is how I update the date
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = #" & SalesCallDate & "# WHERE MillID = " & lstMills.Column(0, i)
And the SalesCallDate = "2/10/2012" so the good date
So why the day and the month are switched?
The front end is ms-access-2010 and the back end is on SQL SERVER 2012
Your SalesCallDate variable contains a date as text:
SalesCallDate = "2/10/2012"
Apparently you intend the date format of that string to be m/d/yyyy, but it gets interpreted as d/m/yyyy format.
Store the string value in yyyy/mm/dd format to eliminate confusion due to locale issues ... 2012/02/10
Since it turns out that SalesCallDate is actually a text box containing a date value, change your UPDATE approach to avoid date problems due to locale.
Dim strUpdate As String
Dim db As DAO.Database
strUpdate = "UPDATE Mills SET LastContactDate = " & _
Format(Me.SalesCallDate, "\#yyyy-m-d\#") & vbCrLf & _
"WHERE MillID = " & Me.lstMills.Column(0, i)
Debug.Print strUpdate
Set db = CurrentDb
db.Execute strUpdate, dbFailonerror
Set db = Nothing
Try this, by explicitly specifying a format
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = #" & _
Format$(SalesCallDate,"yyyy/mm/dd") & "# WHERE MillID = " & _
lstMills.Column(0, i)
UPDATE:
Maybe there is better way to do it, that is independent of any formattings. The idea is to tranfer the date from table to table, without any combo-, list- or text in between. Therefore any conversion from a date type to string and then back to a date field is avoided.
If the tables can be joined (assuming that MillID is the bound field of the listbox):
DoCmd.RunSQL "UPDATE Mills " & _
"INNER JOIN sourceTable ON Mills.MillID = sourceTable.MillID " & _
"SET LastContactDate = sourceTable.SalesCallDate " & _
"WHERE Mills.MillID = " & lstMills
Otherwise
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = " & _
"(SELECT SalesCallDate FROM sourceTable WHERE ID = " & sourceID & ")" _
"WHERE Mills.MillID = " & lstMills