Results 1 to 5 of 5
  1. #1
    MLogue9 is offline Novice
    Windows 8 Access 2007
    Join Date
    Jan 2019
    Posts
    22

    Question docmd.RunSQL breaks from function

    SOLVED - I was missing a quote and an error handler in parent function was hiding syntax error. I don't know why error escalated there, you see I commented the one out in this function.



    I hate how Windows 8 takes a century to list and sort files. I figure I'd be better off listing files into an access table to help organize massive numbers of surveillance videos. Anyhow I got everything set up, but the thing simply stops and exits function at the docmd.runSQL line. I have breakpoints all around it and verified this is where it breaks out.

    Here is example of one SQL command:
    INSERT INTO tClips (directoryShort, directory, filename, fileSize, dateMod, dateDD, dateMM, dateYY, camera) VALUES ( "15", "c:\video_surv2\15", "150929-a.jpg", "223348", "2015-10-01 22:07:30", "1", "10", "2015", "150929-a);"

    Here is the function in question:

    Code:
    Private Function FillDirToTable(colDirList As Collection _
        , ByVal strFolder As String _
        , strFileSpec As String _
        , bIncludeSubfolders As Boolean)
      Dim a
      Dim strCamera As String
       Dim dateMod As Date
       Dim strDirectoryShort As String
       Dim directory As String
       
       Dim dateDD, dateMM, dateYY As Integer
       
        'Build up a list of files, and then add add to this list, any additional folders
        'On Error GoTo Err_Handler
            Dim strErrMsg As String
        Dim strTemp As String
        Dim colFolders As New Collection
        Dim vFolderName As Variant
        Dim strSQL, strFilENP, strDateMod, strDateCreate, strSize As String
          Dim strMsg As String
        'Add the files to the folder.
        strFunction = "FileList/FDTT"
        strFolder = TrailingSlash(strFolder)
        strTemp = Dir(strFolder & strFileSpec)
        strDirectoryShort = GetRightFolder(strFolder)
        
        Do While strTemp <> vbNullString
             gCount = gCount + 1
             SysCmd acSysCmdSetStatus, gCount
           strFilENP = strFolder & strTemp
           
           dateMod = ShowFileInfo(strFilENP, "M") 'get file mod date
           strDateCreate = ShowFileInfo(strFilENP, "C") 'get file CREATE date
           strSize = ShowFileInfo(strFilENP, "S") 'get fiel size
           strTemp = Trim(strTemp)
           dateDD = Day(dateMod)
           dateMM = Month(dateMod)
           dateYY = year(dateMod)
           a = Split(strTemp, ".")
        strCamera = a(0)
        
           
             strSQL = "INSERT INTO tClips " _
              & " (directoryShort, directory, filename, fileSize, dateMod, dateDD, dateMM, dateYY, camera) " _
              & " VALUES ( """ & strDirectoryShort & """" _
              & ", """ & strFolder & """" _
              & ", """ & strTemp & """" _
              & ", """ & strSize & """" _
              & ", """ & dateMod & """" _
              & ", """ & dateDD & """" _
              & ", """ & dateMM & """" _
              & ", """ & dateYY & """" _
              & ", """ & strCamera & ");"
    DoCmd.SetWarnings (warningson)
           DoCmd.RunSQL strSQL
            
            'colDirList.Add strFolder & strTemp
            strTemp = Dir
        Loop
        If bIncludeSubfolders Then
            'Build collection of additional subfolders.
            strTemp = Dir(strFolder, vbDirectory)
            Do While strTemp <> vbNullString
                If (strTemp <> ".") And (strTemp <> "..") Then
                    If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then
                        colFolders.Add strTemp
                    End If
                End If
                strTemp = Dir
            Loop
            'Call function recursively for each subfolder.
            For Each vFolderName In colFolders
                Call FillDirToTable(colDirList, strFolder & TrailingSlash(vFolderName), strFileSpec, True)
            Next vFolderName
        End If
    Exit_Handler:
    strMsg = strFunction & "Sucessful exit."
    
        Call CStatus(strMsg, 218)
        Exit Function
    Err_Handler:
        
       'Error Handling
    Exit Function
    err_hand:
    strErrMsg = strFunction & "> " & Err.Number & " " & Err.Source & " " & Err.description
    Call CStatus(strErrMsg, 518, Err.Number, Err.description, Err.Source)
        
        strSQL = "INSERT INTO Files " _
        & " (FName, FPath) " _
        & " SELECT ""  ~~~ ERROR ~~~""" _
        & ", """ & strFolder & """;"
        CurrentDb.Execute strSQL
        
        Resume Exit_Handler
    End Function
    Last edited by MLogue9; 01-01-2019 at 05:49 PM. Reason: Solved

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I use a single apostrophe instead of doubled quote marks because I find it easier to read.

    Code:
        strSQL = "INSERT INTO Files " _
        & " (FName, FPath) " _
        & " SELECT '~~~ ERROR ~~~'" _
        & ", '" & strFolder & "';"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    You are wrongly treating all fields as text

    The dateMod field needs date delimiters.
    Several fields including the other 3 dates are actually number fields and need to be handled as such.
    The final value strCamera is missing an end quote
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In VBA, you MUST explicitly declare variable types. For instance, in this line
    Code:
    Dim dateDD, dateMM, dateYY As Integer
    only "dateYY" is declared as an integer and "dateDD" and "dateMM" are declared as Variants.

    Same with this line
    Code:
    Dim strSQL, strFilENP, strDateMod, strDateCreate, strSize As String
    only "strSize" is declared as a string and "strSQL", "strFilENP", "strDateMod" and "strDateCreate" are declared as Variants.

    This line
    Code:
    DoCmd.SetWarnings (warningson)
    does nothing. The correct syntax/usage is
    Code:
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    BTW, I NEVER use "DoCmd.RunSQL". I always use "CurrentDb.Execute sSQL, dbfailonerror".


    You have 2 variables that are not declared: "strFunction" and "gCount"


    You have
    Code:
    SysCmd acSysCmdSetStatus, gCount
    but you do not clear the status bar when the function exits......



    The top 2 lines of EVERY module should be
    Code:
    Option Compare Database '<<-- EVERY module should have these two lines
    Option Explicit         '<<-- EVERY module should have these two lines

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I don't know why error escalated there
    In the absence of an error handler in the current procedure, Access goes "up the tree" of any procedures that are active (i.e. those that called subsequent procedures and have not yet terminated) and looks for an error handler. If it finds one, it will take instruction there, which may not be what you want. If it doesn't find one, the error is "fatal". Either way, the result will not usually end well, and in the very least, you could get a custom error message that has no bearing on the current problem. It is odd that you disabled error handling for the posted procedure.

    I agree with the advice about warnings. I'd also suggest that procedures like this contain an error handler which in the very least, resumes to an exit point and that point contains all the cleanup stuff: closing recordsets, Set rs = Nothing (etc), and puts any modified db properties back to normal, and of course, an Exit statement to prevent the error handler from executing.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. DoCmd.RunSQL help!
    By stephenchan433 in forum Access
    Replies: 7
    Last Post: 11-14-2015, 06:35 PM
  2. Queries vs VBA DoCmd.RunSQL
    By ck4794 in forum Programming
    Replies: 1
    Last Post: 10-27-2013, 10:31 AM
  3. DoCmd.RunSQL
    By Access_Blaster in forum Programming
    Replies: 6
    Last Post: 07-23-2013, 09:27 PM
  4. my first DoCmd.RunSQL
    By broecher in forum Programming
    Replies: 4
    Last Post: 11-05-2010, 09:35 PM
  5. docmd.runsql update
    By Mitch_Aus in forum Programming
    Replies: 2
    Last Post: 10-05-2010, 09:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums