Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some simple help with some VB code

Status
Not open for further replies.

Gaffi1

Technical User
Apr 23, 2004
70
US
I have a db that has an automatic scheduling feature. Basically, we input what projects we want to schedule and for how many hours and run this script and it does the schedule for us.

Code:
Private Sub Command131_Click()

    If [Forms]![SchedulingParametersForm].[ShifttoSchedule] <> "" Then

    DoCmd.SetWarnings False
    Beep
    DoCmd.RunCommand acCmdRefresh
    MsgBox "Please be patient, this may take a moment.  Thank you for your patience.", vbInformation, "Creating Schedule"
    ' Close Previous Schedule Output/Edit Form
    DoCmd.Close acTable, "Schedule", acSaveYes
    ' Day to Schedule Selection
    DoCmd.OpenQuery [Forms]![SchedulingParametersForm].[ShifttoSchedule], acViewNormal, acEdit
    ' Project 1 Sub
    DoCmd.OpenQuery "SchedulingPart4", acViewNormal, acEdit
    ' Project 1 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 1 Hours Main
    DoCmd.OpenQuery "SchedulingHours1", acViewNormal, acEdit
    ' Project 1 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 2 Sub
    DoCmd.OpenQuery "SchedulingPart41", acViewNormal, acEdit
    ' Project 2 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 2 Hours Main
    DoCmd.OpenQuery "SchedulingHours11", acViewNormal, acEdit
    ' Project 2  Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 3 Sub
    DoCmd.OpenQuery "SchedulingPart42", acViewNormal, acEdit
    ' Project 3 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 3 Hours Main
    DoCmd.OpenQuery "SchedulingHours12", acViewNormal, acEdit
    ' Project 3 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 4 Sub
    DoCmd.OpenQuery "SchedulingPart43", acViewNormal, acEdit
    ' Project 4 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 4 Hours Main
    DoCmd.OpenQuery "SchedulingHours13", acViewNormal, acEdit
    ' Project 4 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 5 Sub
    DoCmd.OpenQuery "SchedulingPart44", acViewNormal, acEdit
    ' Project 5 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 5 Hours Main
    DoCmd.OpenQuery "SchedulingHours14", acViewNormal, acEdit
    ' Project 5 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 6 Sub
    DoCmd.OpenQuery "SchedulingPart45", acViewNormal, acEdit
    ' Project 6 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 6 Hours Main
    DoCmd.OpenQuery "SchedulingHours15", acViewNormal, acEdit
    ' Project 6 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 7 Sub
    DoCmd.OpenQuery "SchedulingPart46", acViewNormal, acEdit
    ' Project 7 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 7 Hours Main
    DoCmd.OpenQuery "SchedulingHours16", acViewNormal, acEdit
    ' Project 7 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 8 Sub
    DoCmd.OpenQuery "SchedulingPart47", acViewNormal, acEdit
    ' Project 8 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 8 Hours Main
    DoCmd.OpenQuery "SchedulingHours17", acViewNormal, acEdit
    ' Project 8 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 9 Sub
    DoCmd.OpenQuery "SchedulingPart48", acViewNormal, acEdit
    ' Project 9 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 9 Hours Main
    DoCmd.OpenQuery "SchedulingHours18", acViewNormal, acEdit
    ' Project 9 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 10 Sub
    DoCmd.OpenQuery "SchedulingPart49", acViewNormal, acEdit
    ' Project 10 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 10 Hours Main
    DoCmd.OpenQuery "SchedulingHours19", acViewNormal, acEdit
    ' Project 10 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 11 Sub
    DoCmd.OpenQuery "SchedulingPart410", acViewNormal, acEdit
    ' Project 11 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 11 Hours Main
    DoCmd.OpenQuery "SchedulingHours110", acViewNormal, acEdit
    ' Project 11 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 12 Sub
    DoCmd.OpenQuery "SchedulingPart411", acViewNormal, acEdit
    ' Project 12 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 12 Hours Main
    DoCmd.OpenQuery "SchedulingHours111", acViewNormal, acEdit
    ' Project 12 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 13 Sub
    DoCmd.OpenQuery "SchedulingPart412", acViewNormal, acEdit
    ' Project 13 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 13 Hours Main
    DoCmd.OpenQuery "SchedulingHours112", acViewNormal, acEdit
    ' Project 13 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 14 Sub
    DoCmd.OpenQuery "SchedulingPart413", acViewNormal, acEdit
    ' Project 14 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 14 Hours Main
    DoCmd.OpenQuery "SchedulingHours113", acViewNormal, acEdit
    ' Project 14 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    ' Project 15 Sub
    DoCmd.OpenQuery "SchedulingPart414", acViewNormal, acEdit
    ' Project 15 Hours Sub
    DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
    ' Project 15 Hours Main
    DoCmd.OpenQuery "SchedulingHours114", acViewNormal, acEdit
    ' Project 15 Main
    DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
        ' Project Briefing
    DoCmd.OpenQuery "SchedulingBriefing", acViewNormal, acEdit
    DoCmd.OpenForm "Schedule", acNormal, "", "", , acNormal
    DoCmd.Close acForm, "SchedulingParametersForm"
        Else:
            MsgBox "You must select a shift to make a schedule for."
            [Forms]![SchedulingParametersForm].[ShifttoSchedule].SetFocus
            
