Ok MKoslof, I FINALLY got it to work :) ! Well not completely. I got your code to work. Mine is still not perfect. Like I said before, I am trying to do all of this from a console app. When I run the app from the command line (I open up a command window, change directories to my VB workspace, and run my executable), it goes to the next line, and then it echos the input path I specified at the command line as a parameter. Then it just gets hung up there and freezes. I have to kill 2 processes in the task manager, "msaccess.exe", and "mde.exe" (my executable name). Furthermore, before killing "msaccess.exe" while it was frozen, I tried to double click on the database I was using as an input to open it. When I did, it prompted me with the file dialog window, asking me what name do I want to save my MDE as. After killing the 2 processes, I get the "Done" msgbox (you'll see in my code below) and the command prompt comes back to life.
I am assuming something is not right with the "SendKeys" in my console app. Seeing the echo on the next line makes me think the "SendKeys" is going to the DOS window instead of the Access window.
Here is the code I am using. Any ideas?
Option Explicit
Private Declare Function GetStdOutHandle Lib "kernel32" Alias "GetStdHandle" _
(Optional ByVal HandleType As Long = -11) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, _
ByVal lpBuffer As String, ByVal cToWrite As Long, ByRef cWritten As Long, _
Optional ByVal lpOverlapped As Long) As Long
Private Function ConsoleWrite(sText As String) As Long
WriteFile GetStdOutHandle, ByVal sText, Len(sText), ConsoleWrite
End Function
Public Sub Main()
Dim arg As String
Dim x As Object
Dim strFileIn As String
Dim strFileOut As String
On Error Resume Next
arg = Command()
strFileIn = arg
'strFileIn = "d:\temp\db1_02.mdb"
' get the output file to kill if it exists
strFileOut = Left(strFileIn, Len(strFileIn) - 1)
strFileOut = strFileOut + "e"
Kill strFileOut
GenerateMDEFile (strFileIn)
MsgBox "Done"
End Sub
Public Function FileExists(ByVal FileSpec As String) As Boolean
On Error Resume Next
FileExists = (GetAttr(FileSpec) And vbDirectory) = vbNormal
End Function
Function GenerateMDEFile(MyPath As String)
On Error Resume Next
Dim NAcc As Access.Application
Dim MDEName As String
Set NAcc = New Access.Application
MDEName = Left$(MyPath, Len(MyPath) - 4) & ".mde"
If Not FileExists(MDEName) Then
SendKeys MyPath & "{Enter}{Enter}"
SendKeys "{Enter}"
NAcc.DoCmd.RunCommand acCmdMakeMDEFile
Else
Exit Function
End If
Set NAcc = Nothing
End Function