SQL in VBA simple problem

electricjelly

Registered User.
Local time
Yesterday, 19:56
Joined
Apr 3, 2014
Messages
26
Hi I am trying to use an SQL statement to input data into another table but I constantly receive a "invalid procedure call or argument" I have tried several different things to attempt remedying the problem, but nothing seems to solve the issue. I assume that there is something that I am just not seeing because of my inexperienced eyes. Thank you for any help.

The actual code where I receive the error is in red

Code:
Private Sub EmployeeID_AfterUpdate()

On Errror GoTo ErrorHandler

Dim LrecID As Variant
Dim LrecInOUT As Variant
Dim LrecDate As Variant
Dim Ldate As Variant
Dim rstEmployee As DAO.Recordset
Dim dbsNatCat As DAO.Database
Dim strSQL As String
Dim ErrSQL As String
Dim ErrDescription As String
Dim InsSQL As String

    LrecID = DLast("EmployeeID", "QryEmployeeClockIn", "EmployeeID=" & Me.EmployeeID)
    LrecInOUT = DLast("InOut", "QryEmployeeClockIn", "EmployeeID=" & Me.EmployeeID)
    LrecDate = DMax("TimeStamp", "QryEmployeeClockIn", "EmployeeID=" & Me.EmployeeID)
    Ldate = DateAdd("n", 5, LrecDate)
    
        
    'this set of codes is for the declarations for thebigbadwolf
    strSQL = "SELECT * FROM QryEmployeeClockIn ORDER BY EmployeeID, TimeStamp"
    Set dbsNatCat = CurrentDb
    Set rstEmployee = dbsNatCat.OpenRecordset(strSQL, dbOpenDynaset)
    
        
    If LrecInOUT = 0 And Ldate < Now() And LrecDate > Date - 1 Then
        InOut.Value = -1
    ElseIf LrecInOUT = -1 And LrecDate > Date - 1 Then 'And Ldate < Now()
        InOut.Value = 0
        GoTo CalcHours
    ElseIf LrecInOUT = 0 And Ldate > Now() Then
        GoTo TheBigBadWolf
    ElseIf LrecInOUT = -1 And Ldate > Now() Then
        GoTo TheBigBadWolf
    Else: InOut.Value = -1
    End If
 
    TimeStamp = Now()

DoCmd.GoToRecord acForm, "FrmEmployeeClockIn", acNewRec

GoTo ExitSub

CalcHours:
   [SIZE=3][COLOR=Red] DoCmd.RunSQL "INSERT INTO tblhoursworked(FldHoursWorked,FldEmployeeID,FldDateInputted) " _
    & " VALUES ('" & DateDiff(hh, LrecDate, Now()) & "', '" & Me.EmployeeID.Value & "',#" & Now() & "#)"[/COLOR][/SIZE]
    TimeStamp = Now()

'this is where the records are deleted
TheBigBadWolf:
    rstEmployee.Delete
    DoCmd.GoToRecord acForm, "FrmEmployeeClockIn", acNewRec
    GoTo Cleanup

'this code deletes the previous record from thebigbadwolf based on whether not not timestamp is filled
Cleanup:
    DoCmd.SetWarnings False 'will turn off the popup warnings
    DoCmd.RunSQL "DELETE FROM tblEmployeeClockIn " & _
    "WHERE TimeStamp IS NULL;"
    DoCmd.SetWarnings True 'will turn the popup warnings on
    GoTo ExitSub
    
CleanSlate:
    DoCmd.RunCommand acCmdUndo
    Resume ExitSub

ExitSub:
    Exit Sub

ErrorHandler:
    On Error GoTo ErrorHandler2

ErrorHandler2:
'this logs the error into a table without stopping the flow of the form
ErrDescription = Chr(34) & Err.Description & Chr(34)
ErrSQL = "INSERT INTO tblErrorLog (ErrDate, CompName, UsrName, " _
& " ErrNumber, ErrDescription, ErrModule, EmployeeID, TimeInOut)" _
& " VALUES(#" & Now() & "#, '" & Environ("ComputerName") _
& "', '" & CurrentUser & "', " & Err.Number _
& ", " & ErrDescription & ", '" & VBE.ActiveCodePane.CodeModule & "', '" & Me.EmployeeID _
& "', '" & Me.TimeStamp & "')"
Debug.Print ErrSQL

DoCmd.SetWarnings False
DoCmd.RunSQL ErrSQL
DoCmd.SetWarnings True

GoTo CleanSlate
    


End Sub
 
Last edited:
you may have missed a couple of quoation marks

Code:
& ", [SIZE=4][COLOR=red]'[/COLOR][/SIZE]" & strAgeRangeDesc & "[SIZE=4][COLOR=red]'[/COLOR][/SIZE], " &
 
Oh yes!

Sorry - I missed that:banghead:
 
I discovered that the problem lies in the datediff() procedure, I thought I coded it correctly?

DateDiff(h, LrecDate, Now())

Code:
On Errror GoTo ErrorHandler

Dim LrecID As Variant
Dim LrecInOUT As Variant
Dim LrecDate As Variant
Dim Ldate As Variant
Dim rstEmployee As DAO.Recordset
Dim dbsNatCat As DAO.Database
Dim strSQL As String
Dim ErrSQL As String
Dim ErrDescription As String
Dim InsSQL As String
Dim DateSQL As String

    LrecID = DLast("EmployeeID", "QryEmployeeClockIn", "EmployeeID=" & Me.EmployeeID)
    LrecInOUT = DLast("InOut", "QryEmployeeClockIn", "EmployeeID=" & Me.EmployeeID)
    LrecDate = DMax("TimeStamp", "QryEmployeeClockIn", "EmployeeID=" & Me.EmployeeID)
    Ldate = DateAdd("n", 5, LrecDate)
    DateSQL = DateDiff(h, LrecDate, Now())
 
Last edited:
Okay I fixed the issue, I just needed a "" around the h, Thanks Uncle Gizmo and CJ for the help, I did implement the suggestions you gave me as well
 

Users who are viewing this thread

Back
Top Bottom