Solved Run Code

Kha

Member
Local time
Today, 11:51
Joined
Sep 4, 2022
Messages
57
Hello,
I have a code that hides my database, but sometimes it is visible when I perform a compression or repair. How do I instruct the program to run this code inside VBA every time it opens to ensure that the database remains hidden?

attrib +h *.accde*
 
Last edited:
Use Shell command?
Shell("C:\WINDOWS\attrib +h *.accde*", 1)

Can you help me to write the code on the right way please?
 
Last edited:
Hello,
I have a code that hides my database, but sometimes it is visible when I perform a compression or repair. How do I instruct the program to run this code inside VBA every time it opens to ensure that the database remains hidden?

attrib +h +s *.accde*
Hi. Welcome to AWF!

I'm not sure you can hide a file by executing the code to do it from within itself. Maybe? I haven't tried.

Just curious, if you're hiding the database, how are you executing it? If you can execute (open) it, then what's the point of hiding it?
 
Hi. Welcome to AWF!

I'm not sure you can hide a file by executing the code to do it from within itself. Maybe? I haven't tried.

Just curious, if you're hiding the database, how are you executing it? If you can execute (open) it, then what's the point of hiding it?
I make a visible shortcut to the file, and the main file is hidden for protection
 
Public Function Hide_DB()
Shell("C:\WINDOWS\attrib +h *.accde*", 1)
End Function

Help me to build this code in right way
 
Last edited:
I would not worry about the path for attrib. I am pretty sure it is an internal command.?
However you do need to specify the path to the file.

Not sure you can do it on a wildcard, but I tested this on a file without spaces.
With spaces you need to enclose the whole path within double quotes again?

Code:
Sub HideFile()
Dim iRetVal As Integer
iRetVal = Shell("attrib +h +s ""F:\temp\db\test Export.accdb""")
End Sub

I would probably just hide the file you are opening each time, rather than a batch, else use a command or batch file to hide all your accde files.
 
  • Like
Reactions: Kha
I would not worry about the path for attrib. I am pretty sure it is an internal command.?
However you do need to specify the path to the file.

Not sure you can do it on a wildcard, but I tested this on a file without spaces.
With spaces you need to enclose the whole path within double quotes again?

Code:
Sub HideFile()
Dim iRetVal As Integer
iRetVal = Shell("attrib +h +s ""F:\temp\db\test Export.accdb""")
End Sub

I would probably just hide the file you are opening each time, rather than a batch, else use a command or batch file to hide all your accde files.
Thanks, I will try your code
I have Batch file, but I want to make it internal command inside DB
 
I make a visible shortcut to the file, and the main file is hidden for protection
So, if the shortcut is visible, wouldn't the user be able to figure out where the hidden file is by simply looking in the Properties of the shortcut?

In any case, like I said, I'm not sure if you'll be able to use attrib on a file from within itself, but good luck.

Edit: Looks like you were able to achieve it. Good job!
 
Last edited:
  • Like
Reactions: Kha
I would not worry about the path for attrib. I am pretty sure it is an internal command.?
However you do need to specify the path to the file.

Not sure you can do it on a wildcard, but I tested this on a file without spaces.
With spaces you need to enclose the whole path within double quotes again?

Code:
Sub HideFile()
Dim iRetVal As Integer
iRetVal = Shell("attrib +h +s ""F:\temp\db\test Export.accdb""")
End Sub

I would probably just hide the file you are opening each time, rather than a batch, else use a command or batch file to hide all your accde files.
I tried your code it works good,
So, when I publish it to users how do I make it follow the path of where it is? is it possible?
 
I would not worry about the path for attrib. I am pretty sure it is an internal command.?
However you do need to specify the path to the file.

Not sure you can do it on a wildcard, but I tested this on a file without spaces.
With spaces you need to enclose the whole path within double quotes again?

Code:
Sub HideFile()
Dim iRetVal As Integer
iRetVal = Shell("attrib +h +s ""F:\temp\db\test Export.accdb""")
End Sub

I would probably just hide the file you are opening each time, rather than a batch, else use a command or batch file to hide all your accde files.
Sub HideFile()
Dim iRetVal As Integer
Dim Path
Path = CurrentDb.Name
iRetVal = Shell("attrib +h ""Path""")
End Sub

I think CurrentDb.Name will help, can you plz help me to fix this code and make it possible!
 
Last edited:
Start DIMming your variables correctly :(
Try
Code:
Sub HideFile()
Dim iRetVal As Integer
Dim strShell As String
strShell = "attrib +h +s " & CurrentDb.Name & ""
Debug.Print strShell
iRetVal = Shell(strShell)

End Sub
Get used to putting string concatenation into a string variable and DebugPrint that until you get it correct, then either comment out the Debug line or remove it.

The above works for a path with no spaces. I'll let you do some work if the path has spaces. You should have enough now to work it out yourself.

FWIW, I too think you are shutting the barn door after the horse has bolted. The first thing I do when I get a new windows computer is Show file extensions and Show Hidden files. :)

Could just as easy be done in the shortcut that runs a batch file to hide and then open the DB?
 
Last edited:
  • Like
Reactions: Kha
Could just as easy be done in the shortcut that runs a batch file to hide and then open the DB?
Thank you very much, working good!

I use code instead of batch file to Automate the task
 
Thank you very much, working good!

I use code instead of batch file to Automate the task
Well the batch/command file to hide the db and then open it would be automated, would it not?
Glad you got it working anyway.
 
  • Like
Reactions: Kha
The code method will only work if the file is in a trusted location
 
  • Like
Reactions: Kha
you can also use FilesystemObject:
Code:
Public Function fnHideMe()
Const consHidden As Integer = 2
Const consSystem As Integer = 4
With CreateObject("scripting.filesystemobject").getfile(CurrentDb.Name)
    .Attributes = .Attributes Or consHidden Or consSystem
End With
End Function
 
you can also use FilesystemObject:
Code:
Public Function fnHideMe()
Const consHidden As Integer = 2
Const consSystem As Integer = 4
With CreateObject("scripting.filesystemobject").getfile(CurrentDb.Name)
    .Attributes = .Attributes Or consHidden Or consSystem
End With
End Function
Thank you
 

Users who are viewing this thread

Back
Top Bottom