VBA - Create online folder
This is the create folder portion of the code found within MS Access
Private Sub cmdCreateFolder_Click()
'== This command creates owner private folder within owners parent directory
Dim strOwnerID As String
Dim strOwnerName As String
Dim strEmail As String
Dim strPFolder As String
'check for empty email address
If IsNull(Application.Forms("Add/Edit Owner").Controls("txtOwnerEmail")) Then
MsgBox "MailTo cannot be blank. Please enter an email address."
Application.Forms("Add/Edit Owner").Controls("txtOwnerEmail").SetFocus
Exit Sub
End If
'capture email for use in WHERE clause of online dbase INSERT statement
strEmail = Application.Forms("Add/Edit Owner").Controls("txtOwnerEmail")
'capture OwnerID and name for use in naming the folder
strOwnerID = Application.Forms("Add/Edit Owner").Controls("txtOwnerID")
strOwnerName = Application.Forms("Add/Edit Owner").Controls("OwnerName")
'replace spaces in strOwnerName with underscores; FTP will not work with spaces in file/folder name
strOwnerName = Replace(strOwnerName, " ", "_")
'concatenate ID and name to create folder name
strPFolder = strOwnerID & strOwnerName
'The code creates three files.
'First is a text file (.txt) that contains the commands processed by FTP,
'Second is a batch file (.bat) that runs the FTP command
'Third is a marker file (.out) that is created by the batch when FTP has finished processing. 'When this is created, our VBA code detects it and knows it can then carry on with its tidying up routine.
Dim lFnumFtp As Long, lFnumBatch As Long
Dim sFname As String
'turn error trapping on for problems writing to admin.asp
On Error GoTo WriteErr_Handler
'write variable values to admin.aspx
Call WriteFile(sMainDir, strPFolder, strEmail)
'date and name the text file that contains the commands processed by FTP
sFname = sPATH & Format(Now, "yyyy-mm-dd_hhmm") & strPFolder & "-Folder-"
'supply the next file number available for use by the Open function
lFnumFtp = FreeFile
'turn error trapping on for problems creating directory
On Error GoTo Err_Handler
'Create text file with ftp commands
Open sFname & ".txt" For Output As lFnumFtp
Print #lFnumFtp, "open " & sSITE 'open the site
Print #lFnumFtp, sUSER
Print #lFnumFtp, sPASS
Print #lFnumFtp, "binary" 'set file transfer mode
Print #lFnumFtp, "cd " & sDir 'change directory to parent owner directory
Print #lFnumFtp, "mkdir " & strPFolder 'make the new folder
Print #lFnumFtp, "cd " & sMainDir 'change directory to where admin.aspx resides
Print #lFnumFtp, "send "; strDbUpdatePage 'upload admin.asp page containing new arguments for ShellExecute
'to view the FTP session on the command prompt, comment this out and close it manually typing the word "bye"
Print #lFnumFtp, "bye" 'close ftp session
Close lFnumFtp 'close text file
'create the ftp log file command
Dim sLogFile As String
sLogFile = " > " & sPATH & "ftp_createfolder_log.txt"
'supply the next file number available for use
lFnumBatch = FreeFile
'open a batch file
Open sFname & ".bat" For Output As lFnumBatch
Print #lFnumBatch, "ftp -s:" & sFname & ".txt" & sLogFile
Print #lFnumBatch, "Echo ""Complete""> " & sFname & ".out"
Close lFnumBatch
'run the batch file; can view cmd window as minimized, or hidden
'Shell sFname & ".bat", vbMinimizedNoFocus
Shell sFname & ".bat", 0
'yield execution so that the operating system can process other events while ftp is processing
Do While Dir(sFname & ".out") = ""
DoEvents
Loop
'read the log file for success; if success, enter folder name on form
Dim ftplog As String
Dim iFile As Integer
Dim InputData As String
Dim strRetn As String
'declare variable for the created log file
ftplog = sPATH & "ftp_createfolder_log.txt"
'supply the next file number available for use
iFile = FreeFile
Open ftplog For Input Shared As #iFile ' Open file for shared input.
Do While Not EOF(iFile) ' Check for end of file.
Line Input #iFile, InputData ' Read line of data.
strRetn = Left$(InputData, 3) 'Return first 3 characters of line read
Select Case strRetn
Case 220 'connected to server
'Continue
Case 331 'username verified
'Continue
Case 230 'user Logged In
'Continue
Case 250 'change to parent owner directory successful
'Continue
Case 257 'make new child directory successful
'Continue
Case 550 'make new child directory failed
MsgBox "Failed to create this owner's directory. Verify it does not already exist."
Case 221 'closing data connection; requested file action successful
'add folder path to online login.mdb owner record
Call ShellExecute(Me.hwnd, "open", strCommand, vbNullString, vbNullString, SW_SHOWNORMAL)
'enter folder value on form field
Application.Forms("Add/Edit Owner").Controls("txtOwnerOnlinePath") = strPFolder
'Call UpdateRemoteDbase(sDir, strPFolder, strEmail)
Case Else
'MsgBox strRetn 'Left here for testing...
End Select
Loop
Close #iFile ' Close file.
'clean up files used; do not kill the log files since it contents are used in function
'if cleaning the files causes error, ignore and continue processing
On Error Resume Next
Kill sFname & ".bat"
Kill sFname & ".out"
Kill sFname & ".txt"
Here:
'exit procedure to prevent processing of error handler
Exit Sub
WriteErr_Handler:
MsgBox "Write to admin.asp Error : " & Err.Number & vbCrLf & _
"Description : " & Err.Description, vbCritical
Resume Here
Err_Handler:
MsgBox "Create Folder Error : " & Err.Number & vbCrLf & _
"Description : " & Err.Description, vbCritical
Resume Here
End Sub
|