benkingery
Registered User.
- Local time
- Today, 11:11
- Joined
- Jul 15, 2008
- Messages
- 153
I’m trying to create a function to keep track of inventory levels and I’m having a little problem with the VBA code I’m putting in.
I have a 2 tables that look like this
tbl.Transactions
Fields (TransactionID (PK), Date, Item (FK-tbl.Item.[Child_SKU]), Type (‘Credit’ or ‘Debit’), QTY)
tbl.Item
Fields (Child_SKU, Title, CurrentOnhand)
What I want to do is calculate the current onhand inventory of an item and update it to [CurrentOnhand] in tbl.Item. At first I created an aggregrate function query to calculate the available onhand. The SQL is below:
SELECT AZ_Credits.Item, Sum([AZ_Credits.QTY]*IIf([Type]="Credit",1,-1)) AS CurrentOnhand
FROM AZ_Credits
GROUP BY AZ_Credits.Item;
I’ve verified that this query is returning the values that I’d like and the math is being done properly. My only problem is I need the results of this query to be put into tbl.Item in the CurrentOnhand field so I can use it for all the purposes I need it for. Not being able to call the results of a sum query into an update query to update, I looked into creating a recordset with VBA. The VBA function below is what I have built in:
Public Sub Code()
Dim cnn1 As ADODB.Connection
Set cnn1 = CurrentProject.Connection
Dim myRecordSet As New ADODB.Recordset
myRecordSet.ActiveConnection = cnn1
Dim mySql As String
mySql = "SELECT AZ_Credits.Item, Sum([AZ_Credits.QTY]*IIf(Type='Credit',1,-1)) AS CurrentOnhand FROM AZ_Credits GROUP BY AZ_Credits.Item"
myRecordSet.Open (mySql)
While Not myRecordSet.EOF
sSQL = "UPDATE AZ_Children SET [CurrentOnhand]=myRecordSet![CurrentOnhand]"
DoCmd.RunSQL (sSQL)
myRecordSet.MoveNext
Wend
End Sub
I think this code is mostly working, but its asking me to enter the Parameter Values for myRecordSet!CurrentOnhand and I’m not sure what’s going on with that.
Anyone have any ideas about what might not be working properly or what I've missed?
I have a 2 tables that look like this
tbl.Transactions
Fields (TransactionID (PK), Date, Item (FK-tbl.Item.[Child_SKU]), Type (‘Credit’ or ‘Debit’), QTY)
tbl.Item
Fields (Child_SKU, Title, CurrentOnhand)
What I want to do is calculate the current onhand inventory of an item and update it to [CurrentOnhand] in tbl.Item. At first I created an aggregrate function query to calculate the available onhand. The SQL is below:
SELECT AZ_Credits.Item, Sum([AZ_Credits.QTY]*IIf([Type]="Credit",1,-1)) AS CurrentOnhand
FROM AZ_Credits
GROUP BY AZ_Credits.Item;
I’ve verified that this query is returning the values that I’d like and the math is being done properly. My only problem is I need the results of this query to be put into tbl.Item in the CurrentOnhand field so I can use it for all the purposes I need it for. Not being able to call the results of a sum query into an update query to update, I looked into creating a recordset with VBA. The VBA function below is what I have built in:
Public Sub Code()
Dim cnn1 As ADODB.Connection
Set cnn1 = CurrentProject.Connection
Dim myRecordSet As New ADODB.Recordset
myRecordSet.ActiveConnection = cnn1
Dim mySql As String
mySql = "SELECT AZ_Credits.Item, Sum([AZ_Credits.QTY]*IIf(Type='Credit',1,-1)) AS CurrentOnhand FROM AZ_Credits GROUP BY AZ_Credits.Item"
myRecordSet.Open (mySql)
While Not myRecordSet.EOF
sSQL = "UPDATE AZ_Children SET [CurrentOnhand]=myRecordSet![CurrentOnhand]"
DoCmd.RunSQL (sSQL)
myRecordSet.MoveNext
Wend
End Sub
I think this code is mostly working, but its asking me to enter the Parameter Values for myRecordSet!CurrentOnhand and I’m not sure what’s going on with that.
Anyone have any ideas about what might not be working properly or what I've missed?