I am working on a time ticket database and am having trouble with the form / VBScript. I wanted to make a 'smart' form (my first mistake :banghead
, but I am having a lot of trouble for something that should be really simple.
I've attached a zip archive including a picture of the table, and the form for reference.
What I am trying to do:
UPDATE table WHERE job#, Department#, Operation#, Employee# are the same as the form values; AND there is no entry in the stop {date/time} field. [Implying that this is still an open job ticket, and we are now closing said job ticket.] That last bit is important since multiple tickets are acceptable (ex: starting a job Monday and finishing Tuesday is 2 [or more] records; 1 per start/stop.)
Current problems:
I can't get the stop {date/time} field to filter for blank/empty/null entries. If I have ... WHERE ... AND ([stop]=NULL OR [stop]=''); It simply skips all entries and inserts a new record. Without that line it finds the last record and updates it correctly, but this is incorrect. It updates previously closed entries instead of opening new ones.
EDIT: I solved this by making sure the table stop field defaults to NULL; and changing the Query to [stop] IS NULL; I've updated the code below to reflect the fixes so anyone searching in the future can have a reference to a functional insert/update structure -- ugly as it may be.
Other Issues:
I tried putting in "Job's Finished" for comment and BOOM Debug Window. Is there a VBScript way to ensure entries with single/double quote craziness does not explode the database?
Edit: Fixed this by using the VBScript Replace function
Side Note:
Is there a cleaner way to code this? It's pretty ugly, but the only way I knew of doing it.
OnClick Code: (Fixed Up as Noted Above)

I've attached a zip archive including a picture of the table, and the form for reference.
What I am trying to do:
UPDATE table WHERE job#, Department#, Operation#, Employee# are the same as the form values; AND there is no entry in the stop {date/time} field. [Implying that this is still an open job ticket, and we are now closing said job ticket.] That last bit is important since multiple tickets are acceptable (ex: starting a job Monday and finishing Tuesday is 2 [or more] records; 1 per start/stop.)
Current problems:
I can't get the stop {date/time} field to filter for blank/empty/null entries. If I have ... WHERE ... AND ([stop]=NULL OR [stop]=''); It simply skips all entries and inserts a new record. Without that line it finds the last record and updates it correctly, but this is incorrect. It updates previously closed entries instead of opening new ones.
EDIT: I solved this by making sure the table stop field defaults to NULL; and changing the Query to [stop] IS NULL; I've updated the code below to reflect the fixes so anyone searching in the future can have a reference to a functional insert/update structure -- ugly as it may be.
Other Issues:
I tried putting in "Job's Finished" for comment and BOOM Debug Window. Is there a VBScript way to ensure entries with single/double quote craziness does not explode the database?
Edit: Fixed this by using the VBScript Replace function
Side Note:
Is there a cleaner way to code this? It's pretty ugly, but the only way I knew of doing it.
OnClick Code: (Fixed Up as Noted Above)
Code:
Private Sub Button_Save_Click()
Dim dbs As DAO.Database, sql As String, rCount As Integer
Dim reasonTXT As String, notesTXT As String
Set dbs = CurrentDb
' fix string data
If Not IsNull([reason].Value) Then
reasonTXT = Replace(Replace([reason].Value, "'", "''"), "chr(34)", "chr(34)chr(34)")
Else
reasonTXT = ""
End If
If Not IsNull([notes].Value) Then
notesTXT = Replace(Replace([notes].Value, "'", "''"), "chr(34)", "chr(34)chr(34)")
Else
notesTXT = ""
End If
' Verify data integrity
If False Then
' Place holder for checks
' All is good, so run query
Else
' Try to UPDATE values
' -- Consider adding check for comments/notes exist before overwriting them.
' -- Alternatively, make those fields only for closing jobs.
sql = "UPDATE [JOB_TICKET] SET "
sql = sql & "made=" & [made].Value
sql = sql & ", scrap=" & [scrap].Value
sql = sql & ", reason='" & reasonTXT
sql = sql & "', notes='" & notesTXT
sql = sql & "', stop='" & Now() & "'"
sql = sql & " WHERE [job_id]=" & [Job].Value
sql = sql & " AND [department_id]=" & [Department].Value
sql = sql & " AND [operation_id]=" & [Operation].Value
sql = sql & " AND [employee_id]=" & [Employee].Value
sql = sql & " AND [stop] IS NULL;"
' Debug Message Box
'MsgBox (sql)
' Run SQL
dbs.Execute sql, dbFailOnError
rCount = dbs.RecordsAffected
If rCount > 0 Then
' Update Successful, Reset the form
Else
' No Fields Updated, Insert new data
sql = " INSERT INTO [JOB_TICKET]"
sql = sql & " (job_id, department_id, operation_id, employee_id, made, scrap, reason, notes, start) VALUES("
sql = sql & [Job].Value & ", "
sql = sql & [Department].Value & ", "
sql = sql & [Operation].Value & ", "
sql = sql & [Employee].Value & ", "
sql = sql & [made].Value & ", "
sql = sql & [scrap].Value & ", '"
sql = sql & reasonTXT & "', '"
sql = sql & notesTXT & "', '"
sql = sql & Now() & "');"
' Debug Message Box
'MsgBox (sql)
dbs.Execute sql, dbFailOnError
rCount = dbs.RecordsAffected
If rCount > 0 Then
' Success - reset form
Else
' Failure - print message
MsgBox ("Save Error - Please verify the data is correct and try again. If another failure occurs, please contact the system administrator or assistance.")
End If
End If
End If
End Sub
Attachments
Last edited: