How can I convert a ms access database to ACCDE?

Amr Ashraf

Member
Joined
Apr 5, 2022
Messages
14
Programming Experience
Beginner
Hello Everyone , I use this code to open a password protected access database :
Open DB:
Microsoft.Office.Interop.Access.Application AccApp = new Microsoft.Office.Interop.Access.Application();
string MyFile = Environment.CurrentDirectory + "\\Baseet.accdb";

AccApp.Visible = true;
AccApp.OpenCurrentDatabase(MyFile, false, "017014A");
AccApp.RunCommand(AcCommand.acCmdAppMaximize);

I need two things , First to add the current location to access Trusted Locations .
The other thing is to convert this database to ACCDE , In this point I've tried this :
Convert:
Microsoft.Office.Interop.Access.Application AccApp = new Microsoft.Office.Interop.Access.Application();
string MyFile = Environment.CurrentDirectory + "\\Baseet.accdb";
string MyCompiledFile = Environment.CurrentDirectory + "\\Baseet.accde";

AccApp.Visible = true;
AccApp.OpenCurrentDatabase(MyFile, false, "017014A");
AccApp.RunCommand(AcCommand.acCmdAppMaximize);

AccApp.SysCmd (603 , MyFile , MyCompiledFile);

But I got this compiler error on 603 :
Cannot convert from int to Microsoft.Office.Interop.Access.AcSysCmdAction
How can I make it work , Any help appreciated .
 
Solution
Here's some information on, as I suspected, undocumented functionality of the SysCmd function, including this one. The issue here is the fact that C# is a strictly-typed language and that function is defined as having a first parameter of type Microsoft.Office.Interop.Access.AcSysCmdAction so not providing an argument of that type is a compilation error. It doesn't mean that the code wouldn't work if executed but it does mean that the code can't execute because it can't compile. You could try casting the number as that type but I have my doubts that that would work:
C#:
AccApp.SysCmd((Microsoft.Office.Interop.Access.AcSysCmdAction)603, MyFile, MyCompiledFile);
That should compile but it may throw an...
What command are you trying to perform? The documentation for that Microsoft.Office.Interop.Access.AcSysCmdAction enumeration is here. I don't see a value that corresponds to 603.
 
I got it from VBA and works there that command convert accdb to accde , I didn't find it in .Net but it should be somewhere .
This is the working code in VBA :
Visual Basic:
Function ConvertToAccde()
Dim sourcedb, targetdb, nametargetdb As String
Dim MyPath As String
MyPath = Application.CurrentProject.Path
sourcedb = MyPath & "\Baseet.accdb"
targetdb = "D:\tt2.accde"
nametargetdb = MyPath & "\Baseet.accde"
Dim accessApplication As Access.Application
Set accessApplication = New Access.Application
With accessApplication
.SysCmd 603, sourcedb, targetdb
End With
Kill sourcedb
Name targetdb As nametargetdb
FollowHyperlink nametargetdb
DoCmd.Quit
End Function
 
You need to find out what that 603 actually means and that would appear to be a VBA question rather than a C# question. It may be some undocumented functionality because even the VBA documentation seems to suggest that the corresponding line of VBA code is invalid.
 
Sort of :) , I know two facts
1- what it means in VBA and works already .
2-I know that I can execute any VBA command using C# . Am I wrong ?
 
I found this talking about the mentioned command :
"To compile an Access Database file to its compiled counterpart with VBA, you can use the SysCmd -Function with the undocumented AcSysCmdAction ‘603’. This action might be undocumented, but it is around since Access 97 and it works very reliable in all versions of Microsoft Access up to Access 2016."
 
Here's some information on, as I suspected, undocumented functionality of the SysCmd function, including this one. The issue here is the fact that C# is a strictly-typed language and that function is defined as having a first parameter of type Microsoft.Office.Interop.Access.AcSysCmdAction so not providing an argument of that type is a compilation error. It doesn't mean that the code wouldn't work if executed but it does mean that the code can't execute because it can't compile. You could try casting the number as that type but I have my doubts that that would work:
C#:
AccApp.SysCmd((Microsoft.Office.Interop.Access.AcSysCmdAction)603, MyFile, MyCompiledFile);
That should compile but it may throw an InvalidCastException, given that that value isn't a valid member of that enumeration. In that case, I think your only option would be to use a less strictly-typed language, like VB.NET. With Option Strict Off, it should allow you to pass a number where an Enum is expected where C# will not.
 
Solution
Here's some information on, as I suspected, undocumented functionality of the SysCmd function, including this one. The issue here is the fact that C# is a strictly-typed language and that function is defined as having a first parameter of type Microsoft.Office.Interop.Access.AcSysCmdAction so not providing an argument of that type is a compilation error. It doesn't mean that the code wouldn't work if executed but it does mean that the code can't execute because it can't compile. You could try casting the number as that type but I have my doubts that that would work:
C#:
AccApp.SysCmd((Microsoft.Office.Interop.Access.AcSysCmdAction)603, MyFile, MyCompiledFile);
That should compile but it may throw an InvalidCastException, given that that value isn't a valid member of that enumeration. In that case, I think your only option would be to use a less strictly-typed language, like VB.NET. With Option Strict Off, it should allow you to pass a number where an Enum is expected where C# will not.
That was very helpful , Thank you so much.
 
Back
Top Bottom