Dcount function - ms-access

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

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

Displaying account operations by dates

I need to verify the operations done in an account at a particular period of time by asking the user to enter account number and the date range, but each time I run it I have the error "type mismatch"
Here is the code:
Private Sub cmdSearch_Click()
Call search
End Sub
Sub search()
Dim strCriteria, strCount, task As String
Me.Refresh
If IsNull(Me.compte_hist) Or IsNull(Me.date_deb) Or IsNull(Me.date_fin) Then
MsgBox "s'il vous plaƮt assurez-vous que tous les champs sont remplis", vbInformation, "Date Range Required"
Me.compte_hist.SetFocus
Else
strCriteria = "([Date_operation]>= #" & Me.date_deb & "# And [Date_operation] <= #" & Me.date_fin & "#)"
strCount = "[Compte]=#" & Me.compte_hist & "#"
task = "select * from Operations where Operations (" & strCriteria & ")" And " (" & strCount & ") order by [Date_operation]"
DoCmd.ApplyFilter task
End If
End Sub
Try this:
strCriteria = "([Date_operation]>= #" & Format(Me.date_deb, "mm\/dd\/yyyy") & "# And [Date_operation] <= #" & Format(Me.date_fin, "mm\/dd\/yyyy") & "#)"
strCount = "[Compte]=" & Me.compte_hist
task = "select * from Operations where (" & strCriteria & ") And (" & strCount & ") order by [Date_operation]"
Me.RecordSource = task
Also you can apply filter only:
strCriteria = "([Date_operation]>= #" & Format(Me.date_deb, "mm\/dd\/yyyy") & "# And [Date_operation] <= #" & Format(Me.date_fin, "mm\/dd\/yyyy") & "#)"
strCount = "[Compte]=" & Me.compte_hist
task = "(" & strCriteria & ") And (" & strCount & ")"
Me.Filter = task
Me.FilterOn = True
If account number is not numeric, use quotes:
strCount = "[Compte]='" & Me.compte_hist & "'"

day and month reversed when saving to database

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

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

Dates are not updating through sql code in vba

I want to update serial.Issuedate getting from form but its giving syntax error.
Please help me how can I correct this error.
My code is below:
Private Sub Command30_Click()
Set serialrs = CurrentDb.OpenRecordset("serial")
Dim Idate As Date
Dim Itodo As String
Idate = Me.IssuedDate.Value
Itodo = Me.IssuedToDO.Value
Dim issueqry As String
issueqry = "UPDATE serial " _
& " set serial.IssueToDO = '" & Itodo & "'" _
& " serial.issuedate = (#" & Format(Idate, "mm\/dd\/yyyy") & "#)" _
& " WHERE (((serial.id) Between 1 And 10)) "
DoCmd.RunSQL issueqry
MsgBox ("Issued Done")
End Sub
When you update more than one field, you must include a comma between the field expressions like this ...
SET [field name] = "foo", [another field] = 17
^
here
So try your code like this ...
issueqry = "UPDATE serial " _
& " set serial.IssueToDO = '" & Itodo & "'," _
& " serial.issuedate = #" & Format(Idate, "mm/dd/yyyy") & "#" _
& " WHERE serial.id Between 1 And 10"
Also give yourself an opportunity to inspect the string the code built ...
Debug.Print issueqry
You can view the output from Debug.Print in the Immediate window. Ctrl+g will take you there.