Previous Topic

Next Topic

Book Contents

Book Index

Script to Assign Next Job Number

This example teaches you how to write a script to start a new job, assign the next number, save the job, then edit it. We'll also check:

If your client works with Job Costing this script provides a way to generate the next (numeric) Job code.

  1. Record the basics:

    Dim frmJCJob1 as Object
    frmJCJob1 = CreateObject("Accredo.JCJobForm")
    frmJCJob1.Find("ASHENG REFIT")
    frmJCJob1.Find("700010")
    frmJCJob1.Insert

  2. We can tidy this up by editing the specific match for "ASHENG REFIT". Replace "ASHENG REFIT" with ":".

    Dim frmJCJob1 as Object
    frmJCJob1 = CreateObject("Accredo.JCJobForm")
    frmJCJob1.Find(":")
    frmJCJob1.Find("1010")
    frmJCJob1.Insert

  3. We can tidy this up further by editing the specific match for "1010", and instead going to the previous record. Replace the entire line with frmJCJob1.Prev:

    Dim frmJCJob1 as Object
    frmJCJob1 = CreateObject("Accredo.JCJobForm")
    frmJCJob1.Find(":")
    frmJCJob1.Prev
    frmJCJob1.Insert

  4. Then we want to pick up the last numeric job number, turn it into a number, and add 1, before we insert the new job. We'll pop that into a variable called MyJob.

    Dim frmJCJob1 as Object
    frmJCJob1 = CreateObject("Accredo.JCJobForm")
    frmJCJob1.Find (":")
    frmJCJob1.Prev
    MyJob = val(frmJCJob1.JobCode) + 1
    frmJCJob1.Insert

  5. Finally, after inserting, we want to set the JobCode to MyJob. Let's also document what the script does in the first line. This script adds a new job and calculates the next job number:

    'Add a new job and calculate the next job number

    Dim frmJCJob1 as Object
    frmJCJob1 = CreateObject("Accredo.JCJobForm")
    frmJCJob1.Find (":")
    frmJCJob1.Prev
    MyJob = val(frmJCJob1.JobCode) + 1
    frmJCJob1.Insert
    frmJCJob1.JobCode = MyJob

    Save this with the file name StartNewJob.pfs. Run the code.

  6. This works nicely but we might want to take it further. If lots of people are going to be entering Jobs you might want to save the new Job and then edit it, so the Job number has been incremented if someone else runs the script - otherwise you could get two users trying to add the same Job code. This script does that:

    'Add a new job and calculate the next job number

    Dim frmJCJob1 as Object
    frmJCJob1 = CreateObject("Accredo.JCJobForm")
    frmJCJob1.Find (":")
    frmJCJob1.Prev
    MyJob = val(frmJCJob1.JobCode) + 1
    frmJCJob1.Insert
    frmJCJob1.JobCode = MyJob
    frmJCJob1.Save
    frmJCJob1.Edit

  7. As this script stands, it will always open a new Job maintenance form, even if the user is already on a Job form. We could check to see if they already have one open and re-use it. This script does this then adds a new job and calculates the next job number:

    'Add a new job and calculate the next job number

    Dim frmJCJob1 as Object
    frmJCJob1 = GetActiveObject
    If IsNull(frmJCJob1) or frmJCJob1.ClassName <> "JCJobForm" Then ' Can we reuse the current form
      frmJCJob1 = CreateObject("Accredo.JCJobForm")             ' If not open a new one
    End If
    frmJCJob1.Find (":")
    frmJCJob1.Prev
    MyJob = val(frmJCJob1.JobCode) + 1
    frmJCJob1.Insert
    frmJCJob1.JobCode = MyJob
    frmJCJob1.Save
    frmJCJob1.Edit

  8. That's better but what if they had been editing the Job they had open? We really need to test whether they are in Edit mode and cancel the script with a message if they are. This script also adds a new job and calculates the next job number:

    'Add a new job and calculate the next job number

    Dim frmJCJob1 as Object
    frmJCJob1 = GetActiveObject
    If IsNull(frmJCJob1) or frmJCJob1.ClassName <> "JCJobForm" Then ' Can we reuse the current form
      frmJCJob1 = CreateObject("Accredo.JCJobForm")             ' If not open a new one
    ElseIf frmJCJob1.ClassName = "JCJobForm" And frmJCJob1.Editing Then
      Abort("Current Job is being edited. Insert cancelled. Please save changes and re-try.")
    End If
    frmJCJob1.Find (":")
    frmJCJob1.Prev
    MyJob = val(frmJCJob1.JobCode) + 1
    frmJCJob1.Insert
    frmJCJob1.JobCode = MyJob
    frmJCJob1.Save
    frmJCJob1.Edit

  9. Finally, if we want to make job codes a mixture of an incremented number and some validated user suffix, we can prompt for input from the user and select that what they give us meets our rules. This script prompts for a Job Type of Contract, In House, One Off, Repairs or Specials:

    'Add a new job and calculate the next job number

    Dim frmJCJob1 as Object
    frmJCJob1 = GetActiveObject
    If IsNull(frmJCJob1) or frmJCJob1.ClassName <> "JCJobForm" Then ' Can we reuse the current form
      frmJCJob1 = CreateObject("Accredo.JCJobForm")             ' If not open a new one
    ElseIf frmJCJob1.ClassName = "JCJobForm" And frmJCJob1.Editing Then
      Abort("Current Job is being edited. Insert cancelled. Please save changes and re-try.")
    End If
    frmJCJob1.Find (":")
    frmJCJob1.Prev
    MyJob = val(frmJCJob1.JobCode) + 1
    MyType = Left(InputList("Please select job type","Job Type","Contract","In House", _
    "One Off", "Repairs","Specials"),1)
    If IsNull(MyType) then Abort("Cancelled by user.")
    frmJCJob1.Insert
    frmJCJob1.JobCode = MyJob & MyType
    frmJCJob1.Save
    frmJCJob1.Edit

  10. Save the script with the name StartNewJob.pfs.