I'm trying to write to a file using a combination of DoCmd.TransferText (to a temp file) and DOS commands. Problem is, the lines are getting written out of order!
Here's a snippet (this is actually part of a loop):
Here's where it gets really bad: if I try to kill the temp file (after appending it to the main file), I lose everything but the headers.
I understand that Shell is asynchronous, so I tried replacing the calls to Shell with CmdWait, where CmdWait is as follows:
Supposedly, the OpenProcess/WaitForSingleObject technique forces synchronization, but it did not seem to help in this case.
I am not sure if DoCmd is asynchronous (I couldn't find any info on this). But oddly enough, I do seem to be getting all the data as long as I omit the Kill. It's just that the headers are appearing in random order.
Can anyone tell me what I am doing wrong?
Here's a snippet (this is actually part of a loop):
Code:
strCmd = "cmd /c echo " & strHdr & " >> " & strPath
Shell (strCmd)
DoCmd.TransferText acExportDelim, , tblWhatever, strTmp
strCmd = "cmd /c type " & strTmp & " >> " & strPath
Shell (strCmd)
'Kill strTmp ' this seems to thwart the TransferText
Here's where it gets really bad: if I try to kill the temp file (after appending it to the main file), I lose everything but the headers.
I understand that Shell is asynchronous, so I tried replacing the calls to Shell with CmdWait, where CmdWait is as follows:
Code:
Public Sub CmdWait(cmd As String)
Dim pid As Long
Dim phnd As Long
pid = Shell(cmd)
DoEvents
phnd = OpenProcess(SYNCHRONIZE, 0, pid)
If phnd <> 0 Then
WaitForSingleObject phnd, INFINITE
CloseHandle phnd
End If
End Sub
Supposedly, the OpenProcess/WaitForSingleObject technique forces synchronization, but it did not seem to help in this case.
I am not sure if DoCmd is asynchronous (I couldn't find any info on this). But oddly enough, I do seem to be getting all the data as long as I omit the Kill. It's just that the headers are appearing in random order.
Can anyone tell me what I am doing wrong?