day and month reversed when saving to database - ms-access

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

Related

When I run this code, it is counting every time, however it is not resetting for the month or the year

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

Dcount function

I want to count the call number, Datecall, [Username] is the name of fields of table BCKHDY but why numbercall always equal 0. If I delete AND DateCall= #" & DateFrom & "#, code run, that mean there is something wrong with Datecall. What 's wrong?
Private Sub txtnbCall_Click()
Dim mydept As Integer
DateFrom = Me.txtfrom.Value
User = Forms![Navigation form]![txtLogin].Value
If Not IsNull(DLookup("Deptname", "tblUser", "UserLogin = '" & User & "'")) Then
mydept = DLookup("Deptname", "tblUser", "UserLogin = '" & User & "'")
Me.txtnbCall = numbercall(mydept, DateFrom)
End If
End Sub
Public Function numbercall(ByVal mydept As Integer, _
ByVal DateFrom As Date) As Integer
numbercall = DCount("CompanyName", "BCKHDY", _
"[UserName] = " & mydept & "AND DateCall >= #" & DateFrom & "#")
End Function
You're missing a space here:
mydept & "AND
Should be
mydept & " AND
Only spaces inside a string count. If you forget the space, the criteria would include something like 1And
You also need to format the date as either yyyy-MM-dd or MM/dd/yyyy:
"[UserName] = " & mydept & " AND DateCall >= #" & Format(DateFrom, "yyyy-MM-dd") & "#")
You don't have to call DLookup twice, do declare all variables, and you probably filter on the wrong field in DCount:
Private Sub txtnbCall_Click()
Dim mydept As Variant
Dim DateFrom As Date
Dim User As String
DateFrom = Me!txtfrom.Value
User = Forms![Navigation form]![txtLogin].Value
mydept = DLookup("Deptname", "tblUser", "UserLogin = '" & User & "'")
If Not IsNull(mydept) Then
Me!txtnbCall.Value = numbercall(mydept, DateFrom)
End If
End Sub
Public Function numbercall(ByVal mydept As Integer, _
ByVal DateFrom As Date) As Integer
numbercall = DCount("*", "BCKHDY", _
"[Deptname] = " & mydept & " AND DateCall >= #" & Format(DateFrom, "yyyy\/mm\/dd") & "#")
End Function

Can't find what is wrong with the code

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

How can i detect conflict in time in vb 6.0 and ms access database in a Class scheduling system

Function RoomInUse() As Boolean
Dim room As String
Dim day As String
Dim tmein As Date
Dim tmeout As Date
Dim mond As String
Set rs = New ADODB.Recordset
With rs
mond = "select * from tblsched where room Like '" & Combo2.Text & "%' and day like '" & Combo3.Text & "' and (tmein <= #" & Combo1 & "# And " & _
"tmeout >= #" & Combo1 & "#) Or (#" & Combo1 & "#" & _
"<= tmein And tmeout < #" & Combo8 & "#) Or (#" & _
Combo1 & "# <= tmein And tmein < #" & Combo8 & "#)) " '"
.Open mond, con, 3, 3
End With
If rs.RecordCount >= 1 Then
RoomInUse = True
Else
RoomInUse = False
End If
End Function
What I want is if there is already a schedule in a room for example ROOM 1 in 7:00 AM - 9:00 AM in monday .then i add new schedule in the same room then in the time 8:00 AM - 9:30 AM the same day also then.the second record will not be save because there is still session in that room (7:00-9:00) it is not over yet so i want that there must be msgbox that tells the room is still occupied.
Translation(?): Don't allow conflicts in scheduling with overlapping time.
I use this function to detect conflicts between two date ranges (returns true if conflicting, and false otherwise):
Public Function interlapDate(start1 As Date, end1 As Date, start2 As Date, end2 As Date) As Boolean
'Credits to Martin Fowler's Range Pattern Algorithm
interlapDate = end1 >= start2 And end2 >= start1
End Function
See article here
And to put that into perspective, you may use something like:
Private Function roomIsAvailable() as Boolean
Dim strQuery as string
Dim rs as New ADODB.Recordset
Dim newTimeIn as Date
Dim newTimeOut as Date
'Initialize
roomIsAvailable = True
'Assuming from ur sample code that combo1 and combo2 are the user-input range
newTimeIn = TimeValue(CDate(combo1))
newTimeOut = TimeValue(CDate(combo2))
strQuery = "SELECT time_start, time_end" & _
" FROM tbl_sched" & _
" WHERE room LIKE '" & Combo2.Text & "'" & _
" AND day LIKE '" & Combo3.Text & "'"
rs.open strQuery, con, 3, 3
Do While Not rs.EOF
'Compare new range to each range saved in database
If interlapDate(rs!time_start, rs!time_end, newTimeIn, newTimeOut) Then
GoTo conflictFound
Exit Do
End If
rs.moveNext
Loop
Exit Function
conflictFound:
Msgbox "Overlap found!",vbExclamation
roomIsAvailable = False
End Function

Dlookup retrieves a date wrong

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