Solved

Bad File Name and Invalid Procedure Call

Posted on 1998-09-30
5
245 Views
Last Modified: 2008-02-20
I have been getting bad file name in my program when I open
a text file and write.

Do you have an idea why I get Invalid Procedure Call when I use the SHELL.  My problem is kind of intermetent because most of the time SHELL is able to function well.
0
Comment
Question by:MokMok
5 Comments
 
LVL 14

Expert Comment

by:waty
Comment Utility
could you post your code, so I could take a look.
0
 
LVL 14

Expert Comment

by:waty
Comment Utility
could you post your code, so I could take a look.
0
 
LVL 2

Expert Comment

by:swilt
Comment Utility
I hope this helps

1) Use freefile
2) Make sure Open statement and the Write or Print use the same file handle
3) Put a break point on the Open to make sure it is only run when you expect it to be
4) Put DoEvents after Close just to make sure the fiel closes properly

Are you sure the shell is causing the problem (it could be the line after the shell).
if it is the line after then try looping with DoEvents to give the shelled appliction time to start up

to write to a file:

dim fh as integer
dim i as integer

fh = freefile
open "c:\myfile.txt" for output as #fh
for i = 1 to 10
   print #fh, "Line " & i
next i
close #fh
doevents


to read a file:

Dim fh as integer
dim buf as string

fh = freefile
open "c:\myfile.txt" for input as #fh
while not eof(fh)
    line input #fh,buf
    msgbox buf
wend
close #fh
doevents


0
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
Comment Utility
The most likely reason is that your path name is not valid.  Check that the path and file are correct by double checking using Windows Explorer.  

A possibility is that you are using 16 bit VB and trying to access a 32 bit path (long path name).
0
 

Author Comment

by:MokMok
Comment Utility
HELLO.  HERE ARE THE FORM MAIN AND SOME MODULES THAT SOMEHOW MIGHT CAUSE THE PROBLEM.  I HOPE YOU CAN HELP ME.  THIS IS SUPPOSED TO BE A SCHEDULER AND IT IS SUPPOSED TO RUN 24 HOURS A DAY BUT SINCE I AM EXPERIENCING THE INVALID PROCEDURE CALL PROBLEM, IT HANGS.

THIS IS MY MODULE WHERE I PUT ALL MY VARIABLES

