I have this code (in Form_Before_update Event) which checks for duplicate values in sdtCode field:
If DCount("[sdtCode]", "[tbl_sdt_Info]", "[sdtCode] = '" & Me.sdtCode.Value & "'") > 0 Then
Me.Undo
MsgBox "duplicates found"
End If
It works perfectly. However, after I use the following code to link the record to a picture, and when I try to move to another record the fisrst code is triggered and it gives me the "duplicates found" message!!!!
Private Sub sdtPicture_Click()
Dim fd As FileDialog
Dim i As Integer
Dim strSelectedPicture As Variant
Dim strExtension As String
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
'show only set of extension file in dialog
.Filters.Clear
.Filters.Add "Image file", "*.jpeg;*.png;*.jpg;*.gif", 1
If .Show = -1 Then
For Each strSelectedPicture In .SelectedItems
For i = Len(strSelectedPicture) To 1 Step -1
If Mid(strSelectedPicture, i, 1) = "." Then
strExtension = Mid(strSelectedPicture, i)
Exit For
End If
Next i
Me.sdtImagePath.Value = strSelectedPicture
' if folder name doesnt exist then make new one
On Error Resume Next
MkDir "C:\dbImageArchive\students\"
' On Error GoTo 0
'if folder exist, copy image to distination folder
'file name in the drive C:\
FileCopy strSelectedPicture, "C:\dbImageArchive\students\" & "sdt_" & Me.sdtCode & strExtension
Me.sdtPicturePath.Value = "C:\dbImageArchive\students\" & "sdt_" & Me.sdtCode.Value & strExtension
'Add a text box (sdtPictureName) to display the name of the picture file
'Me.sdtPictureName = Me.sdtID & strExtension
Next strSelectedPicture
Else
'display when no file is selected
MsgBox "?? E?II ???C?", vbInformation, ""
End If
Set fd = Nothing
End With
Me.cboOrganizations.SetFocus
'Me.Refresh
End Sub
I tried the Form_after_update event. It produced further problems. Any ideas to solve this issue please. Thank you.
I wrote a script to download files using VBA. The VBA script has to download items that start with https://collaboration.company.corp/collrooms/specificfolder or with \collaboration.company.corp#SSL\DavWWWRoot\collrooms\specificfolder
The specific folders are the same.
If I allow the script to select the specific mapping, it will only recognize it if I use the definition \collaboration.company.corp#SSL\DavWWWRoot\collrooms\specificfolder
How can I create a mapping in VBA to tell Excel that https://collaboration.company.corp/collrooms/specificfolder and \collaboration.company.corp#SSL\DavWWWRoot\collrooms\specificfolder is the same and that the first specification is also valid?
My code:
Option Explicit
Sub FolderSelection()
'Shows the folder picker dialog in order the user to select the folder
'in which the downloaded files will be saved.
Dim FoldersPath As String
'Show the folder picker dialog.
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a folder to save your files..."
.Show
If .SelectedItems.Count = 0 Then
Sheets("Main").Range("B4") = "-"
MsgBox "You did't select a folder!", vbExclamation, "Canceled"
Exit Sub
Else
FoldersPath = .SelectedItems(1)
End If
End With
'Pass the folder's path to the cell. HERE I AM MISSING THE MAPPING. It will show files starting with https if selected and not transfer it to the other structure.
Sheets("Main").Range("B4") = FoldersPath
End Sub
Sub Clear()
'Clears the URLs, the result column and the folder's path.
Dim LastRow As Long
'Find the last row.
With Sheets("Main")
.Activate
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
'Clear the ranges.
If LastRow > 7 Then
With Sheets("Main")
.Range("C8:D" & LastRow).ClearContents
.Range("B4:D4").ClearContents
.Range("B4").Select
End With
End If
End Sub
and the other part of the download macro is
'Check if the folder exists. I did not check whether it will also download with the https structure?
DownloadFolder = sh.Range("B4")
On Error Resume Next
If Dir(DownloadFolder, vbDirectory) = vbNullString Then
MsgBox "The path is incorrect!", vbCritical, "Folder's Path Error"
sh.Range("B4").Select
Exit Sub
End If
On Error GoTo 0
I tried with a script that I found on Stackoverflow but it does not work
I created an additional module:
Sub test()
Dim dm As New DriveMapper
Dim sharepointFolder As Scripting.Folder
Set sharepointFolder = dm.MapDrive("https://collaboration.company.corp/collrooms/")
' unsure whether I have to add something here and whether this will work with https
Debug.Print sharepointFolder.Path
End Sub
and added the following WebDAV mapping as a new CLASS
Option Explicit
Private oMappedDrive As Scripting.Drive
Private oFSO As New Scripting.FileSystemObject
Private oNetwork As New WshNetwork
Private Sub Class_Terminate()
UnmapDrive
End Sub
Public Function MapDrive(NetworkPath As String) As Scripting.Folder
Dim DriveLetter As String, i As Integer
UnmapDrive
For i = Asc("Z") To Asc("A") Step -1
DriveLetter = Chr(i)
If Not oFSO.DriveExists(DriveLetter) Then
oNetwork.MapNetworkDrive DriveLetter & ":", NetworkPath
Set oMappedDrive = oFSO.GetDrive(DriveLetter)
Set MapDrive = oMappedDrive.RootFolder
Exit For
End If
Next i
End Function
Private Sub UnmapDrive()
If Not oMappedDrive Is Nothing Then
If oMappedDrive.IsReady Then
oNetwork.RemoveNetworkDrive oMappedDrive.DriveLetter & ":"
End If
Set oMappedDrive = Nothing
End If
End Sub
The question is also whether removing the dispose method "Class_Terminate" which unmaps the drive would help? When the class goes out of scope then the drive get's unmapped. And how I could put it all together.
I am trying to automate the tasks I would normally run through to compact my database, save backups, and update revision numbers for an automatic update system I am using. I am stuck on trying to make an accde file with a vba script.
Everything I find pertaining to the subject seems to point to using something like this.
function MakeACCDE(InPath As String, OutPath As String)
Dim app As New Access.Application
app.AutomationSecurity = msoAutomationSecurityLow
app.SysCmd 603, InPath, OutPath
End Function
A few users on various forums claim that this code works for them but I have not had any luck. My database runs the code without errors, but nothing actually happens.
Is there a particular piece of syntax I am not using or maybe something with the format of the file paths?
I found the following code at: http://www.experts-exchange.com/questions/28429044/How-do-I-create-an-Access-2010-accde-from-VBA.html
I inserted into my Access 2010 accdb, ran it, and it created an accde
**UPDATE: Seeing you want to run from a different DB, I tested that also... just change the line 'tmpDB_Full_Name = CurrentProject.FullName' to be your source database
Option Compare Database
Option Explicit
Function Create_MDE()
Dim tmpDB_Full_Name As String
Dim tmpDB_Name As String
Dim tmpDB_Backup_Full_Name As String
Dim tmpCopy_File As Variant
Dim tmpDirectory As String
'Call SetStartupOptions("AllowBypassKey", dbBoolean, False) '---This runs a procedure to deactivate the Shift & F11 key
'tmpDB_Full_Name = CurrentProject.FullName
tmpDB_Full_Name = "C:\data\access\MyDb.accdb"
tmpDirectory = CurrentProject.Path
tmpDB_Name = CurrentProject.Name
tmpDB_Backup_Full_Name = tmpDirectory & "\" & left(tmpDB_Name, Len(tmpDB_Name) - 6) & "-Backup.accdb"
'this removes a file created on the same day
If Dir(tmpDB_Backup_Full_Name) <> "" Then
Kill tmpDB_Backup_Full_Name
End If
'this creates a backup into destination tmpDirectory
If Dir(tmpDB_Backup_Full_Name) = "" Then
Set tmpCopy_File = CreateObject("Scripting.FileSystemObject")
tmpCopy_File.CopyFile tmpDB_Full_Name, tmpDB_Backup_Full_Name, True
End If
Dim app As New Access.Application
app.AutomationSecurity = msoAutomationSecurityLow
app.SysCmd 603, tmpDB_Backup_Full_Name, tmpDirectory & "\" & left(tmpDB_Name, Len(tmpDB_Name) - 9) & ".accde"
'Call SetStartupOptions("AllowBypassKey", dbBoolean, True) '---This runs a procedure to activate the Shift & F11
MsgBox ("Compile Complete!")
End Function
I have prepared a ready-made solution that creates an ACCDE file and at the same time allows you to protect it with a password. With frequent updates, it makes my life so much easier. I tested it on Microsoft Access 2016 and 2019.
The function SaveAccdbAsAccde() performs the following steps:
compiles and saves changes to the database
copies the database to '...(~temp~).ACCDB'
creates the file '...(~temp~).ACCDE'
if everything worked, it sets a password to the database and copies it as the target file
deletes working files
To protect the database with a password, do the following: SaveAccdbAsAccde("password")
I used a few functions that might also come in handy for other tasks:
helper functions based on Scripting.FileSystemObject for handling files use : CopyFile(),DeleteFile(),FileExists()
functions to secure / unsecure the database with a password EncryptDb() and DecryptDb()
All details below:
Option Explicit
'------------------------------------------------------------------------------------
'main function
Public Sub SaveAccdbAsAccde(Optional filePassword As String)
On Error Resume Next
Application.RunCommand acCmdCompileAndSaveAllModules
err.Clear
If err <> 0 Then MsgBox "Save changes in forms and reports before preparing the ACCDE file.": Exit Sub
On Error GoTo err_proc
Dim strFile0 As String, strFile1 As String, strFile2 As String, strFile3 As String
strFile0 = CurrentDb.Name
strFile1 = Replace(CurrentDb.Name, ".accdb", "(~temp~).accdb")
strFile2 = Replace(CurrentDb.Name, ".accdb", "(~temp~).accde")
strFile3 = Replace(CurrentDb.Name, ".accdb", ".accde")
If Not DeleteFile(strFile1) Then MsgBox "Can't felete file: " & strFile2: Exit Sub
If Not CopyFile(strFile0, strFile1) Then MsgBox "Can't copy file: " & strFile0 & " na " & strFile1: Exit Sub
If Not DeleteFile(strFile2) Then MsgBox "Can't delete file: " & strFile2: Exit Sub
MakeACCDESysCmd strFile1, strFile2
If Not FileExists(strFile2) Then MsgBox "Can't create file: " & strFile2: Exit Sub
If Not DeleteFile(strFile3) Then MsgBox "Can't delete file: " & strFile3: Exit Sub
EncryptDb strFile2, strFile3, filePassword
If Not FileExists(strFile3) Then MsgBox "Can't create file: " & strFile3: Exit Sub
If Not DeleteFile(strFile2) Then MsgBox "Can't delete file: " & strFile2: Exit Sub
If Not DeleteFile(strFile1) Then MsgBox "Can't delete file: " & strFile2: Exit Sub
MsgBox "Done: " & strFile3
exit_proc:
Exit Sub
err_proc:
MsgBox err.Description, vbCritical, "Error"
Resume exit_proc
End Sub
'------------------------------------------------------------------------------------
Public Sub EncryptDb(strSourcePath As String, strDestPath As String, pwd As String)
If pwd <> "" Then pwd = ";pwd=" & pwd
DBEngine.CompactDatabase strSourcePath, strDestPath, dbLangGeneral & pwd, dbVersion167, pwd
End Sub
Public Sub DecryptDb(strSourcePath As String, strDestPath As String, pwd As String)
If pwd <> "" Then pwd = ";pwd=" & pwd
DBEngine.CompactDatabase strSourcePath, strDestPath, dbLangGeneral & ";pwd=", dbVersion167, pwd
End Sub
Public Function MakeACCDESysCmd(InPath As String, OutPath As String)
Dim app As Access.Application
Set app = New Access.Application
app.AutomationSecurity = 1 'msoAutomationSecurityLow - Enables all macros. This is the default value when the application is started.
app.SysCmd 603, InPath, OutPath 'an undocumented action
app.Quit acQuitSaveNone
Set app = Nothing
End Function
'------------------------------------------------------------------------------------
Public Function CopyFile(strFromFile, strToFile)
On Error Resume Next
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
err.Clear
objFSO.CopyFile strFromFile, strToFile, True
CopyFile = err = 0
Set objFSO = Nothing
End Function
Public Function DeleteFile(strFile)
If Not FileExists(strFile) Then DeleteFile = True: Exit Function
On Error Resume Next
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
err.Clear
objFSO.DeleteFile strFile, True
DeleteFile = err = 0
Set objFSO = Nothing
End Function
Public Function FileExists(strFile)
On Error Resume Next
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
FileExists = objFSO.FileExists(strFile)
Set objFSO = Nothing
End Function
I have tested the following code in Access 2016 using ACCDE and ACCDR as the destination file extension:
Dim otherAccess As Access.Application
Set otherAccess = New Access.Application
otherAccess.AutomationSecurity = 1 'msoAutomationSecurityLow
otherAccess.SysCmd 603, InPath, OutPath
otherAccess.Quit acQuitSaveNone
Set otherAccess = Nothing
My goal is to export excel file to txt file format. The idea is to has a GUI to let user select the excel file that she/he wish to export and she/he can decide which file path and file name to save. Once users has finish input and output setting, he/she just need to click Export text button to export the excel file to txt file and save in the location that he/she has decided. The GUI as below
I've a macro to convert excel file to txt format
Private Sub ConvertToText()
ActiveWorkbook.SaveAs FileName:="C:\Projects\ExelToText\Text.txt", FileFormat:=xlCurrentPlatformText, CreateBackup:=False
End Sub
My question is how could I pass value from FileInput and FileOutput as variable to above macro instead harcode the filepath. Appreciate your helps and if you have any better suggestions, please share it out. Thanks
Below is the full source code
Private Sub ReadButton_Click()
OpenWorkbookUsingFileDialog
End Sub
------------------------------
Private Sub WriteButton_Click()
WriteWorkbookUsingFileDialog
End Sub
------------------------------
Private Sub ExportButton_Click()
ConvertToText
End Sub
------------------------------
Private Sub OpenWorkbookUsingFileDialog()
Dim fdl As FileDialog
Dim FileName As String
Dim FileChosen As Integer
Set fdl = Application.FileDialog(msoFileDialogFilePicker)
fdl.Title = "Please Select a Excel File"
fdl.InitialFileName = "c:\"
fdl.InitialView = msoFileDialogViewSmallIcons
fdl.Filters.Clear
fdl.Filters.Add "Excel Files", "*.xlsx; *.xls"
FileChosen = fdl.Show
If FileChosen <> -1 Then
MsgBox "You have choosen nothing"
ReadTextBox = Null
Else
MsgBox fdl.SelectedItems(1)
FileName = fdl.SelectedItems(1)
ReadTextBox = FileName
End If
End Sub
-----------------------------------
Private Sub WriteWorkbookUsingFileDialog()
Dim file_name As Variant
file_name = Application.GetSaveAsFilename( _
FileFilter:="Text Files,*.txt,All Files,*.*", _
Title:="Save As File Name")
If file_name = False Then Exit Sub
If LCase$(Right$(file_name, 4)) <> ".txt" Then
file_name = file_name & ".txt"
End If
WriteTextBox = file_name
End Sub
----------------------------
Private Sub ConvertToText()
ActiveWorkbook.SaveAs FileName:="C:\Projects\ExelToText\Text.txt",FileFormat:=xlCurrentPlatformText, CreateBackup:=False
End Sub
Make it so your subroutine ConvertToText requires a file path/string argument:
Private Sub ConvertToText(sourcePath as String, destPath as String)
Dim wb as Workbook
Set wb = Workbooks.Open(sourcePath)
wb.SaveAs FileName:=destPath,
FileFormat:=xlCurrentPlatformText, CreateBackup:=False
wb.Close
End Sub
Then, make small modifications to your ExportButton to send this parameter to the ConvertToText sub:
Private Sub ExportButton_Click()
On Error Resume Next
ConvertToText Me.TextBox1.Value, Me.TextBox2.Value 'Modify this so that it refers to the TextBoxes on your form
If Err.Number <> 0 Then
MsgBox "Unable to convert file. Please ensure a valid file was entered.", vbCritical
End If
On Error GoTo 0
End Sub
I have been working on translating the code below from VBA to VB.Net. I am about 90% of the way there but I am stuck on 3 issues.
First, In VBA I used the Application.GetOpenFilename to open a file dialog from within my code, I can't seem to get that done on VB.
Second, I am trying to export a sheet and copy the data onto a sheet on my workbook. I use the following line of code:
With Globals.ThisWorkbook.Application.ActiveWorkbook.Open(strImportFile)
.Worksheets(1).Cells.Copy Workbooks(strSourceFile).Worksheets(sDestSheet).Range("A1")
.Close(savechanges:=False)
Unfortunately, the middle line is not working the Workbooks gives me an error that it cannot be used as type.
And finally, I am trying to get the Application ScreenUpdating, but that also fails. Here is the rest of my code:
Imports Microsoft.Office.Interop.Excel
Module adminModule
Function GetImportFile(Index As Long) As String
'This function is used in the import_Module to name all of the files
'that will be imported to the template. The function is primarily used by
'Sub import_OC_Data
Select Case Index
Case 1 : GetImportFile = "byemployee.csv"
Case 2 : GetImportFile = "byposition.csv"
Case 3 : GetImportFile = "statusreport.xls"
Case 4 : GetImportFile = "bydepartment.csv"
End Select
Return ""
End Function
Function GetDestSheet(Index As Long) As String
'This function is used in the import_Module to name all of the sheets
'where the files will be imported to in template.The function is primarily used by
'Sub import_OC_Data
Select Case Index
Case 1 : GetDestSheet = "byDepartment"
Case 2 : GetDestSheet = "byPosition"
Case 3 : GetDestSheet = "statusReport"
Case 4 : GetDestSheet = "byDepartment"
End Select
Return ""
End Function
Sub importRawData()
Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
Dim n As Long
Dim strSourceFile As String
Dim strImportFile As String
Dim sDestSheet As String
Dim strTitle As String = "Verify Source File"
Dim strPrompt As String = " source file does not exist." & vbNewLine & "Press OK to browse for the file or Cancel to quit"
Dim strAlert As String = ("You have not selected a workbook." & vbNewLine & "Press Retry to select a workbook or Cancel to exit program")
Dim strVmbProceedResults As String = ("Procedure Canceled. Your workbook will now close")
Dim vmbContinue As MsgBoxResult
Dim vmbProceed As MsgBoxResult
strSourceFile = Globals.ThisWorkbook.Application.ActiveWorkbook.Na
For n = 1 To 4 Step 1
strImportFile = xlWBPath & GetImportFile(n)
sDestSheet = GetDestSheet(n)
If Len(Dir(strImportFile)) > 0 Then
With Globals.ThisWorkbook.Application.ActiveWorkbook.Open(strImportFile)
.Worksheets(1).Cells.Copy Workbooks(strSourceFile).Worksheets(sDestSheet).Range("A1")
.Close(savechanges:=False)
End With
Else
vmbProceed = MsgBox(strImportFile & strPrompt, vbOKCancel + vbQuestion, strTitle)
If vmbProceed = vbCancel Then
vmbProceed = MsgBox(strVmbProceedResults, vbOKOnly + vbCritical)
Globals.ThisWorkbook.Close(saveChanges:=False)
Exit Sub
Else
strImportFile = Application.GetOpenFilename("Excel Files (*.xls;*.xlsx; *.xlsm; *.csv), *.xls; *.csv; *.xlsx; *.xlsm")
If strImportFile = "False" Then Application.ScreenUpdating = True
vmbContinue = MsgBox(strAlert, vbRetryCancel + vbCritical, "No Workbook Selected")
If vmbContinue = vbCancel Then
Globals.ThisWorkbook.Close(saveChanges:=False)
Exit Sub
Else
strImportFile = Application.GetOpenFilename("Excel Files (*.xls;*.xlsx; *.xlsm; *.csv), *.xls; *.csv; *.xlsx; *.xlsm")
Globals.ThisWorkbook.Application.ActiveWorkbook.Open(Filename:=strImportFile)
With Globals.ThisWorkbook.Application.ActiveWorkbook.Open(strImportFile)
.Worksheets(1).Cells.Copy Workbooks(strSourceFile).Worksheets(sDestSheet).Range("A1")
.Close(saveChanges:=False)
End With
End If
On Error GoTo exit_
Application.ScreenUpdating = False
Globals.ThisWorkbook.Application.ActiveWorkbook.Open(Filename:=strImportFile)
With Globals.ThisWorkbook.Application.ActiveWorkbook.Openn(strImportFile)
.Worksheets(1).Cells.Copy Workbooks(strSourceFile).Worksheets(sDestSheet).Range("A1")
.Close(savechanges:=False)
End With
exit_:
Application.ScreenUpdating = True
If Err() Then MsgBox(Err.Description, vbCritical, "Error")
End If
End If
Next n
End Sub
End Module