PSLine2000Documentation/Forms/SetupPaths.md

4.0 KiB

SetupPaths


Record Source

  • None

Controls

Control Name Reference
None -

VBA Code

Option Compare Database   'Use database order for string comparisons

Private Sub Button6_Click()
   Sheet1path$ = Trim$(itsaNull$(Shear1Path))
   Sheet2path$ = Trim$(itsaNull$(Shear2Path))
   Initforms$ = Trim$(itsaNull$(InitForm))
   Open ShearsName For Output As #1
      Print #1, Sheet1path$
      Print #1, Sheet2path$
      Print #1, Initforms$
   Close #1
   DoCmd.Close
End Sub

Private Sub Button7_Click()
   DoCmd.Close
End Sub

Private Sub Form_Open(Cancel As Integer)
   On Error GoTo ErrorRead
   Open ShearsName For Input As #1
      Line Input #1, i$: Sheet1path$ = i$
      Line Input #1, i$: Sheet2path$ = i$
      Line Input #1, i$: Initforms$ = i$
   Close #1
   Shear1Path = Trim$(Sheet1path$)
   Shear2Path = Trim$(Sheet2path$)
   InitForm = Trim$(Initforms$)
Exit Sub

ErrorRead:
   i$ = ""
Resume Next

End Sub

What it does

VBA Code Description

Overview

This VBA code is written for an Access database application and appears to be part of a form's event handler. The code handles two button clicks: Button6_Click and Button7_Click. Additionally, the Form_Open subroutine is defined to handle the form's opening event.

Button6_Click Subroutine


This subroutine is triggered when the Button6 control is clicked. Its primary function is to:

  1. Trim and validate input strings: The code trims any leading or trailing whitespace from three string variables: Sheet1path, Sheet2path, and Initforms$. The trim operation is performed using the Trim()` function.
  2. Open a file for output: A file named ShearsName is opened in output mode (For Output As #1) using the DoCmd.OpenObject method.
  3. Write input strings to the file: The trimmed string values are written to the file using the Print #1, StringValue statement.
  4. Close the file: After writing all the values, the file is closed using the Close #1 statement.

Button7_Click Subroutine


This subroutine is triggered when the Button7 control is clicked. Its primary function is to:

  1. Close the current form: The code uses the DoCmd.Close method to close the current form.

Form_Open Subroutine


This subroutine is called when the form is opened, either by double-clicking on its icon or programmatically using another VBA script. Its primary function is to:

  1. Handle potential errors: If an error occurs during file input/output operations, the code jumps to the ErrorRead label and sets a default value (i$ = "") for the Sheet1path, Sheet2path, or Initforms$ variables.
  2. Open the file for input: A file named ShearsName is opened in input mode (For Input As #1) using the DoCmd.OpenObject method.
  3. Read and trim input strings: The code reads three line inputs from the file, assigning them to the corresponding variables (i$ = Sheet1path, i$ = Sheet2path, etc.). The trimmed values are stored in the original variable names.
  4. Update form controls: After reading all the values, the code updates three form control variables: Shear1Path, Shear2Path, and InitForm by assigning the trimmed string values.

ErrorRead Label


If an error occurs during file input/output operations, the code jumps to this label. Here, the value of i$ is set to an empty string (""). The Resume Next statement then skips to the next instruction (in this case, Line Input #1, i$: ...) to try reading from the next line in the file.

Best Practices

The code could benefit from additional error handling and input validation. For example:

  • Use DoCmd.OpenRecordset instead of DoCmd.OpenObject for more flexibility.
  • Implement a loop to handle multiple lines of data, rather than assuming only one line is available.
  • Validate user input (e.g., check if the file exists or if it's in the correct format) before attempting to read from it.