你需要你的路徑與您的數據庫作爲參數結合到MS Access。
string dbPath = @"c:\MyProgram.mdb";
string pathToAccess = @"C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE";
ProcessStartInfo psiMDB = new ProcessStartInfo(pathToAccess);
psiMDB.Arguments = dbPath + " /cmd 1";
Process p = Process.Start(psiMDB);
也不要忘記附上/cmd
你把下一個參數之前。
附加VBA代碼讀取啓動參數
Option Compare Database
Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW"() As Long
Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)
Sub PrintStartupArguments()
MsgBox CmdLineToStr()
End Sub
Function CmdLineToStr() As String
Dim Buffer() As Byte
Dim StrLen As Long
Dim CmdPtr As Long
CmdPtr = GetCommandLine()
If CmdPtr > 0 Then
StrLen = lstrlenW(CmdPtr) * 2
If StrLen > 0 Then
ReDim Buffer(0 To (StrLen - 1)) As Byte
CopyMemory Buffer(0), ByVal CmdPtr, StrLen
CmdLineToStr = Buffer
End If
End If
End Function
如果您想通過使用C#與VBA中的變量進行交互,您應該閱讀[Office Interop對象](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/如何對接入辦公onterop對象)。這將要求您重寫問題中發佈的所有代碼,並可能在此期間修復您的錯誤 –