Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Compare Database
- 'ModularSaveCodeVersion = 35
- Option Explicit
- 'Do Not Move the above line
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' Version 1 - 26/Apr/2016 - initial '
- ' Version 2 - 02/may/2016 - added the version system to the module and d to tag'
- ' Version 3 - 16/May/2016 - updated the saveall function to handle situations '
- ' where we need to update a record instead of make a '
- ' new one. Does not break compatability '
- ' Version 4 - 17/May/2016 - added the "o" tag for radio buttons '
- ' Version 5 - 19/May/2016 - added some commenting, changed the fieldname check '
- ' Version 6 - 19/May/2016 - fixed huge save bug '
- ' Version 7 - 07/Jun/2016 - fixed another huge save bug from argFieldName '
- ' checking for null when it can't ever be null '
- ' Version 8 - 13/Jun/2016 - changed all integers to longs. '
- ' Version 9 - 23/Jun/2016 - change the argument name argType to argFieldType '
- ' Version10 - 27/Jun/2016 - added a check to skip autonumber fields if i '
- ' accedentally include them in the list of fields to '
- ' save. '
- ' Version11 - 31/Aug/2016 - Fixed error handling for the validation and save '
- ' functions. added the ability to use the label '
- ' captions from apropriately named label controls '
- ' instead of field names '
- ' Version12 - 31/Aug/2016 - missed some bugs introduced by the error handling '
- ' (looking at you exit sub in a function) '
- ' Version13 - 07/Sep/2016 - Updated currentdb ref to dblocal function '
- ' Version14 - 22/Sep/2016 - added a validation check for list boxes to see if '
- ' their item count is > 0 '
- ' Version15 - 26/Sep/2016 - updated the item count check to see if it IS 0 '
- ' not gt 0 -_- '
- ' Version16 - 20/Oct/2016 - Added the f flag for information that is null if '
- ' the control is disabled. it can also be validated '
- ' to a data type using the n flag for example. '
- ' added enumeration for field types '
- ' Version17 - 08/Dec/2016 - added a function to strip off the prefix of the '
- ' control, or just pass it along if it didnt need to '
- ' split the n flag to be n == float/double and '
- ' i == integer/long. changed the label printing on '
- ' the validate function to a 3 value piece of data '
- ' thats backwards compatable with booleans. it adds '
- ' support to use the default <fieldname>_Label. '
- ' Version18 - 09/Dec/2016 - added the capability to check the validity of the '
- ' data even if it is not required. '
- ' Version19 - 05/Jan/2017 - added support for debug mode '
- ' Version20 - 24/Jan/2017 - added support for column heads on list boxes in '
- ' the validate function '
- ' Version21 - 24/Jan/2017 - fixed support for debug mode '
- ' Version22 - 24/Jan/2017 - fixed validate improperly flagging list boxes and '
- ' option/radio buttons when they were supposed to be '
- ' set to ignore with flag x '
- ' Version23 - 06/Feb/2017 - Added function LoadQryIntoForm to autoload data '
- ' into a form quick and easy. added a modifier for '
- ' the f flag "g" so that if it has a non empty value '
- ' it will save it since presumably it will have been '
- ' validated before the control was disabled. '
- ' Version24 - 27/Mar/2017 - Added function WipeForm to auto wipe forms using '
- ' tags default c '
- ' Version25 - 25/May/2017 - updated function wipeform to skip tag e and not '
- ' allow it to be used as the custom tag. '
- ' Version26 - 19/Jul/2017 - Fixed a bug in the LoadQryIntoForm function on the '
- ' Where string where it reopened the qry instead of '
- ' the recordset with the filter applied. '
- ' Version27 - 08/Aug/2017 - Updated save code to be able to use query defs '
- ' Version28 - 17/Aug/2017 - Updated save code for the use case where you have '
- ' a radio button group that is not manditory but '
- ' needs label data. also to note on v27, you can not '
- ' use a query that has aliased fields. Also added a '
- ' check to see if the recordset is updateable. '
- ' Version29 - 31/Aug/2017 - Updated Validate so that it allows you to validate '
- ' only fields with a specific custom tag, blank is '
- ' default behavior. '
- ' Version30 - 17/Jan/2018 - Updated Validate so that it trims before '
- ' validating '
- ' Version31 - 22/Jan/2018 - Updated SaveAll so that you can use a custom tag '
- ' so you can use multiple saves per form '
- ' Version32 - 22/Jan/2018 - Updated SaveAll so that you can use a paramaterized'
- ' query (for faster updating) '
- ' Version33 - 27/Apr/2018 - Updated documentation for saveall and validate '
- ' Version34 - 15/Aug/2018 - removed the message box stating the recordset is '
- ' empty from LoadQryIntoForm '
- ' Version35 - 17/Aug/2018 - using StandardErrorBox now '
- ' '
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' '
- 'Instructions: '
- ' '
- '1a) for textboxes, check boxes, list boxes with only one selection, '
- ' and combo boxes set the name of the control like Type prefix & field name '
- ' ex: "txt" & "Date_time" = "txtDate_time" '
- '1b) for radio button groups set the group to the field name then the labels '
- ' should be prefix fieldname radiobutton value '
- ' ex: "lbl" & "AssetTag" & 1 = "lblAssetTag1" then the caption would be the '
- ' asset tag like "10-001" '
- '2) set the tag field under the "other" tab NOTE: you can have multiple letters'
- ' s - basic tag to indicate you want to save the value of the control '
- ' x - non-manditory fields that you still wish to save overrides f '
- ' r - specifically for radio buttons to parse the labels caption data '
- ' o - radio buttons where you want the value of the radio button instead of '
- ' text, also 0 is not a valid number to set the radio buttons to '
- ' n - for float/double '
- ' d - indicates that the value must be a date '
- ' e - Read Only do not attempt to write to. This is more for loading data then'
- ' saving data. '
- ' l - checks a listbox to see if it has any item data(Validation Only) '
- ' f - not manditory if control is disabled (ignore value if disabled) '
- ' g - modifier to f to check if it has a value and if it does save it '
- ' c - For AutoClearing '
- ' i - For Integer '
- ' '
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' Requirements: '
- ' -Basic include version 15 for the dbLocal function '
- ' '
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Public Enum mscFieldType
- mscString = 0
- mscNumber = 1
- mscDate = 2
- End Enum
- Public Enum mscLabelDesc
- msclblstyle = -1
- mscNone = 0
- msc_labelStyle = 1
- End Enum
- Public Enum mscSourceType
- mscTableDef = 0
- mscQueryDef = 1
- mscParaQueryDef = 2
- End Enum
- Public Sub SaveAll(argSaveTable As String, argForm As String, Optional argFieldName As Variant = "", Optional argValue As Variant = Null, Optional argFieldType As mscFieldType = mscString, Optional argQryOrTbl As mscSourceType = mscTableDef, Optional argCustomSaveTag As String = "s")
- 'Instructions for use
- 'If argQryOrTbl is mscTableDef then
- ' if the argFieldName argument is blank, it adds a new record
- ' else filter to the first record that has argFieldName as the value argValue.
- ' you also have to set argFieldType to the correct data type so the filter can use the appropriate filter.
- ' ie. string uses single quoters, numbers use nothing and dates use pound signs
- 'If argQryOrTbl is mscQueryDef then
- ' this works essentially the same way as with mscTableDef
- 'If argQryOrTbl is mscParaQueryDef
- ' then this can ONLY update the record returned by the value/array of values in the argValue parameter.
- ' argFieldName,argFieldType are both unneeded.
- '
- 'For all, if you need to save two different sets of data from the same form, you can change the argCustomSaveTag from "s"
- ' to whatever is needed.(this is a very specific need, that generally isnt needed.)
- '
- 'you have to name the controls the same as the fields but with the control type prefix.
- ' this strips the control type prefix off using a predefined list of control prefixes
- ' (or you can have no prefix at all, but that makes things needlessly complicated)
- '
- If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
- Dim rs As DAO.Recordset
- Dim qry As QueryDef
- Dim ctrl As Control
- Dim u As Long
- Dim i As Long
- If Len(argCustomSaveTag) <> 1 Then
- Err.Raise vbObjectError + 4, "SaveAll", "The Custom save tag argument is invalid."
- End If
- dbLocal.TableDefs.Refresh
- If argQryOrTbl = mscTableDef Then
- Set rs = dbLocal.TableDefs(argSaveTable).OpenRecordset(dbOpenDynaset, dbSeeChanges)
- ElseIf argQryOrTbl = mscQueryDef Then
- Set rs = dbLocal.QueryDefs(argSaveTable).OpenRecordset(dbOpenDynaset, dbSeeChanges)
- ElseIf argQryOrTbl = mscParaQueryDef Then
- Set qry = dbLocal.QueryDefs(argSaveTable)
- If VarType(argValue) >= vbArray Then
- u = UBound2(argValue)
- If u = (qry.Parameters.count - 1) Then
- For i = 0 To u Step 1
- qry.Parameters(i).Value = argValue(i)
- Next i
- Else
- Err.Raise vbObjectError + 6, "SaveAll", "Number of Parameters in query(" & qry.Parameters.count & ") do not match the number of parameters passed in(" & u + 1 & ")"
- End If
- Else
- If Not (IsNull(argValue)) And qry.Parameters.count = 1 Then
- qry.Parameters(0) = argValue
- ElseIf qry.Parameters.count = 0 And Not (IsNull(argValue)) Then
- Err.Raise vbObjectError + 5, "SaveAll", "Number of Parameters in query(" & qry.Parameters.count & ") do not match the number of parameters passed in(1)"
- ElseIf qry.Parameters.count = 0 And (IsNull(argValue)) Then
- End If
- End If
- Set rs = qry.OpenRecordset(dbOpenDynaset, dbSeeChanges)
- If rs.RecordCount > 0 Then
- If rs.Updatable Then
- rs.MoveFirst
- rs.Edit
- For Each ctrl In Forms(argForm).Controls
- If ctrl.Tag Like "*" & argCustomSaveTag & "*" Then
- 'Debug.Print ctrl.Name
- If Not ((rs.Fields(StripPrefix(ctrl.Name)).Attributes And dbAutoIncrField) = 16) Then
- 'should detect if a field is autonumber and skip it.
- 's for save
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- 'x for exclude from check
- If ctrl.Value & "" = "" Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- ElseIf ctrl.Tag Like "*f*" Then
- If ctrl.Enabled Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- Else
- If ctrl.Tag Like "*g*" Then
- If ctrl.Value & "" = "" Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- End If
- End If
- ElseIf ctrl.Tag Like "*r*" Then
- 'r to extract the value intended from the label of the radio button assuming you named the label the same name
- 'with a different prefix and the value of the radio button as a postifx
- rs.Fields(StripPrefix(ctrl.Name)).Value = Forms(argForm).Controls("lbl" & StripPrefix(ctrl.Name) & ctrl.Value).Caption
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- Else
- Debug.Print "skipped " & ctrl.Name & " for being an Autonumber field"
- End If
- End If
- Next
- rs.Update
- Else
- Err.Raise vbObjectError + 2, "SaveAll", "The Recordset you are trying to update can not be updated."
- End If
- Else
- Err.Raise vbObjectError + 1, "SaveAll", "The Record you are trying to update does not exist"
- End If
- GoTo Error_Exit
- End If
- If argFieldName = "" Then
- 'Since you can not pass in a null under the string data type, the argument argFieldName will never be null, but can be an empty string
- If rs.Updatable Then
- rs.AddNew
- For Each ctrl In Forms(argForm).Controls
- If ctrl.Tag Like "*" & argCustomSaveTag & "*" Then
- 's for save
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- 'x for exclude from check
- If ctrl.Value & "" = "" Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- Else
- If ctrl.Tag Like "*r*" And ctrl.Value <> 0 Then
- 'use case where you have a radio button group that you need label caption data from
- 'that is not manditory.
- rs.Fields(StripPrefix(ctrl.Name)).Value = Forms(argForm).Controls("lbl" & StripPrefix(ctrl.Name) & ctrl.Value).Caption
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- End If
- ElseIf ctrl.Tag Like "*f*" Then
- If ctrl.Enabled Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- Else
- If ctrl.Tag Like "*g*" Then
- If ctrl.Value & "" = "" Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- End If
- End If
- ElseIf ctrl.Tag Like "*r*" Then
- 'r to extract the value intended from the label of the radio button assuming you named the label the same name
- 'with a different prefix and the value of the radio button as a postifx
- 'if you want to store the value of radio button instead of the label, just make sure you set a default and leave the r tag off.
- rs.Fields(StripPrefix(ctrl.Name)).Value = Forms(argForm).Controls("lbl" & StripPrefix(ctrl.Name) & ctrl.Value).Caption
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- End If
- Next
- rs.Update
- Else
- Err.Raise vbObjectError + 3, "SaveAll", "The Recordset you are trying to add a record to can not be updated."
- End If
- Else
- If Not IsNull(argValue) Then
- If argFieldType = mscString Then
- rs.filter = argFieldName & " = '" & argValue & "'"
- ElseIf argFieldType = mscNumber Then
- rs.filter = argFieldName & " = " & argValue
- ElseIf argFieldType = mscDate Then
- rs.filter = argFieldName & " = #" & argValue & "#"
- Else
- rs.filter = "1=2"
- End If
- Else
- rs.filter = "1=2"
- End If
- Set rs = rs.OpenRecordset(dbOpenDynaset, dbSeeChanges)
- If rs.RecordCount > 0 Then
- If rs.Updatable Then
- rs.MoveFirst
- rs.Edit
- For Each ctrl In Forms(argForm).Controls
- If ctrl.Tag Like "*" & argCustomSaveTag & "*" Then
- 'Debug.Print ctrl.Name
- If Not ((rs.Fields(StripPrefix(ctrl.Name)).Attributes And dbAutoIncrField) = 16) Then
- 'should detect if a field is autonumber and skip it.
- 's for save
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- 'x for exclude from check
- If ctrl.Value & "" = "" Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- ElseIf ctrl.Tag Like "*f*" Then
- If ctrl.Enabled Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- Else
- If ctrl.Tag Like "*g*" Then
- If ctrl.Value & "" = "" Then
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = Null
- End If
- End If
- ElseIf ctrl.Tag Like "*r*" Then
- 'r to extract the value intended from the label of the radio button assuming you named the label the same name
- 'with a different prefix and the value of the radio button as a postifx
- rs.Fields(StripPrefix(ctrl.Name)).Value = Forms(argForm).Controls("lbl" & StripPrefix(ctrl.Name) & ctrl.Value).Caption
- Else
- rs.Fields(StripPrefix(ctrl.Name)).Value = ctrl.Value
- End If
- Else
- Debug.Print "skipped " & ctrl.Name & " for being an Autonumber field"
- End If
- End If
- Next
- rs.Update
- Else
- Err.Raise vbObjectError + 2, "SaveAll", "The Recordset you are trying to update can not be updated."
- End If
- Else
- Err.Raise vbObjectError + 1, "SaveAll", "The Record you are trying to update does not exist"
- End If
- End If
- Error_Exit:
- Set rs = Nothing
- Exit Sub
- Error_Handler:
- StandardErrorBox "Save", Err, Errors
- 'MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
- "Error Number: " & Err.Number & vbCrLf & _
- "Error Source: Save" & vbCrLf & _
- "Error Description: " & Err.Description _
- , vbOKOnly + vbCritical, "An Error has Occured!"
- Resume Error_Exit
- End Sub
- Public Function Validate(argForm As String, Optional argLabelDesc As mscLabelDesc = mscNone, Optional argCustomTag As String = "") As String
- 'if this ouputs an empty string, everything properly validated.
- 'if argCustomTag is set then it will only validate controls that have that custom tag.
- 'if you wish to use the argLabelDesc option msclblstyle the toolbox has a function
- ' that can rename all bound (to a specific control) labels to the parent controls name.
- ' this function is called: renameLabel
- If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
- Dim ctrl As Control
- Dim out As String: out = vbCrLf
- Dim sourceForm As Form: Set sourceForm = Forms(argForm)
- If Len(argCustomTag) > 1 Then
- Err.Raise vbObjectError + 1, "Validate", "Custom tag can only be one character long"
- End If
- If argCustomTag = "" Then
- For Each ctrl In sourceForm.Controls
- If ctrl.Tag & "" <> "" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*f*" And ctrl.Enabled Then
- If ctrl.Tag Like "*n*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsNumeric(ctrl.Value) Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*i*" Then
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsNumeric(ctrl.Value) Or CStr(ctrl.Value & "") Like "*.*" Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*d*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsDate(ctrl.Value) Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*l*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- ElseIf ctrl.ListCount = 0 Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- ElseIf ctrl.ColumnHeads And ctrl.ListCount = 1 Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*r*" Or ctrl.Tag Like "*o*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- ElseIf ctrl.Value = 0 Then
- ' if you want your radio button group to be not selected when you use this and catch the error, then set the default to 0 and start the valid value set at 1
- 'considering changing this to check for -1 so a valid value set can be from 0 up.
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*s*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Trim(ctrl.Value & "") = "" Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- End If
- ElseIf Not ctrl.Tag Like "*f*" Then
- If ctrl.Tag Like "*n*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsNumeric(ctrl.Value) Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*i*" Then
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsNumeric(ctrl.Value) Or CStr(ctrl.Value & "") & "" Like "*.*" Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*d*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsDate(ctrl.Value) Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*l*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- ElseIf ctrl.ListCount = 0 Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- ElseIf ctrl.ColumnHeads And ctrl.ListCount = 1 Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*r*" Or ctrl.Tag Like "*o*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- ElseIf ctrl.Value = 0 Then
- ' if you want your radio button group to be not selected when you use this and catch the error, then set the default to 0 and start the valid value set at 1
- 'considering changing this to check for -1 so a valid value set can be from 0 up.
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*s*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Trim(ctrl.Value & "") = "" Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- End If
- End If
- End If
- Next
- Else
- For Each ctrl In sourceForm.Controls
- If ctrl.Tag & "" Like "*" & argCustomTag & "*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*f*" And ctrl.Enabled Then
- If ctrl.Tag Like "*n*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsNumeric(ctrl.Value) Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*i*" Then
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsNumeric(ctrl.Value) Or CStr(ctrl.Value & "") Like "*.*" Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*d*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsDate(ctrl.Value) Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*l*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- ElseIf ctrl.ListCount = 0 Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- ElseIf ctrl.ColumnHeads And ctrl.ListCount = 1 Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*r*" Or ctrl.Tag Like "*o*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- ElseIf ctrl.Value = 0 Then
- ' if you want your radio button group to be not selected when you use this and catch the error, then set the default to 0 and start the valid value set at 1
- 'considering changing this to check for -1 so a valid value set can be from 0 up.
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*" & argCustomTag & "*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Trim(ctrl.Value & "") = "" Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- End If
- ElseIf Not ctrl.Tag Like "*f*" Then
- If ctrl.Tag Like "*n*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsNumeric(ctrl.Value) Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*i*" Then
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsNumeric(ctrl.Value) Or CStr(ctrl.Value & "") & "" Like "*.*" Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*d*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Not IsDate(ctrl.Value) Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*l*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- ElseIf ctrl.ListCount = 0 Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- ElseIf ctrl.ColumnHeads And ctrl.ListCount = 1 Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*r*" Or ctrl.Tag Like "*o*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" Then
- ElseIf ctrl.Value = 0 Then
- ' if you want your radio button group to be not selected when you use this and catch the error, then set the default to 0 and start the valid value set at 1
- 'considering changing this to check for -1 so a valid value set can be from 0 up.
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- ElseIf ctrl.Tag Like "*" & argCustomTag & "*" Then
- 'Debug.Print "Name: " & ctrl.Name & " Fieldame: " & StripPrefix(ctrl.Name) & " Value: " & ctrl.Value
- If ctrl.Tag Like "*x*" And Trim(ctrl.Value & "") = "" Then
- ElseIf Trim(ctrl.Value & "") = "" Then
- If argLabelDesc = msclblstyle Then
- out = out & sourceForm.Controls("lbl" & StripPrefix(ctrl.Name)).Caption & vbCrLf
- ElseIf argLabelDesc = msc_labelStyle Then
- out = out & sourceForm.Controls(StripPrefix(ctrl.Name) & "_Label").Caption & vbCrLf
- Else
- out = out & StripPrefix(ctrl.Name) & vbCrLf
- End If
- End If
- End If
- End If
- End If
- Next
- End If
- out = Left(out, Len(out) - 2)
- Error_Exit:
- Validate = out
- Exit Function
- Error_Handler:
- 'MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
- "Error Number: " & err.Number & vbCrLf & _
- "Error Source: Save" & vbCrLf & _
- "Error Description: " & err.Description _
- , vbOKOnly + vbCritical, "An Error has Occured!"
- out = "The following error has occured" & vbCrLf & vbCrLf & _
- "Error Number: " & Err.Number & vbCrLf & _
- "Error Source: Validate" & vbCrLf & _
- "Error Description: " & Err.Description
- Resume Error_Exit
- End Function
- Public Function StripPrefix(argIn As String) As String
- Dim PrefixList() As String: PrefixList = Split("txt,cmb,cbo,chk,lst,rad,opt", ",")
- Dim v As Variant
- For Each v In PrefixList
- If argIn Like v & "*" Then
- StripPrefix = Right(argIn, Len(argIn) - Len(v))
- Exit Function
- End If
- Next
- StripPrefix = argIn
- End Function
- Public Function LoadQryIntoForm(argQry As String, argForm As String, Optional argParameters As Variant = Null, Optional argExcludeEmptyTags As Boolean = True, Optional argIsFilterString As Boolean = True) As Long
- If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
- Dim qry As QueryDef
- Dim rs As DAO.Recordset
- Dim frm As Form
- Dim f As Field
- Dim u As Long
- Dim c As Control
- Dim i As Long
- LoadQryIntoForm = 0
- Set qry = dbLocal.QueryDefs(argQry)
- If argIsFilterString Then
- Set rs = qry.OpenRecordset(dbOpenDynaset, dbSeeChanges)
- rs.filter = argParameters
- Set rs = rs.OpenRecordset(dbOpenSnapshot)
- Else
- If VarType(argParameters) >= vbArray Then
- u = UBound(argParameters)
- If u = (qry.Parameters.count - 1) Then
- For i = 0 To u Step 1
- qry.Parameters(i).Value = argParameters(i)
- Next i
- Else
- Err.Raise vbObjectError, "LoadQryIntoForm", "Number of Parameters in query(" & qry.Parameters.count & ") do not match the number of parameters passed in(" & u + 1 & ")"
- End If
- Else
- If Not (IsNull(argParameters)) And qry.Parameters.count = 1 Then
- qry.Parameters(0) = argParameters
- ElseIf qry.Parameters.count = 0 And Not (IsNull(argParameters)) Then
- Err.Raise vbObjectError + 1, "LoadQryIntoForm", "Number of Parameters in query(" & qry.Parameters.count & ") do not match the number of parameters passed in(1)"
- ElseIf qry.Parameters.count = 0 And (IsNull(argParameters)) Then
- End If
- End If
- Set rs = qry.OpenRecordset(dbOpenDynaset, dbSeeChanges)
- End If
- Set frm = Forms(argForm)
- 'If argFilter <> "" Then
- 'rs.filter = argFilter
- 'Set rs = rs.OpenRecordset(dbOpenDynaset, dbSeeChanges)
- 'End If
- If rs.RecordCount Then
- rs.MoveFirst
- If argExcludeEmptyTags Then
- For Each f In rs.Fields
- For Each c In frm.Controls
- If f.Name = StripPrefix(c.Name) And Not (c.Tag Like "*[el]*") And c.Tag & "" <> "" Then
- c.Value = f.Value
- End If
- Next
- Next
- Else
- For Each f In rs.Fields
- For Each c In frm.Controls
- If f.Name = StripPrefix(c.Name) And Not (c.Tag Like "*[el]*") Then
- c.Value = f.Value
- End If
- Next
- Next
- End If
- Else
- 'MsgBox "Error, The recordset is empty"
- LoadQryIntoForm = 1
- End If
- Error_Exit:
- Set rs = Nothing
- Set qry = Nothing
- Set frm = Nothing
- Exit Function
- Error_Handler:
- StandardErrorBox "LoadQryIntoForm", Err
- 'MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
- "Error Number: " & Err.Number & vbCrLf & _
- "Error Source: LoadQryIntoForm" & vbCrLf & _
- "Error Description: " & Err.Description _
- , vbOKOnly + vbCritical, "An Error has Occured!"
- LoadQryIntoForm = -1
- Resume Error_Exit
- End Function
- Public Sub WipeForm(argForm As String, Optional argCustomTag As String = "c")
- Dim f As Form
- Dim c As Control
- If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
- If Len(Trim(argCustomTag)) = 1 And argCustomTag <> "e" Then
- Set f = Forms(argForm)
- For Each c In f.Controls
- If c.Tag Like "*" & argCustomTag & "*" And Not (c.Tag Like "*e*") Then
- Select Case c.ControlType
- Case acCheckBox
- c.Value = False
- Case acOptionGroup
- c.Value = 0
- Case Else
- c.Value = ""
- End Select
- End If
- Next
- End If
- Error_Exit:
- Set f = Nothing
- Exit Sub
- Error_Handler:
- StandardErrorBox "WipeForm", Err
- 'MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
- "Error Number: " & Err.Number & vbCrLf & _
- "Error Source: WipeForm" & vbCrLf & _
- "Error Description: " & Err.Description _
- , vbOKOnly + vbCritical, "An Error has Occured!"
- Resume Error_Exit
- End Sub
- Public Sub AppendToTag(argForm As String, argTag As String, Optional argConditionTag As String = "")
- Dim f As Form
- Dim c As Control
- If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
- Set f = Forms(argForm)
- If argConditionTag <> "" Then
- For Each c In f.Controls
- If c.Tag Like "*" & argConditionTag & "*" Then
- If Not c.Tag Like "*" & argTag & "*" Then
- c.Tag = c.Tag & argTag
- End If
- End If
- Next
- Else
- For Each c In f.Controls
- If c.ControlType = acTextBox Or c.ControlType = acTextBox Or c.ControlType = acCheckBox Or c.ControlType = acComboBox Or c.ControlType = acListBox Or c.ControlType = acOptionGroup Then
- If Not c.Tag Like "*" & argTag & "*" Then
- c.Tag = c.Tag & argTag
- End If
- End If
- Next
- End If
- Error_Exit:
- Set f = Nothing
- Exit Sub
- Error_Handler:
- StandardErrorBox "AppendToTag", Err
- 'MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
- "Error Number: " & Err.Number & vbCrLf & _
- "Error Source: AppendToTag" & vbCrLf & _
- "Error Description: " & Err.Description _
- , vbOKOnly + vbCritical, "An Error has Occured!"
- Resume Error_Exit
- End Sub
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Supporting functions not in this module
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Public Sub StandardErrorBox(ByVal argSource As String, ByRef e As ErrObject, Optional ByRef daoE As DAO.Errors = Nothing, Optional ByVal argExtra As String = "")
- Dim msg As String
- Dim u As Long
- Dim er As DAO.Error
- msg = "The following error(s) has/have occured" & vbCrLf & vbCrLf & "Error Number: " & e.Number & vbCrLf & _
- "Error Source: " & e.Source & vbCrLf & _
- "Error Description: " & e.Description
- If Not daoE Is Nothing Then
- If e.Source Like "*[oO][dD][bB][cC]*" Then
- msg = msg & vbCrLf & vbCrLf & "DAO errors" & vbCrLf
- For Each er In daoE
- msg = msg & vbCrLf & "Error Number: " & er.Number & vbCrLf & _
- "Error Source: " & er.Source & vbCrLf & _
- "Error Description: " & er.Description
- Next
- End If
- End If
- If argExtra <> "" Then
- msg = msg & vbCrLf & vbCrLf & "Additional Information: " & argExtra
- End If
- msg = msg & vbCrLf & vbCrLf & "Function: " & argSource
- MsgBox msg, vbOKOnly + vbCritical, "An Error has Occured!"
- End Sub
- Public Function dbLocal(Optional bolCleanup As Boolean = False) As DAO.Database
- ' This function started life based on a suggestion from
- ' Michael Kaplan in comp.databases.ms-access back in the early 2000s
- ' 2003/02/08 DWF added comments to explain it to myself!
- ' 2005/03/18 DWF changed to use Static variable instead
- ' uses GoTos instead of If/Then because:
- ' error of dbCurrent not being Nothing but dbCurrent being closed (3420)
- ' would then be jumping back into the middle of an If/Then statement
- On Error GoTo errHandler
- Static dbCurrent As DAO.Database
- Dim strTest As String
- If bolCleanup Then GoTo closeDB
- retryDB:
- If dbCurrent Is Nothing Then
- Set dbCurrent = CurrentDb()
- End If
- ' now that we know the db variable is not Nothing, test if it's Open
- strTest = dbCurrent.Name
- dbCurrent.QueryDefs.Refresh
- exitRoutine:
- Set dbLocal = dbCurrent
- Exit Function
- closeDB:
- If Not (dbCurrent Is Nothing) Then
- 'dbCurrent.close ' this never has any effect
- Set dbCurrent = Nothing
- End If
- GoTo exitRoutine
- errHandler:
- Select Case Err.Number
- Case 3420 ' Object invalid or no longer set.
- Set dbCurrent = Nothing
- If Not bolCleanup Then
- Resume retryDB
- Else
- Resume closeDB
- End If
- Case Else
- StandardErrorBox "dbLocal", Err
- 'MsgBox Err.Number & ": " & Err.Description, vbExclamation, "Error in dbLocal()"
- Resume exitRoutine
- End Select
- End Function
Advertisement
Add Comment
Please, Sign In to add comment