KySoto

LoadQryIntoForm

Aug 14th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 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
  2. If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
  3. Dim qry As QueryDef
  4. Dim rs As DAO.Recordset
  5. Dim frm As Form
  6. Dim f As Field
  7. Dim u As Long
  8. Dim c As Control
  9. Dim i As Long
  10. LoadQryIntoForm = 0
  11. Set qry = dbLocal.QueryDefs(argQry)
  12. If argIsFilterString Then
  13.     Set rs = qry.OpenRecordset(dbOpenDynaset, dbSeeChanges)
  14.     rs.filter = argParameters
  15.     Set rs = rs.OpenRecordset(dbOpenSnapshot)
  16. Else
  17.     If VarType(argParameters) >= vbArray Then
  18.         u = UBound(argParameters)
  19.         If u = (qry.Parameters.count - 1) Then
  20.             For i = 0 To u Step 1
  21.                 qry.Parameters(i).Value = argParameters(i)
  22.             Next i
  23.         Else
  24.              Err.Raise vbObjectError, "LoadQryIntoForm", "Number of Parameters in query(" & qry.Parameters.count & ") do not match the number of parameters passed in(" & u + 1 & ")"
  25.         End If
  26.     Else
  27.         If Not (IsNull(argParameters)) And qry.Parameters.count = 1 Then
  28.             qry.Parameters(0) = argParameters
  29.         ElseIf qry.Parameters.count = 0 And Not (IsNull(argParameters)) Then
  30.             Err.Raise vbObjectError + 1, "LoadQryIntoForm", "Number of Parameters in query(" & qry.Parameters.count & ") do not match the number of parameters passed in(1)"
  31.         ElseIf qry.Parameters.count = 0 And (IsNull(argParameters)) Then
  32.         End If
  33.     End If
  34.     Set rs = qry.OpenRecordset(dbOpenDynaset, dbSeeChanges)
  35. End If
  36.  
  37. Set frm = Forms(argForm)
  38. 'If argFilter <> "" Then
  39.    'rs.filter = argFilter
  40.    'Set rs = rs.OpenRecordset(dbOpenDynaset, dbSeeChanges)
  41. 'End If
  42. If rs.RecordCount Then
  43.     rs.MoveFirst
  44.     If argExcludeEmptyTags Then
  45.         For Each f In rs.Fields
  46.             For Each c In frm.Controls
  47.                 If f.Name = StripPrefix(c.Name) And Not (c.Tag Like "*[el]*") And c.Tag & "" <> "" Then
  48.                     c.Value = f.Value
  49.                 End If
  50.             Next
  51.         Next
  52.     Else
  53.             For Each f In rs.Fields
  54.         For Each c In frm.Controls
  55.             If f.Name = StripPrefix(c.Name) And Not (c.Tag Like "*[el]*") Then
  56.                 c.Value = f.Value
  57.             End If
  58.         Next
  59.     Next
  60.     End If
  61. Else
  62.     LoadQryIntoForm = 1
  63. End If
  64. error_exit:
  65. Set rs = Nothing
  66. Set qry = Nothing
  67. Set frm = Nothing
  68. Exit Function
  69. Error_Handler:
  70.     MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
  71.            "Error Number: " & Err.Number & vbCrLf & _
  72.            "Error Source: LoadQryIntoForm" & vbCrLf & _
  73.            "Error Description: " & Err.Description _
  74.            , vbOKOnly + vbCritical, "An Error has Occured!"
  75.            LoadQryIntoForm = -1
  76. Resume error_exit
  77. End Function
Advertisement
Add Comment
Please, Sign In to add comment