'function declaration for the Win32 APIs
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function EnumProcesses Lib "psapi" (IpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long
Declare Function EnumProcessModules Lib "psapi" (ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long
Declare Function GetModuleBaseName Lib "psapi" Alias "GetModuleBaseNameA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpBaseName As String, ByVal nSize As Long) As Long
Declare Function GetModuleBaseNameW Lib "psapi" (ByVal hProcess As Long, ByVal hModule As Long, lpBaseName As Byte, ByVal nSize As Long) As Long
Declare Function GetModuleFileNameEx Lib "psapi" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Declare Function GetModuleFileNameExW Lib "psapi" (ByVal hProcess As Long, ByVal hModule As Long, lpFileName As Byte, ByVal nSize As Long) As Long
Declare Function GetModuleInformation Lib "psapi" (ByVal hProcess As Long, ByVal hModule As Long, lpmodinfo As MODULEINFO, ByVal cb As Long) As Long
Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

'Constants for the databases
Public Const DBDIR = "T:\LineView\DBs\Sked.Mdb"
Public Const DBPROCDIR = "D:\Sked\Process.Mdb"
Public Const RSTOFDB = "ServerActivities"
Public Const RSTOFPROCDB = "CheckIfRunning"
Public Const SKEDLOG = "D:\Sked\CIMSKED.LOG"
Public Const ERRORLOG = "D:\Sked\ERROR.LOG"
Public Const LOGEVERY = "D:\Sked\EVERY.Log"
'Constants for ProcessFunc
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const PROCESS_VM_READ = &H10
'Vairables for the databases
Public dbAccess As Database
Public dbProc As Database
Public rstAct As Recordset
Public rstChk As Recordset
'Variables for Timer
Public myTime As String
Public myDate As String
'Variables for ListView
Public item As ListItem

'Record Structures
Public Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Public Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXsize As Long
    dwYsize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Public Type MODULEINFO
    lpBaseOfDll As Long
    SizeOfImage As Long
    EntryPoint As Long
End Type

THIS IS WHAT'S INSIDE MY FORM MAIN

Private Sub Form_Load()

On Error GoTo errhandler

'Open all required databases
Call DB.OpenDB
Call ListSubs.ListRec
Exit Sub

errhandler:
    LogToFile.WriteError "Main Form Load - " & Err.Description
    Resume
End Sub

Private Sub Form_Unload(Cancel As Integer)

On Error GoTo errhandler

'Close all databases
Call DB.CloseDB
Exit Sub

errhandler:
    LogToFile.WriteError "Main Form Unload - " & Err.Description
    Resume
End Sub

Private Sub SkedTimer_Timer()
Dim rCode As Long
Dim myFileName As String
Dim strfilename As String

On Error GoTo errhandler

'set time and date
myTime = Format(Time, "hh:mm")
myDate = Format(Date, "mm/dd/yy")

'move all DBs to first record
Call DB.MyFirstPos
'begin the check for sheduled tasks
Call DB.DeleteAll
Call ProcessFunc.GetRunningProcess
With rstAct
    Do While Not .EOF
        If (!status = "Enabled") Then
            If (!Time = myTime) And (!Date = myDate) And _
               (!computername = "SALME") Then
               rCode = Shell(!filename, 1)
               While RecordLocked
                .Edit
               Wend
               !processId = rCode
               If rCode <> 0 Then
                    !status = "Running"
                    myFileName = !filename
                    Call TimeSubs.AddInterval
                    .Update
                    Call ListSubs.ListRec
                    LogToFile.WriteToFile myFileName & " Started"
               End If 'if rcode <> 0
               strfilename = "Filename = '" & myFileName & "'"
               .FindFirst strfilename
        End If
        Else
            If (!status = "Running") Then
                myFileName = !filename
                If (!Time = myTime) And (!Date = myDate) And _
                   (!computername = "SALME") Then
                    'add interval
                    While RecordLocked
                        .Edit
                    Wend
                    Call TimeSubs.AddInterval
                    .Update
                Else
                    'check if still running
                    strfilename = "Filename = '" & myFileName & "'"
                    rstChk.FindFirst strfilename
                    If (rstChk.NoMatch) Then
                        While RecordLocked
                            .Edit
                        Wend
                        !status = "Enabled"
                        .Update
                        Call ListSubs.ListRec
                        LogToFile.WriteToFile myFileName & " Finished"
                    End If ' if rst.nomatch
                               
                    strfilename = "Filename = '" & myFileName & "'"
                    .FindFirst strfilename
                    If (CDate(!Time) < CDate(strtime)) And (!skedIndex = 0) Then
                        While RecordLocked
                            .Edit
                        Wend
                        !status = "Disabled"
                        .Update
                    End If 'if cdate
                End If 'if equal time
            End If 'if running
            strfilename = "Filename = '" & myFileName & "'"
            .FindFirst strfilename
        End If ' if enabled
        .MoveNext
    Loop 'do while not eof
End With 'with rstact

With rstAct
    'set time and date
    myTime = Format(Time, "hh:mm")
    myDate = Format(Date, "mm/dd/yy")
       
    If .RecordCount > 0 Then
        .MoveFirst
    End If
       
    Do While Not .EOF
        Do While (CDate(!Time) < CDate(myTime)) And (!Date = myDate) _
             And (!skedIndex <> 0) And (!computername = "SALME")
            myFileName = !filename
            While RecordLocked
                .Edit
            Wend
            Call TimeSubs.AddInterval
            .Update
               
            strfilename = "Filename = '" & myFileName & "'"
            .FindFirst strfilename
        Loop
        .MoveNext
    Loop
    Call ListSubs.ListRec
End With

Exit Sub

errhandler:
    LogToFile.WriteError "SkedTime.Timer - " & Err.Description
    Resume
End Sub

THIS IS MY DB MODULE

Public Sub OpenDB()

On Error GoTo errhandler

    Set dbAccess = OpenDatabase(DBDIR)
    Set rstAct = dbAccess.OpenRecordset(RSTOFDB, dbOpenDynaset)
    Set dbProc = OpenDatabase(DBPROCDIR)
    Set rstChk = dbProc.OpenRecordset(RSTOFPROCDB, dbOpenDynaset)
    Exit Sub
   
errhandler:
    LogToFile.WriteError "OpenDB - " & Err.Description
    Resume
End Sub

Public Sub CloseDB()

On Error GoTo errhandler

    dbProc.Close
    dbAccess.Close
    Exit Sub
   
errhandler:
    LogToFile.WriteToFile "CloseDB - " & Err.Description
    Resume
End Sub

Public Sub MyFirstPos()

On Error GoTo errhandler

    If rstAct.RecordCount > 0 Then
        rstAct.MoveFirst
    End If
    If rstChk.RecordCount > 0 Then
        rstChk.MoveFirst
    End If
    Exit Sub
   
errhandler:
    LogToFile.WriteError "MyFirstPos - " & Err.Description
    Resume
End Sub

Public Sub DeleteAll()
    On Error GoTo errhandler

    With rstChk
        Do While Not .EOF
            .Delete
            .MoveNext
        Loop
    End With
   
    Exit Sub
   
errhandler:
    LogToFile.WriteError "DeleteAll - " & Err.Description
    Resume
End Sub

THIS IS MY MODULE WHERE I LIST ALL MY ITEMS FROM MY RECORD

Public Sub ListRec()

On Error GoTo errhandler

    With rstAct
        If .RecordCount > 0 Then
            .MoveFirst
        End If
       
        Main.Lst.ListItems.Clear
        Do While (Not (.EOF))
            If (!computername = "SALME") Then
                Set item = Main.Lst.ListItems.Add
                item.Text = !Date
                item.SubItems(1) = !Time
                item.SubItems(2) = !filename
                item.SubItems(3) = !skedtype
                item.SubItems(4) = !mintext
                item.SubItems(5) = !status
            End If
            .MoveNext
        Loop
    End With
   
    Exit Sub
   
errhandler:
    LogToFile.WriteError "ListRec - " & Err.Description
    Resume
End Sub

THIS IS MY MODULE WHERE I PUT ALL MY LOGFILES

Public Sub WriteToFile(msg As String)

On Error GoTo errhandler
   
    Open SKEDLOG For Append As #1
    Print #1, Now & " Execution of " & msg
    Close #1
    Exit Sub
   
errhandler:
    LogToFile.WriteError "WriteToFile - " & Err.Description
    Resume
End Sub

Public Sub WriteError(eror As String)
   
On Error GoTo errhandler
   
    Open ERRORLOG For Append As #2
    Print #2, Now & " " & eror
    Close #2
    Exit Sub
   
errhandler:
    Resume
End Sub

THIS IS MY MODULE THAT LOOKS INTO THE TASK MANAGER TO SEE IF THE APPLICATION I SHELLED IS STILL RUNNING

Public Sub GetRunningProcess()
Dim f As Long
Dim c As Long
Dim sName As String
Dim c2 As Long
Dim sPathName As String
Dim i As Long
Dim iCur As Long
Dim cRequest As Long
Dim cGot As Long
Dim hProcess As Long
Dim hModule As Long
   
    On Error GoTo errhandler
   
    cRequest = 96
    Do
        ReDim aProcesses(0 To (cRequest / 4) - 1) As Long
        f = EnumProcesses(aProcesses(0), cRequest, cGot)
        If f = 0 Then Exit Sub
        If cGot < cRequest Then Exit Do
        cRequest = cRequest * 2
    Loop
    cGot = cGot / 4
    ReDim Preserve aProcesses(0 To cGot - 1) As Long
   
   
    For i = 0 To cGot - 1
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aProcesses(i))
        If hProcess = 0 Then GoTo nextfor
        f = EnumProcessModules(hProcess, hModule, 4, c)
        If f = 0 Then GoTo nextfor
        sName = String$(255, 0)
        sPathName = String$(255, 0)
        c2 = GetModuleFileNameEx(hProcess, hModule, sPathName, 255)
        fname = Left$(sPathName, c2)
       
        With rstChk
            .AddNew
            !filename = fname
            !ProcId = aProcesses(i)
            .Update
        End With
       
nextfor:
        Next
       
        Exit Sub
       
errhandler:
    LogToFile.WriteError "GetRunningProcess - " & Err.Description
    Resume
End Sub

THIS IS MY MODULE TO SEE IF THE RECORD IS CURRENT LOCKED BY ANOTHER USER

Function RecordLocked() As Boolean

On Error GoTo errhandler
       
        RecordLocked = False
        rstAct.Edit
        Exit Function
       
errhandler:
    RecordLocked = True
    LogToFile.WriteError "RecordLocked - " & Err.Description
    Exit Function
End Function

THIS IS MY MODULE WHERE I ADD THE INTERVAL AFTER SHELLING THE SCHEDULED TASK

Public Sub AddInterval()
Dim myNewTime As String
Dim myNewDate As String
Dim myTimeDate As String
Dim fromDBTimeDate As String

On Error GoTo errhandler

    fromDBTimeDate = rstAct!Date & " " & rstAct!Time

    With rstAct
        Select Case (!skedIndex)
        'Schedule type = every n minutes
        Case 1:
            myTimeDate = DateAdd("n", !mintext, fromDBTimeDate)
            myNewTime = Format(myTimeDate, "hh:mm")
            myNewDate = Format(myTimeDate, "mm/dd/yy")
            !Date = myNewDate
            !Time = myNewTime
        'Schedule type = every n hours
        Case 2:
            myTimeDate = DateAdd("h", !mintext, fromDBTimeDate)
            myNewTime = Format(myTimeDate, "hh:mm")
            myNewDate = Format(myTimeDate, "mm/dd/yy")
            !Date = myNewDate
            !Time = myNewTime
        'Schedule type = shifts a day
        Case 3:
            myTimeDate = DateAdd("h", !mintext, fromDBTimeDate)
            myNewTime = Format(myTimeDate, "hh:mm")
            myNewDate = Format(myTimeDate, "mm/dd/yy")
            !Date = myNewDate
            !Time = myNewTime
        'Schedule type = days a week
        Case 4:
            myTimeDate = DateAdd("ww", !mintext, fromDBTimeDate)
            myNewTime = Format(myTimeDate, "hh:mm")
            myNewDate = Format(myTimeDate, "mm/dd/yy")
            !Date = myNewDate
            !Time = myNewTime
        'Schedule type = daily
        Case 5:
            myTimeDate = DateAdd("d", !mintext, fromDBTimeDate)
            myNewTime = Format(myTimeDate, "hh:mm")
            myNewDate = Format(myTimeDate, "mm/dd/yy")
            !Date = myNewDate
            !Time = myNewTime
        'Schedule type = Monthly
        Case 6:
            myTimeDate = DateAdd("m", !mintext, fromDBTimeDate)
            myNewTime = Format(myTimeDate, "hh:mm")
            myNewDate = Format(myTimeDate, "mm/dd/yy")
            !Date = myNewDate
            !Time = myNewTime
        End Select
    End With
    Exit Sub
   
errhandler:
    LogToFile.WriteError "AddInterval - " & Err.Description
    Resume
End Sub

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now