I have a query string in the following code. I want to put the entire code into a function so I can use it somewhere else as well.
Code:
Private Sub Form_Load()
'To enable the AllowAddition property once the form is opened
Dim dbs As DAO.Database
Dim rstTest As DAO.Recordset
Dim strQuery As String
Dim db As Database
strQuery = "SELECT OrderingT.Order_ID, OrderingT.UnitsRequested,OrderingT.OrderStatus, ([UnitsRequested])-Count([Product_ID]) AS [The Remaining Units] " + _
"FROM (OrderingT INNER JOIN PackingSlipT ON OrderingT.Order_ID = PackingSlipT.Order_ID) INNER JOIN ProductT ON PackingSlipT.PackingSlip_ID = ProductT.PackingSlip_ID " + _
"WHERE (PackingSlipT.PackingSlip_ID = " & Form.Tag & ") " + _
"GROUP BY OrderingT.Order_ID, OrderingT.UnitsRequested , OrderingT.OrderStatus;"
Set db = CurrentDb
Set rstTest = db.OpenRecordset(strQuery)
If Not rstTest.EOF Then
If rstTest![The Remaining Units] = 0 Then
MsgBox "This order was filled"
Else
'If the remaining units is not equal to zero once the form is opened , then the record additions are allowed.
Me.AllowAdditions = True
AddRecord.Enabled = True
End If
End If
'To read the packing slip number of the product form into a Tag
If InStr(Me.Filter, "=") > 0 Then
If IsNumeric(Mid$(Me.Filter, InStr(Me.Filter, "=") + 1, Len(Me.Filter) - InStr(Me.Filter, "="))) Then
Me.Tag = (Mid$(Me.Filter, InStr(Me.Filter, "=") + 1, Len(Me.Filter) - InStr(Me.Filter, "=")))
End If
End If
End Sub