0

Working within Access 2007. Struggling to have database give a real time, updated amount of inventory on hand based on shipments/receipts. I was recommended the following code, but it is throwing a compile error.

Private Sub Command62_Click()
'Arguments: ContID = Container to be reported on
' AsOfDate = the date at which quantity is to be calculated
'If missing, all transactions are included
' Return: Quantity on hand. Zero on error.

Dim db As DAO.Database   'current database
Dim rs As DAO.Recordset    ' various recordsets

Dim lngContID As Long       ' ContID as long
Dim strAsOf As String       ' AsOfDate as string
Dim strLasShipRec As String   ' Last ship date as string
Dim strDateClause As String    'Date clause to be used in SQL statement
Dim strSQL As String            ' SQL statement
Dim lngOnHand As Long     'IOnHand inventory

If Not IsNull(CONTID) Then
'Initialize: Validate and Convert parameters

Set db = CurrentDb()
lngContID = CONTID
If IsDate(AsOfDate) Then
strAsOf = "#" & Format$(AsOfDate, "mm\/dd\/yyyy") & "#"

'Get the LastShipRec date and quantity fo this container

If Len(strAsOf) > 0 Then
strDateClause = "AND (LastShipRec<="&strAsOf&")"
End If

strSQL = "Select Top 1 LastShipRec, Quantity FROM tblContInventory"& "WHERE ((ContID = "&lngContID&")"&strDateClause)&";ORDER BY LastShipRec DESC;"

It is giving an 'end of statement expected' compile at:

strSQL = "Select Top 1 LastShipRec, Quantity FROM tblContInventory"& "WHERE ((ContID = "&lngContID&")"&strDateClause)&";ORDER BY `enter`LastShipRec DESC;"

AND

If Len(strAsOf) > 0 Then

strDateClause = "AND (LastShipRec<="&strAsOf&")"
End If

Would someone be able to point me in the correct direction? How do I correct this error?

2 Answers 2

1

The line where strSQL = ... is attempting to build a long string. Looking through this long statement, it looks like a parenthesis is out of place and there's an extra semicolon in the first line. Try

strSQL = "Select Top 1 LastShipRec, Quantity FROM tblContInventory"& "WHERE ((ContID = "&lngContID&")" & strDateClause & ") ORDER BY LastShipRec DESC;"

When building a sql string, i recommend creating the string and then doing debug.print for debugging so as to see if the string created is a valid SQL statement.

Sign up to request clarification or add additional context in comments.

1 Comment

This SQL statement also needs a space between tblContInventory and WHERE. Try replacing ... tblContInventory"& "WHERE ... with tblContInventory WHERE ....
0

remove the bracket from after strDateClause, looks like it needs to go in the ""'s also your SQL has terminators ; before the end. Looking at it, you don't use the strDateClause

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.