Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    Error Handling in Run time

    I'm testing some features on a machine that uses access run-time. It seems that any time I use a function I get the message:



    "execution of the application has stopped due to a run-time error. The application can't continue and will be shut down."
    I have tried several methods for error handling and cant get any different result than this. eg. if error is <>0 then show the error number and message.

    The first function this happens on is FindAsYouType. I can remove this function and the form is still functional to the end user.

    But, I have a form that requires a list box to be populated using a function as shown here:

    Code:
    Private Sub Form_Load()         Call ListFiles("\\server\general\Documents\!Management\VBA_Templates_do_not_modify", , , Me.lstFileList)
             DoCmd.SetWarnings False
             DoCmd.OpenQuery "UpdateRiskDeselection"
             DoCmd.SetWarnings True
             
    End Sub
    which calls:

    Code:
    Public Function ListFiles(strPath As String, Optional strFileSpec As String, _    Optional bIncludeSubfolders As Boolean, Optional lst As ListBox)
    On Error GoTo Err_Handler
        'Purpose:   List the files in the path.
        'Arguments: strPath = the path to search.
        '           strFileSpec = "*.*" unless you specify differently.
        '           bIncludeSubfolders: If True, returns results from subdirectories of strPath as well.
        '           lst: if you pass in a list box, items are added to it. If not, files are listed to immediate window.
        '               The list box must have its Row Source Type property set to Value List.
        'Method:    FilDir() adds items to a collection, calling itself recursively for subfolders.
        Dim colDirList As New Collection
        Dim varItem As Variant
        
        Call FillDir(colDirList, strPath, strFileSpec, bIncludeSubfolders)
       
        'Add the files to a list box if one was passed in. Otherwise list to the Immediate Window.
        If lst Is Nothing Then
            For Each varItem In colDirList
                Debug.Print varItem
            Next
        Else
            For Each varItem In colDirList
            lst.AddItem varItem
            Next
        End If
    
    
    Exit_Handler:
        Exit Function
    
    
    Err_Handler:
        MsgBox "Error " & Err.Number & ": " & Err.Description
        Resume Exit_Handler
    End Function
    
    
    Private Function FillDir(colDirList As Collection, ByVal strFolder As String, strFileSpec As String, _
        bIncludeSubfolders As Boolean)
        'Build up a list of files, and then add add to this list, any additional folders
        Dim strTemp As String
        Dim colFolders As New Collection
        Dim vFolderName As Variant
    
    
        'Add the files to the folder.
        strFolder = TrailingSlash(strFolder)
        strTemp = Dir(strFolder & strFileSpec)
        Do While strTemp <> vbNullString
            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 FillDir(colDirList, strFolder & TrailingSlash(vFolderName), strFileSpec, True)
            Next vFolderName
        End If
    End Function
    
    
    Public Function TrailingSlash(varIn As Variant) As String
        If Len(varIn) > 0& Then
            If Right(varIn, 1&) = "\" Then
                TrailingSlash = varIn
            Else
                TrailingSlash = varIn & "\"
            End If
        End If
    End Function
    Its worth mentioning both functions were on a form load event. When I remove the form load event the app remains open with no errors.

    How would I start to troubleshoot this? It seems the error handling isn't getting me anywhere.

    Andy.

  2. #2
    dashiellx's Avatar
    dashiellx is offline Falconer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Location
    Baltimore
    Posts
    49
    Have you stepped through the code to see what line is throwing the error?

    Have you tried the form's current event instead?

  3. #3
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    I cant get to the code on the run-time machine. Unless i'm wrong? Would be great if I could. I will try the current event and read up tomorrow. Thanks.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    Error handling will work with runtime so sounds like your ‘if err <>0’ was wrong. Suggest you show what you actually tried

  5. #5
    dashiellx's Avatar
    dashiellx is offline Falconer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Location
    Baltimore
    Posts
    49
    In my experience, if the error is occurring in the run time, then the same error should be occurring in the full access version.
    Last edited by dashiellx; 02-19-2019 at 01:41 PM. Reason: grammar

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    wild guess, but perhaps the runtime machine has a different view of the directories?

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    IIRC...In Runtime, you cannot make things that the Access Gnomes consider to be 'design changes,' and maybe they consider assigning the files to a Listbox to be such a change.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    you cannot make things that the Access Gnomes consider to be 'design changes,'
    in runtime, you certainly can't go into design view, but you can use vba to do things like move/resize controls, change colours/fonts/recordsets/sources/visibility and the like - and you can't subsequently save those changes (runtime should be .accde anyway)

    @Andy - you do know that if you change the file extension to .accdr then the file will behave as if it is runtime even if you have a full version of access installed. - so you can test your problem on your development machine

  9. #9
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    I've tried a lot Ajax.two examples are these:

    Code:
    Private Sub Form_Load()On Error GoTo ErrorHandler
        Call FindAsUTypeLoad(Me)
        Exit Sub
    ErrorHandler:
     MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
    
    
      End Sub
    And:

    Code:
    '  ErrorHandler:
    If Err.Number <> 0 Then     Msg = "Error # " & Str(Err.Number) & " was generated by " _
             & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
         MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
         End If
         Resume Next
    the run time machine does have a different view of directories on the server but I'm using the absolute path which does work. (tested on other DBs and programs.)

    I've created a runtime version on my machine and its working fine.

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    and what about error handling in the findasyoutype function?

  11. #11
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    I've not changed it at all its "as downloaded"

    Code:
    'Author:        Allen Browne.   allen@allenbrowne.com'Date:          August, 2006.
    'Limitations:   Combos where the bound column is hidden have these limitations:
    '                   -RowSourceType is "Table/Query": filtering supported in Access 2002 or later.
    '                   -RowSourceType is "Value List" or a callback function: filtering not supported.
    'Instructions:  1. Copy this module into your database.
    '               2. Copy the combo and text box onto your form.
    '               3. Set the form's On Load property to:  [Event Procedure]
    '               4. Click the Build button beside the form's On Load property. Access opens the code window.
    '               5. In the code window, between the "Private Sub Form_Load" and "End Sub" lines, enter this:
    '                       Call FindAsUTypeLoad(Me)
    'Documentation: http://allenbrowne.com/AppFindAsUType.html
    Option Compare Database
    Option Explicit
    
    
    'Configuration options
    Private Const mbcStartOfField = False   'True to match only the start of the field; False for anywhere in field.
    Private Const mstrcWildcardChar = "*"   'Pattern matching wildcard. "*" for Access. "%" for SQL Server.
    Private Const mstrcSep = ";"            'Separator between list items. May need changing for some regional settings.
    
    
    'Columns of cboFindAsUTypeField
    Private Const micControlName = 0
    Private Const micControlLabel = 1
    Private Const micControlType = 2
    Private Const micFilterField = 3
    Private Const micFieldType = 4
    
    
    'Constant to indicate a control is sitting on the form (not on the page of a tab control.)
    Private Const mlngcOnTheForm = -1&
    
    
    'Module name (for error handler.)
    Private Const conMod = "ajbFindAsUType"
    
    
    Public Function FindAsUTypeLoad(frm As Form, ParamArray avarExceptionList()) As Boolean
    On Error GoTo Err_Handler
        'Purpose:   Initialize the code for Find.
        'Return:    True on success.
        'Arguments: - frm = a reference to the form where you want this filtering.
        '           - Optionally, you can specify controls NOT to offer filtering on, by putting the control names in quotes.
        'Note:      The form must contain the 2 controls, cboFindAsUTypeField and txtFindAsUTypeValue,
        '               with the combo set up correctly.
        'Usage:     Set the Load event procedure of the form to:
        '               Call FindAsUType(Me)
        '           To suppress filtering on controls FirstName and City, use:
        '               Call FindAsUType(Me, "FirstName", "City")
        Dim rs As DAO.Recordset             'Clone set of the form.
        Dim ctl As Control                  'Each control on the form.
        Dim strForm As String               'Name of form (for error handler.)
        Dim strControl As String            'Name of the control.
        Dim strField As String              'Name of the filter to use in the filter string.
        Dim strControlSource As String      'Name of the field the control is bound to.
        Dim strOut As String                'List for the RowSource of cboFindAsUTypeField.
        Dim lngI As Long                    'Loop counter.
        Dim lngJ As Long                    'Page counter loop controller.
        Dim bSkip As Boolean                'Flag to provide no filtering for this control.
        Dim bResult As Boolean              'Return value for this function.
        Dim lngParentNumber As Long         '-1 if the control is directly on the form, else PageIndex of it parent.
        Dim lngMaxParentNumber As Long      'PageIndex of last page of tab control. -1 if no tab control.
        Dim astrControls() As String        'Array to handle the controls on the form.
        Const lngcControl = 0&              'First element of array astrControls is the control name.
        Const lngcField = 1&                'Second element of the array is the field name to filter on.
        
        'The form must have a control source if we are to filter it, and needs our 3 controls.
        strForm = frm.Name
        
        If HasUnboundControls(frm, "cboFindAsUTypeField", "txtFindAsUTypeValue") And (frm.RecordSource <> vbNullString) Then
            'Set the event handers for the 2 contorls
            frm!cboFindAsUTypeField.AfterUpdate = "=FindAsUTypeChange([Form])"
            frm.txtFindAsUTypeValue.OnChange = "=FindAsUTypeChange([Form])"
            'Calculate the number of pages on the tab control if there is one.
            lngMaxParentNumber = MaxParentNumber(frm)
            
            'Declare an array large enough to handle the controls on the form,
            '   for each page of any tab control (since these have their own tab index),
            '   and for storing the control name and the filter field name.
            ReDim astrControls(0& To frm.Controls.Count - 1&, mlngcOnTheForm To lngMaxParentNumber, lngcControl To lngcField) As String
            Set rs = frm.RecordsetClone             'For info about the fields the controls are bound to.
            
            'Loop through the controls on the form.
            For Each ctl In frm.Controls
                'Ignore hidden controls, and limit ourselves to text boxes and combos.
                If ctl.Visible Then
                    If (ctl.ControlType = acTextBox) Or (ctl.ControlType = acComboBox) Then
                        bSkip = False
                        strField = vbNullString
                        strControl = ctl.Name
                        'Ignore if the control name is in the exception list.
                        For lngI = LBound(avarExceptionList) To UBound(avarExceptionList)
                            If avarExceptionList(lngI) = strControl Then
                                bSkip = True
                                Exit For
                            End If
                        Next
                        
                        If Not bSkip Then
                            'Ignore if unbound, or bound to an expression.
                            strControlSource = ctl.ControlSource
                            If (strControlSource = vbNullString) Or (strControlSource Like "=*") Then
                                bSkip = True
                            Else
                                'Ignore yes/no fields, binary (JET uses for unknown), and complex data types (> 100.)
                                Select Case rs(strControlSource).Type
                                Case dbBoolean, dbLongBinary, dbBinary, dbGUID, Is > 100
                                    bSkip = True
                                End Select
                            End If
                        End If
                        
                        'Ignore if we cannot specify the field to filter on.
                        If Not bSkip Then
                            strField = GetFilterField(ctl)
                            If strField = vbNullString Then
                                bSkip = True
                            End If
                        End If
                        
                        'Add this control name to our array, in the order of the tab index.
                        If Not bSkip Then
                            lngParentNumber = ParentNumber(ctl)
                            astrControls(ctl.TabIndex, lngParentNumber, lngcControl) = strControl
                            astrControls(ctl.TabIndex, lngParentNumber, lngcField) = strField
                        End If
                    End If
                End If
            Next
            
            'Loop through the array of controls, to build the string for the RowSource of cboFindAsUTypeField (5 columns.)
            For lngJ = LBound(astrControls, 2) To UBound(astrControls, 2)
                For lngI = LBound(astrControls) To UBound(astrControls)
                    If astrControls(lngI, lngJ, lngcControl) <> vbNullString Then
                        Set ctl = frm.Controls(astrControls(lngI, lngJ, lngcControl))
                        strOut = strOut & """" & ctl.Name & """" & mstrcSep & _
                            """" & Caption4Control(frm, ctl) & """" & mstrcSep & _
                            ctl.ControlType & mstrcSep & _
                            """" & astrControls(lngI, lngJ, lngcField) & """" & mstrcSep & _
                            """" & rs(ctl.ControlSource).Type & """" & mstrcSep
                    End If
                Next
            Next
            rs.Close
            
            'Remove the trailing separator, and assign to the RowSource of cboFindAsUTypeField.
            lngI = Len(strOut) - Len(mstrcSep)
            If lngI > 0 Then
                With frm.cboFindAsUTypeField
                    .RowSource = Left(strOut, lngI)
                    .Value = .ItemData(0)           'Initialize to the first item in the list.
                End With
                bResult = True            'Return True: the list loaded successfully.
            End If
        End If
        
        'Show the filter controls. (Separate routine, since they could fail if the control does not exist.)
        Call ShowHideControl(frm, "cboFindAsUTypeField", bResult)
        Call ShowHideControl(frm, "txtFindAsUTypeValue", bResult)
        
        'Return value
        FindAsUTypeLoad = bResult
    
    
    Exit_Handler:
        Set ctl = Nothing
        Set rs = Nothing
        Exit Function
    
    
    Err_Handler:
        Call LogError(Err.Number, Err.Description, conMod & ".FindAsUTypeLoad", "Form " & strForm)
        Resume Exit_Handler
    End Function
    
    
    Public Function FindAsUTypeChange(frm As Form) As Boolean
    On Error GoTo Err_Handler
        'Purpose:   Filter the form, by the control named in cboFindAsUTypeField and the value in txtFindAsUTypeValue.
        'Return:    True unless an error occurred.
        'Usage:     The code assigns this to the Change event of the text box, and the AfterUpdate event of the combo.
        Dim strText As String       'The text of the text box.
        Dim lngSelStart As Long     'Selection Starting point.
        Dim strField As String      'Name of the field to filter on.
        Dim bHasFocus As Boolean    'True if the text box has focus (since it can be called from the combo too.)
        Const strcTextBox = "txtFindAsUTypeValue"
    
    
        'If the text box has focus, remember the selection insert point and use its Text. Otherwise use its Value.
        bHasFocus = (frm.ActiveControl.Name = strcTextBox)
        If bHasFocus Then
            strText = frm!txtFindAsUTypeValue.Text
            lngSelStart = frm!txtFindAsUTypeValue.SelStart
        Else
            strText = Nz(frm!txtFindAsUTypeValue.Value, vbNullString)
        End If
        
        'Save any uncommitted edits in the form. (This loses the insertion point, and converts Text to Value.)
        If frm.Dirty Then
            frm.Dirty = False
        End If
        
        'Read the filter field name from the combo.
        strField = Nz(frm.cboFindAsUTypeField.Column(micFilterField), vbNullString)
        
        'Unfilter if there is no text to find, or no control to filter. Otherwise, filter.
        If (strText = vbNullString) Or (strField = vbNullString) Then
            frm.FilterOn = False
        Else
            frm.Filter = strField & " Like """ & IIf(mbcStartOfField, vbNullString, mstrcWildcardChar) & _
                strText & mstrcWildcardChar & """"
            frm.FilterOn = True
        End If
        
        'If the control had focus, restore focus if necessary, and set the insertion point.
        If bHasFocus Then
            If frm.ActiveControl.Name <> strcTextBox Then
                frm(strcTextBox).SetFocus
            End If
            If strText <> vbNullString Then
                frm!txtFindAsUTypeValue = strText
                frm!txtFindAsUTypeValue.SelStart = lngSelStart
            End If
        End If
        
        'Return True if the routine completed without error.
        FindAsUTypeChange = True
    
    
    Exit_Handler:
        Exit Function
    
    
    Err_Handler:
        Select Case Err.Number
        Case 2474
            Resume Next
        Case 2185   'Text box loses focus when no characters left.
            Resume Exit_Handler
        Case Else
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "txtFindAsUTypeValue_Change"
            Resume Exit_Handler
        End Select
    End Function
    
    
    Private Function Caption4Control(frm As Form, ctl As Control) As String
    On Error GoTo Err_Handler
        'Purpose:
        Dim strCaption As String
    
    
        '1st choice: Assign the caption of the attached label.
        strCaption = ctl.Controls(0).Caption
        
        '2nd choice: Read the caption from the label over the column in a continuous form.
        If strCaption = vbNullString Then
            strCaption = CaptionFromHeader(frm, ctl)
        End If
        
        'Strip the trailing semicolon.
        If Right$(strCaption, 1&) = ":" Then
            strCaption = Left$(strCaption, Len(strCaption) - 1&)
        End If
        'Strip the ampersand hotkey.
        If InStr(strCaption, "&") > 0& Then
            strCaption = Replace(strCaption, "&&", Chr$(31))
            strCaption = Replace(strCaption, "&", vbNullString)
            strCaption = Replace(strCaption, Chr$(31), "&")
        End If
        
        '3rd choice: Use the control name.
        If strCaption = vbNullString Then
            strCaption = ctl.Name
        End If
        
        Caption4Control = strCaption
        
    Exit_Handler:
        Exit Function
    
    
    Err_Handler:
        Select Case Err.Number
        Case 2467&
            Resume Next
        Case Else
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Caption4Control()"
            Resume Exit_Handler
        End Select
    End Function
    
    
    Private Function CaptionFromHeader(frm As Form, ctl As Control) As String
    On Error GoTo Err_Handler
        'Purpose:   Look for a label in the column header, directly over the control, in continuous form view.
        'Return:    Caption of the label if found.
        Dim ctlHeader As Control    'controls in the header of the form.
        Const icRadius = 120        'one twelveth of an inch, in twips.
        
        'If we are in Form view, and it's a Continuous Form,
        '   and there is a label in the Form Header directly above the column, return its Caption.
        If (frm.CurrentView = 1) And (frm.DefaultView = 1) Then
            For Each ctlHeader In frm.Section(acHeader).Controls
                If ctlHeader.ControlType = acLabel Then
                    If (ctlHeader.Left > ctl.Left - icRadius) And (ctlHeader.Left < ctl.Left + icRadius) Then
                        CaptionFromHeader = ctlHeader.Caption
                    End If
                End If
            Next
        End If
        
    Exit_Handler:
        Set ctlHeader = Nothing
        Exit Function
    
    
    Err_Handler:
        If Err.Number <> 2462& Then     'No such Section.
            Call LogError(Err.Number, Err.Description, conMod & ".CaptionFromHeader")
        End If
        Resume Exit_Handler
    End Function
    
    
    Private Function HasUnboundControls(frm As Form, ParamArray avarControlNames()) As Boolean
    On Error GoTo Err_Handler
        'Purpose:   Return true if all the controls named in the array are present on the form, and are unbound.
        Dim lngI As Long
        Dim bCancel As Boolean
        
        If UBound(avarControlNames) > 0& Then
            'Loop through the named controls on the form.
            For lngI = LBound(avarControlNames) To UBound(avarControlNames)
                If frm.Controls(avarControlNames(lngI)).ControlSource <> vbNullString Then
                    bCancel = True
                    Exit For
                End If
            Next
            'If we did not drop to the error handler, the form has the named controls.
            HasUnboundControls = Not bCancel
        End If
        
    Exit_Handler:
        Exit Function
    
    
    Err_Handler:
        Resume Exit_Handler
    End Function
    
    
    Private Function MaxParentNumber(frm As Form) As Long
    On Error GoTo Err_Handler
        'Purpose:   Return the PageIndex of the tab page that the control is on.
        'Return:    -1 if setting directly on the form, else the PageIndex of the last page of the tab control.
        'Note:      PageIndex is zero based, so subtract 1 from the count of pages.
        Dim ctl As Control          'Each control on the form.
        Dim lngReturn As Long
        
        lngReturn = mlngcOnTheForm      'Initialize to no tab control.
        For Each ctl In frm.Controls
            If ctl.ControlType = acTabCtl Then
                lngReturn = ctl.Pages.Count - 1
                Exit For                    'A form can have only one tab control.
            End If
        Next
        
        MaxParentNumber = lngReturn
        
    Exit_Handler:
        Exit Function
    
    
    Err_Handler:
        Call LogError(Err.Number, Err.Description, conMod & ".MaxParentNumber")
        Resume Exit_Handler
    End Function
    
    
    Private Function ParentNumber(ctl As Control) As Integer
    On Error Resume Next
        'Purpose:   Return the PageIndex of the tab page that the control is on.
        'Return:    -1 if setting directly on the form, else the page of the tab control.
        'Note:      This works for text boxes and combos, not for labels or controls in an option group.
        Dim iReturn As Integer
        
        iReturn = ctl.Parent.PageIndex
        If Err.Number <> 0& Then
            iReturn = mlngcOnTheForm
        End If
        ParentNumber = iReturn
    End Function
    
    
    Private Function ShowHideControl(frm As Form, strControlName As String, bShow As Boolean) As Boolean
    On Error Resume Next
        'Purpose:   Show or hide a control on the form, without error message.
        'Return:    True if the contorl's Visible property was set successfully.
        'Arguments: frm            = a reference to the form where the control is expected.
        '           strControlName = the name of the control to show or hide.
        '           bShow          = True to make visible; False to make invisible.
        'Note:      This is a separate routine, since hiding a non-existant control will error.
        frm.Controls(strControlName).Visible = bShow
        ShowHideControl = (Err.Number = 0&)
    End Function
    
    
    Private Function GetFilterField(ctl As Control) As String
    On Error GoTo Err_Handler
        'Purpose:   Determine the field name to use when filtering on this control.
        'Return:    The field name the control is bound to, except for combos.
        '               In Access 2002 and later, we return the syntax Access uses for filtering these controls.
        'Argument:  The control we are trying to filter.
        'Note:      We don't use the Recordset of the combo, because:
        '               a) it's not supported earlier than Access 2002, and
        '               b) it's often not loaded at this point.
        '               Instead, we OpenRecordset to get the source field name,
        '               which works even if the field is aliased in the RowSource.
        '               Opening for append only is quicker, as it loads no existing records.
        Dim rs As DAO.Recordset     'To get information about the combo's RowSource.
        Dim iColumn As Integer      'The first visible column of the combo (zero-based.)
        Dim strField As String      'Return value: the field name to use for the filter string.
        Dim bCancel As Boolean      'Flag to not filter on this control.
        
        If ctl.ControlType = acComboBox Then
            iColumn = FirstVisibleColumn(ctl)
            If iColumn = ctl.BoundColumn - 1 Then
                'The bound column is the first visible column: filter on the control source field.
                strField = "[" & ctl.ControlSource & "]"
            Else
                'In Access 2002 and later, we can use the lookup syntax Access uses, if the source is a Table/Query.
                If Int(Val(SysCmd(acSysCmdAccessVer))) >= 10 Then
                    If ctl.RowSourceType = "Table/Query" Then
                        Set rs = DBEngine(0)(0).OpenRecordset(ctl.RowSource, dbOpenDynaset, dbAppendOnly)
                        With rs.Fields(iColumn)
                            strField = "[Lookup_" & ctl.Name & "].[" & .SourceField & "]"
                        End With
                        rs.Close
                    Else
                        bCancel = True  'Hidden bound column not supported if RowSourceType is Value List or call-back function.
                    End If
                Else
                    bCancel = True      'Hidden bound column not supported for versions earlier than Access 2002.
                End If
            End If
        Else
            'Not a combo: filter on the control source field.
            strField = "[" & ctl.ControlSource & "]"
        End If
        
        If strField <> vbNullString Then
            GetFilterField = strField
        ElseIf Not bCancel Then
            GetFilterField = "[" & ctl.ControlSource & "]"
        End If
        Set rs = Nothing
    
    
    Exit_Handler:
        Exit Function
    
    
    Err_Handler:
        Call LogError(Err.Number, Err.Description, conMod & ".GetFilterField")
        Resume Exit_Handler
    End Function
    
    
    Private Function FirstVisibleColumn(cbo As ComboBox) As Integer
    On Error GoTo Err_Handler
        'Purpose:   Return the column number of the first visible column in a combo.
        'Return:    Column number. ZERO-BASED!
        'Argument:  The combo to examine.
        'Note:      Also returns zero on error.
        Dim i As Integer            'Loop controller.
        Dim varArray As Variant     'Array of the combo's ColumnWidths values.
        Dim iResult As Integer      'Colum number to return.
        Dim bFound As Boolean       'Flag that we found a value to return.
        
        If cbo.ColumnWidths = vbNullString Then
            'If no column widths are specified, the first column is visible.
            iResult = 0
            bFound = True
        Else
            'Parse the ColumnWidths string into an array, and find the first non-zero value.
            varArray = Split(cbo.ColumnWidths, mstrcSep)
            For i = LBound(varArray) To UBound(varArray)
                If varArray(i) <> 0 Then
                    iResult = i
                    bFound = True
                    Exit For
                End If
            Next
            'If the column widths ran out before all columns were checked, the next column is the first visible one.
            If Not bFound Then
                If i < cbo.ColumnCount Then
                    iResult = i
                    bFound = True
                End If
            End If
        End If
        
        FirstVisibleColumn = iResult
    
    
    Exit_Handler:
        Exit Function
    
    
    Err_Handler:
        Call LogError(Err.Number, Err.Description, conMod & ".FirstVisibleColumn")
        Resume Exit_Handler
    End Function
    
    
    '------------------------------------------------------------------------------------------------
    'You may prefer to replace this with a true error logger. See http://allenbrowne.com/ser-23a.html
    Private Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _
        strCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = True) As Boolean
    On Error GoTo Err_LogError
        ' Purpose: Generic error handler.
        ' Arguments: lngErrNumber - value of Err.Number
        ' strErrDescription - value of Err.Description
        ' strCallingProc - name of sub|function that generated the error.
        ' vParameters - optional string: List of parameters to record.
        ' bShowUser - optional boolean: If False, suppresses display.
        ' Author: Allen Browne, allen@allenbrowne.com
    
    
        Dim strMsg As String      ' String for display in MsgBox
    
    
        Select Case lngErrNumber
        Case 0
            Debug.Print strCallingProc & " called error 0."
        Case 2501                ' Cancelled
            'Do nothing.
        Case 3314, 2101, 2115    ' Can't save.
            If bShowUser Then
                strMsg = "Record cannot be saved at this time." & vbCrLf & _
                    "Complete the entry, or press <Esc> to undo."
                MsgBox strMsg, vbExclamation, strCallingProc
            End If
        Case Else
            If bShowUser Then
                strMsg = "Error " & lngErrNumber & ": " & strErrDescription
                MsgBox strMsg, vbExclamation, strCallingProc
            End If
            LogError = True
        End Select
    
    
    Exit_LogError:
        Exit Function
    
    
    Err_LogError:
        strMsg = "An unexpected situation arose in your program." & vbCrLf & _
            "Please write down the following details:" & vbCrLf & vbCrLf & _
            "Calling Proc: " & strCallingProc & vbCrLf & _
            "Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf & vbCrLf & _
            "Unable to record because Error " & Err.Number & vbCrLf & Err.Description
        MsgBox strMsg, vbCritical, "LogError()"
        Resume Exit_LogError
    End Function
    
    
    '------------------------------------------------------------------------------------------------
    'Replace() and Split() are needed for Access 97 only. Leave them commented out for later versions.
    '------------------------------------------------------------------------------------------------
    'Private Function Replace(strExpr As String, strFind As String, strReplace As String, Optional lngStart As Long = 1) As String
    '    Dim strOut As String
    '    Dim lngLenExpr As Long
    '    Dim lngLenFind As Long
    '    Dim lng As Long
    '
    '    lngLenExpr = Len(strExpr)
    '    lngLenFind = Len(strFind)
    '
    '    If (lngLenExpr > 0&) And (lngLenFind > 0&) And (lngLenExpr >= lngStart) Then
    '        lng = lngStart
    '        If lng > 1 Then
    '            strOut = Left$(strExpr, lng - 1&)
    '        End If
    '        Do While lng <= lngLenExpr
    '            If Mid(strExpr, lng, lngLenFind) = strFind Then
    '                strOut = strOut & strReplace
    '                lng = lng + lngLenFind
    '            Else
    '                strOut = strOut & Mid(strExpr, lng, 1&)
    '                lng = lng + 1
    '            End If
    '        Loop
    '        Replace = strOut
    '    End If
    '
    'End Function
    '
    'Private Function Split(strIn As String, strDelim As String) As Variant
    '    'Purpose:   Return a variant array from the items in the string, delimited the 2nd argument.
    '    Dim varArray As Variant     'Variant array for output.
    '    Dim lngStart As Long        'Position in string where argument starts.
    '    Dim lngEnd As Long          'Position in string where argument ends.
    '    Dim lngLenDelim As Long     'Length of the delimiter string.
    '    Dim i As Integer            'index to the array.
    '
    '    lngLenDelim = Len(strDelim)
    '    If (lngLenDelim = 0&) Or (Len(strIn) = 0&) Then
    '        ReDim varArray(0)       'Initialize a zero-item array.
    '    Else
    '        ReDim varArray(9)       'Initialize a 10 item array.
    '        i = -1                  'First item will be zero when we add 1.
    '        lngStart = 1            'Start searching at first character of string.
    '
    '        'Search for the delimiter in the input string, until not found any more.
    '        Do
    '            i = i + 1
    '            If i > UBound(varArray) Then    'Add more items if necessary
    '                ReDim Preserve varArray(UBound(varArray) + 10)
    '            End If
    '
    '            lngEnd = InStr(lngStart, strIn, strDelim)
    '            If lngEnd = 0 Then  'This is the last item.
    '                varArray(i) = Trim(Mid(strIn, lngStart))
    '                Exit Do
    '            Else
    '                varArray(i) = Trim(Mid(strIn, lngStart, lngEnd - lngStart))
    '                lngStart = lngEnd + lngLenDelim
    '            End If
    '        Loop
    '        'Set the upper bound of the array to the correct number of items.
    '        ReDim Preserve varArray(i)
    '    End If
    '
    '    Split = varArray
    'End Function
    '------------------------------------------------------------------------------------------------

    The findasyoutype code is actually working. After update event in the input textbox is:

    Code:
    =FindAsUTypeChange([Form])
    But I'm not calling the function on form load as I thought you should (as stated in the instructions). I am testing this now but I'm happy to leave it if cant get anywhere with it. the other function "listfiles" I do need to work. The listbox will show template documents.

    The testing I'm doing now consists of deleting parts and trying to narrow down where it fails. Bit of a long process right now.

    Edit: Its when I'm calling the function in form load it fails.
    Last edited by Homegrownandy; 02-20-2019 at 04:16 AM. Reason: clarification

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    I've created a runtime version on my machine and its working fine
    Edit: Its when I'm calling the function in form load it fails.
    Andy, you are being vague - which function? which runtime?

    At the moment the implication is there is either something different about the data on the other computer or perhaps you are using different libraries/access versions.

  13. #13
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    I've created a runtime version on my machine and its working fine
    - I followed your suggestion and changed the file extension to .accdr. This works without issue on my machine.

    Edit: Its when I'm calling the function in form load it fails.
    Both functions ajbFileList & ajbFindAsUType on the other machine will fail and close when in the form load event. Bit strange since the find as you type function will continue to work on both machines without calling it in form load.

    The only reason I'm testing on the find as you type code rather than the one I need to populate the listbox is because its easier to understand this code. I will try to narrow down the error on "
    ajbFileList" function and update.

  14. #14
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    I'm going to have to drop out - work commitments. You just need to be clear about what you are doing otherwise we are guessing - for example you appear to have changed names from 'ListFiles' to 'ajbFileList'. Why? Have you change it in the function as well?

  15. #15
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    When I said functions there I meant modules. In my head they are the same thing, sorry for the confusion. I've not renamed anything, it's exactly as it in the example downloaded from:

    http://allenbrowne.com/AppFindAsUType.html

    Module is named ajbFindAsUType (I don't think this name matters).

    thanks for the suggestions so far anyway. Ill try to run the downloaded one "as is" on the other machine.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 12-18-2015, 11:43 AM
  2. Replies: 5
    Last Post: 09-06-2015, 12:06 PM
  3. Error Handling Question - On Error Goto
    By redbull in forum Programming
    Replies: 7
    Last Post: 12-06-2012, 07:54 AM
  4. Replies: 3
    Last Post: 09-05-2012, 10:23 AM
  5. Handling Inventory Costs That Change Over Time
    By mubtuhogar in forum Database Design
    Replies: 5
    Last Post: 10-12-2010, 09:19 AM

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