End If

End Sub

We would like to input code to skip to the end of the script if there is no project/hours entered in a field to be scheduled so that the auto scheduler runs a bit faster. Where you see the switches from project 1, project 2, etc, the first query is pulling which project to schedule and for how many hours from the form. So, the script would have to check after each project field to see if there is anything else needing to be scheduled.

Any suggestions? I'm fairly new to VB and my attempts so far to code this have been unsuccesful (the autoscheduler fails to work propertly with my attempts to put in if/thens...).

Thanks for your help!
 
Well, you already have an If Statement in there.
Are you sure, you're not capable of the syntax?

A slight digression...
I, personally, have a "bad" habit, of reducing code to as few lines as possible.

I'd like to do it here, as a suggestion.

Code:
Dim x as Integer
Dim y As Variant

 If [Forms]![SchedulingParametersForm].[ShifttoSchedule] <> "" Then

    DoCmd.SetWarnings False
    Beep
    DoCmd.RunCommand acCmdRefresh
    MsgBox "Please be patient, this may take a moment.  Thank you for your patience.", vbInformation, "Creating Schedule"
    ' Close Previous Schedule Output/Edit Form
    DoCmd.Close acTable, "Schedule", acSaveYes
    ' Day to Schedule Selection
    DoCmd.OpenQuery [Forms]![SchedulingParametersForm].[ShifttoSchedule], acViewNormal, acEdit




For x = 0 to 14
    If projectField = ? Then
     If x = 0 Then y = "" Else y = x
       ' Project 1 Sub
        DoCmd.OpenQuery "SchedulingPart4" & y, acViewNormal, acEdit
       ' Project 1 Hours Sub
        DoCmd.OpenQuery "SchedulingHoursSub", acViewNormal, acEdit
        ' Project 1 Hours Main
        DoCmd.OpenQuery "SchedulingHours1" & y, acViewNormal, acEdit
         ' Project 1 Main
         DoCmd.OpenQuery "SchedulingPart5", acViewNormal, acEdit
    End If
Next



    ' Project Briefing
    DoCmd.OpenQuery "SchedulingBriefing", acViewNormal, acEdit
    DoCmd.OpenForm "Schedule", acNormal, "", "", , acNormal
    DoCmd.Close acForm, "SchedulingParametersForm"
        Else:
            MsgBox "You must select a shift to make a schedule for."
            [Forms]![SchedulingParametersForm].[ShifttoSchedule].SetFocus
            
End If

End Sub

You see my If statement, I have no idea what I'm checking but, it's in the right position to circumvent any unnecessary processing, project by project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top