Can I keep reusing a file system object or do I need to create multiple objects? (1 Viewer)

JMongi

Active member
Local time
Today, 07:57
Joined
Jan 6, 2021
Messages
802
Nice catch! I've done some syntax checking and debugging and whatnot. Syntax-wise everything is good. But I'm running into some array issues. I had some variable scope issues that I've sorted out with the help of some msgbox calls. Anywho...here is the main question:

Code:
Dim arr
arr = Array (strA, strB, strC, strD)

This does not assign the string values of those variables to the array locations.

Rich (BB code):
Dim arr(3)
arr(0) = strA
..
arr(3) = strD

This also does not result in any values in the array.

Is there something I need to do in VBScript to get the string out of the variable and into the array?

I have verified thtat strA, strB, etc have the appropriate strings assigned before I try to populate my array.
 

JMongi

Active member
Local time
Today, 07:57
Joined
Jan 6, 2021
Messages
802
It appears to do nothing at all.

If I MsgBox arr(0) I get nothing (Msgbox doesn't call)
If I try to step through the array using a For Loop, the for loop doesn't even start (I have msgbox's embedded in the for loop).

If I change from using variables to using actual strings by adding quotes around all of the variable names, then everythign works correctly (as far as assigning values to the array).
 

cheekybuddha

AWF VIP
Local time
Today, 12:57
Joined
Jul 21, 2014
Messages
2,280
The following works for me:
Code:
Dim a, b, c, arr, i

a = "hello"
b = "world"
c = "!"

arr = Array(a, b, c)

For i = 0 To 2
  MsgBox arr(i)
Next
Saved as array_test.vbs and double-clicked, I get 3 MsgBox'es

Perhaps show your code in context.
 

JMongi

Active member
Local time
Today, 07:57
Joined
Jan 6, 2021
Messages
802
Wow, not you got me thinking I"ve inadvertently done something weird. Hmmm....
Ok, retyped out your sample code and it worked as expected.
Now that I know that is WILL work, I can focus on why it's not working. Let me take a deep breath and find the probably stupid, obvious mistake I made :ROFLMAO:
 
Last edited:

JMongi

Active member
Local time
Today, 07:57
Joined
Jan 6, 2021
Messages
802
Yep, stupid mistake!
Whatever I had fixed another time introduced a typo in one of my variable names. Since that variable name didn't exist, setting the array just failed (without an error) and there you have it.
 

JMongi

Active member
Local time
Today, 07:57
Joined
Jan 6, 2021
Messages
802
So, to the best of my knowledge, this code works!

Rich (BB code):
'====================================================================
' Project: Company Operations Database (Office365 - Access 2019)
' Title: Company Operations Install Script
' Filename: CompanyOpInstall.vbs
' Creation Date: 09/26/2022
' Revision Date: -
' Author: jmongi
' Purpose: Installs the Company Operations Application
' Acknowledgments: Thanks to the users of AccessWorld Forums at www.access-programmers.co.uk for help with
'                    development and testing.  Users include but are not limited to theDBguy, Isaac, isladogs,
'                    gasman, cheekybuddha, arnelgp, Minty
' Module List
' --LocationChk:    Verify and create directory locations
' --RuntimeChk:     Checks that MS Access runtime is installed
' --FileXfer:        Copy files from server location to local user
' --WriteLog:        Writes a string to a specified text log file
' --ErrHandler:        Manage errors that occur
'=====================================================================================================
Option Explicit
On Error Resume Next                                        'errors will not halt script, see ErrHandler sub
Dim sModuleName                                                'Used with ErrHandler
sModuleName = "Main Script"

'Set Constants
Const cIcon = "MainApp.ico"                                                'Shortcut Icon
Const cSCName = "CompanyOperations"                                        'Shortcut name
Const cServerPath = "\\SERVER\PersonalFolders\ProductionFE\"             'Front End Server Path
Const cLocalApp = "\CompanyOp\"                                            'Local App Folder Name
Const cLocalFE = "FE\"                                                    'Front End Local Folder Name
Const cLocalLog = "Log\"                                                'Application Log Local Folder Name
Const cLocalArchive = "Archive\"                                        'Applicaton Archive Local Folder Name
Const cScriptName = "CompanyOpLaunch.vbs"                                 'Name of this script

'Scriptwide varirables
Dim sLocalUser
Dim sLocalApp
Dim sLocalFE
Dim sLocalLog
Dim sLocalArchive
Dim sIconLoc

