I've tried to put together some code for a form. This code actually compiles (and I've very proud of that!) but it gives me this error when running and points to line 41 in the code below.
"Run-time error 3122--You tried to execute a query that does not include the specified expression 'ORDER_NUM' as part of an aggregate function."
What I want to do is this:
1. Enter a box number being returned. This will be entered into a dummy field [txtScanCapture] by a scanner, and if it has 3-4 digits, it transfers into txtScan_Box_Num . (For those who have been helping me, I have decided *not* to enter the customer number on box return because it's pointless as far as I can tell.) This BOX_NUM was previously assigned to a [Cust_Num] and [Order_Num] in tblBOX when the box was shipped.
2. Automatically insert date() into [DATE_BOX_RETURN] on subfrmBOX_SHIPP ING which is on frmBOX_SHIPPING .
3. Look in tblORDERS for the [Order_Num] and [Cust_Num] matching the [Box_Num] just scanned. If field [Date_Ret] is empty, insert date(). If it isn't empty, then that implies it was done on a previous box return.
I don't know how mangled my code is but I gave it my absolute best effort. Can you please look at my code and tell me what all might be wrong with it? I'm not sure if I have my If statments correctly nested or if my strsql is in the right place. Note I have two uses of strsql--is that ok? These are the things I'm just not sure on. Suggestions to eliminate my run-time error will also be greatly appreciated!
PS. For those who have helped on a similiar problem, I changed the names of two fields on the main form...since I won't be scanning the cust_num I changed the names of two fields, taking out the "Scan" reference.
Also, I've got a couple of open posts that I need to kind of wrap-up/close but I want to get all similar problems solved so I can put my final solutions in. Things might still change slightly. ;-)
Thanks to those of you who continue to help my pitiful self. hahaha.
"Run-time error 3122--You tried to execute a query that does not include the specified expression 'ORDER_NUM' as part of an aggregate function."
What I want to do is this:
1. Enter a box number being returned. This will be entered into a dummy field [txtScanCapture] by a scanner, and if it has 3-4 digits, it transfers into txtScan_Box_Num . (For those who have been helping me, I have decided *not* to enter the customer number on box return because it's pointless as far as I can tell.) This BOX_NUM was previously assigned to a [Cust_Num] and [Order_Num] in tblBOX when the box was shipped.
2. Automatically insert date() into [DATE_BOX_RETURN] on subfrmBOX_SHIPP ING which is on frmBOX_SHIPPING .
3. Look in tblORDERS for the [Order_Num] and [Cust_Num] matching the [Box_Num] just scanned. If field [Date_Ret] is empty, insert date(). If it isn't empty, then that implies it was done on a previous box return.
I don't know how mangled my code is but I gave it my absolute best effort. Can you please look at my code and tell me what all might be wrong with it? I'm not sure if I have my If statments correctly nested or if my strsql is in the right place. Note I have two uses of strsql--is that ok? These are the things I'm just not sure on. Suggestions to eliminate my run-time error will also be greatly appreciated!
PS. For those who have helped on a similiar problem, I changed the names of two fields on the main form...since I won't be scanning the cust_num I changed the names of two fields, taking out the "Scan" reference.
Also, I've got a couple of open posts that I need to kind of wrap-up/close but I want to get all similar problems solved so I can put my final solutions in. Things might still change slightly. ;-)
Code:
Option Explicit
Option Compare Database
Public strLastScan As String
Public db As DAO.Database
Private Sub Form_Open(Cancel As Integer)
Set db = CurrentDb
End Sub
Private Sub txtScanCapture_AfterUpdate()
Dim strSQL As String
Select Case Len(Me.txtScanCapture)
Case 3, 4
'Box
'Is box registered in database?
If DCount("BOX_NUM", _
"tblBOX", _
"BOX_NUM='" & Me.txtScanCapture & "'") = 0 Then
'Box does not exist in DB
MsgBox "Box " & Me.txtScanCapture & " is not a valid box number."
Else
Me.txtScan_Box_Num = Me.txtScanCapture
'Box exists.
'Set received date=now
strSQL = "UPDATE tblBOX " & _
"SET [DATE_BOX_SHIP]=Date()" & _
"WHERE ([BOX_NUM]='" & Me.txtScanCapture & "')"
DoCmd.SetWarnings (False)
DoCmd.RunSQL strSQL
DoCmd.SetWarnings (True)
Me.subfrmBOX_RECEIVING.Requery
'Use the box_num to obtain the cust_num & order_num from tblBOX
'but I don't know how to store the values to use in the .FindFirst below
strSQL = "SELECT [CUST_NUM]" & _
", [ORDER_NUM]" & _
"FROM tblBOX " & _
"WHERE [BOX_NUM]='" & Me.txtScanCapture & "'" & _
"GROUP BY [CUST_NUM]"
With db.OpenRecordset(strSQL, dbOpenSnapshot)
If .RecordCount = 0 Then
MsgBox "There is no record of this box shipping"
'Do whatever you want to handle this case
Else
Me.txtScan_Box_Num = !BOX_NUM
Me.tb_Cust_Num = !CUST_NUM
Me.Max_ORDER_NUM = !ORDER_NUM
End If
Call .Close
End With
End If
'Update the DATE_RET in tblOrders where necessary
With db.OpenRecordset("tblORDERS", dbOpenDynaset)
Call .FindFirst("[Order_Num]='" & Me.Max_ORDER_NUM & "'")
If Not .NoMatch Then
If IsNull(![DATE_RET]) Then
Call .Edit
![DATE_RET] = Date
Call .Update
End If
End If
Call .Close
End With
strLastScan = "Box"
Case Else
'Some sort of error or user error
MsgBox "Box Numbers can only be 3 or 4 digits."
End Select
Me.txtScanCapture = ""
End Sub
Comment