Dim oShell
Set oShell = CreateObject("WScript.Shell")
Call ErrHandler(sModuleName)

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
Call ErrHandler(sModuleName)

'Set user variables
sLocalUser = oShell.ExpandEnvironmentStrings("%AppData%")
sLocalApp = sLocalUser & cLocalApp
sLocalFE = sLocalUser & cLocalApp & cLocalFE
sLocalLog = sLocalUser & cLocalApp & cLocalLog
sLocalArchive = sLocalUser & cLocalApp & cLocalArchive
sIconLoc = sLocalFE & cIcon

Call ErrHandler (sModuleName)

Call LocationChk
Call FileXfer
Call CreateSC
MsgBox "Installation Successful!"

WScript.Quit
' Subroutine Modules
'===================================================================================================
Sub RuntimeChk
sModuleName = "RuntimeChk"
'Some code to check Access runtime
End Sub
'====================================================================================================
Sub ErrHandler (ByVal ErrModule)    'Custom error handler for VBScript
Dim sError
If Err.Number <> 0 Then
    'Store the error
    sError = "Error No:" & Err.Number & " - " & Err.Description & " occurred in module " & ErrModule
    MsgBox sError
    Err.Clear
    
    'Notify the user of the error.
    MsgBox "An error has occurrred installing the program.  The installation will attempt to recover. " _
        & "If the error occurs again, please contact your system administrator."
End If
End Sub
'=====================================================================================================
Sub LocationChk ()  ' Check if installation folders exist and create them if they do not exist
sModuleName = "LocationChk"

Dim i
Dim aNewFolder

aNewFolder = Array(sLocalApp, sLocalFE, sLocalLog, sLocalArchive)

For i = 0 To 3
    If Not oFSO.FolderExists(aNewFolder(i)) Then
        MsgBox aNewFolder(i)
        oFSO.CreateFolder (aNewFolder(i))
    End If
Next
Call ErrHandler (sModuleName)
End Sub
'=====================================================================================================
Sub FileXfer ()        'Transfer new files from shared network location to local user
sModuleName = "FileXfer"
Dim Source, Destination

Source = cServerPath & "*.*"
Destination = sLocalFE

oFSO.CopyFile Source, Destination, True        'The true flag suppresses the user prompt for overwrite
Call ErrHandler (sModuleName)

End Sub
'=====================================================================================================
Sub CreateSC ()
sModuleName = "CreateSC"
    
Dim sDesktopPath, sSCPath, sSCTarget
Dim link

sDesktopPath = oShell.SpecialFolders("Desktop")
sSCPath = sDesktopPath & "\" & cSCName & ".lnk"
sSCTarget = sLocalFE & cScriptName

If Not oFSO.FileExists(sSCPath) Then
    Set link = oShell.CreateShortcut(sSCPath)
        link.TargetPath = sSCTarget
        link.Description = cSCName
        link.IconLocation = sIconLoc & ", 0"
        link.Save
End If

Call ErrHandler(sModuleName)

End Sub
 
Last edited:

JMongi

Active member
Local time
Today, 07:57
Joined
Jan 6, 2021
Messages
802
Replaced old code with new code that works. Tried to anonymize the code too.
Thanks to all who helped!
 

cheekybuddha

AWF VIP
Local time
Today, 12:57
Joined
Jul 21, 2014
Messages
2,280
Oh yeah! Missed it! o_O

Probably hidden by the On Error Resume Next.
 

JMongi

Active member
Local time
Today, 07:57
Joined
Jan 6, 2021
Messages
802
Ah...should have commented that out for debugging. That would have saved me a headache!
Other thing that burned me was shortuct creation. Three different example code blocks did not instantiate the shortcut object (but did for the shell). That's enough for it to be intentional, right? Wrong!
Created the shortcut object (like I thought should be there) and voila, no more issues! *sigh*
 

JMongi

Active member
Local time
Today, 07:57
Joined
Jan 6, 2021
Messages
802
Well, it's not doing much heavy lifting, but my two scripts seem to be functioning and error free!
One is an install script that does the initial FE file copying and creates a desktop shortcut.
The second script is the database launch script which copies the FE on each launch and writes a log file.
I can roll out additonal functionality in the future but I feel good to get the basic deployment structure in place.
 

Users who are viewing this thread

Top Bottom