Searching for record using multiple criteria (1 Viewer)

Cirrostratus

Registered User.
Local time
Today, 12:18
Joined
May 16, 2013
Messages
29
I have a form where a user enters data. One of the things the user enters is a "lot size". I need this lot size field to be checked against a query in the database to determine if the quantity is acceptable or not.

The order number in this query is in certain cases missing a leading zero, so I need to truncate this from the user entered form field.

Finally I want the user to be notified if they are trying to exceed the lot size in the query.

My code is as follows:

Code:
Public Function RemoveFirstChar(RemFstChar As String) As String
Dim TempString As String
 
          TempString = RemFstChar
            If Left(RemFstChar, 1) = "0" Then
                If Len(RemFstChar) > 1 Then
                    TempString = Right(RemFstChar, Len(RemFstChar) - 1)
                End If
            End If
RemoveFirstChar = TempString
End Function
 
 
Private Sub Lot_Size_AfterUpdate()
 
          Dim strWhere As String
 
          strWhere = "([Order_No]=""& RemoveFirstChar(Forms!frmProdStartEntry!Order_No) & "") AND ([Release_No]=""& Forms!frmProdStartEntry!Release_No &"") AND ([Sequence_No]=""& Forms!frmProdStartEntry!Sequence_No &"")"
 
          Buildable = DLookup("[UnitsOpen]", "SQ_ProdUnitsBuildable", strWhere)
 
        If Forms!frmProdStartEntry!Lot_Size > Buildable Then
          MsgBox "You can only build " & Buildable & "pcs, please revise qty!", vbInformation, ""
          Cancel = True
          Me.Lot_Size.SetFocus
          Exit Sub
        End If
 
          btnStart.SetFocus
End Sub

I cant get this code to work :banghead:. Can you guys help me out and point out what is wrong with it? All the help is greatly appreciated!!
 

TJPoorman

Registered User.
Local time
Today, 10:18
Joined
Jul 23, 2013
Messages
402
What errors are you getting? And what line is the code breaking on?
 

Cirrostratus

Registered User.
Local time
Today, 12:18
Joined
May 16, 2013
Messages
29
No errors, it just doesn't return anything. If I put a qty larger than allowed, it still lets me proceed. No message box pop's up. :confused:
 

Cirrostratus

Registered User.
Local time
Today, 12:18
Joined
May 16, 2013
Messages
29
I guess there is a couple of things that could be going wrong here. The RemoveFirstChar function, I'm not sure if that works inside the dlookup function?

And the Buildable, does it need to be defined or is my message box syntax wrong?

Thanks again!
 

JHB

Have been here a while
Local time
Today, 18:18
Joined
Jun 17, 2012
Messages
7,732
The strWhere is wrong:
strWhere = "([Order_No]=""& RemoveFirstChar(Forms!frmProdStartEntry!Order_No) & "") AND ([Release_No]=""& Forms!frmProdStartEntry!Release_No &"") AND ([Sequence_No]=""& Forms!frmProdStartEntry!Sequence_No &"")"
If the criteria is number then:
[ANumberField]=" & NumberCriteria
If the criteria is Text then:
[ATextField]='" & TextCriteria & "'"
Or:
[ATextField]=" & "'" & TextCriteria & "'"
 

Cirrostratus

Registered User.
Local time
Today, 12:18
Joined
May 16, 2013
Messages
29
all of those fields are text, so I changed it to:

strWhere = "([Order_No]='"& RemoveFirstChar(Forms!frmProdStartEntry!Order_No) & "'") AND ([Release_No]='"& Forms!frmProdStartEntry!Release_No &"'") AND ([Sequence_No]='"& Forms!frmProdStartEntry!Sequence_No &"'")"

But now VB is giving a compile error message, "expected end of statement" at the closing parenthesis that I have highlighted in bold. Something is still not right.. :confused:
 

pr2-eugin

Super Moderator
Local time
Today, 17:18
Joined
Nov 30, 2011
Messages
8,494
Try this..
Code:
strWhere = "([Order_No]='" & RemoveFirstChar(Forms!frmProdStartEntry!Order_No) & "'") AND ([Release_No]='" & Forms!frmProdStartEntry!Release_No & "'") AND ([Sequence_No]='" & Forms!frmProdStartEntry!Sequence_No & "'")"
 

Cirrostratus

Registered User.
Local time
Today, 12:18
Joined
May 16, 2013
Messages
29
Try this..
Code:
strWhere = "([Order_No]='" & RemoveFirstChar(Forms!frmProdStartEntry!Order_No) & "'") AND ([Release_No]='" & Forms!frmProdStartEntry!Release_No & "'") AND ([Sequence_No]='" & Forms!frmProdStartEntry!Sequence_No & "'")"

Yeah, that's still giving the same error message. Very strange?!?
 

pr2-eugin

Super Moderator
Local time
Today, 17:18
Joined
Nov 30, 2011
Messages
8,494
Are all the controls on the same Form from which this code is run? Or is that another Form?
 

pr2-eugin

Super Moderator
Local time
Today, 17:18
Joined
Nov 30, 2011
Messages
8,494
So just refer to the Controls by using Me. rather than using Forms!FormName!ControlName
Code:
strWhere = "([Order_No]='" & RemoveFirstChar([B]Me.[/B]Order_No) & "'") AND ([Release_No]='" & [B]Me.[/B]Release_No & "'") AND ([Sequence_No]='" & [B]Me.[/B]Sequence_No & "'")"
 

TJPoorman

Registered User.
Local time
Today, 10:18
Joined
Jul 23, 2013
Messages
402
Try this..
Code:
strWhere = "([Order_No]='" & RemoveFirstChar(Forms!frmProdStartEntry!Order_No) & "'") AND ([Release_No]='" & Forms!frmProdStartEntry!Release_No & "'") AND ([Sequence_No]='" & Forms!frmProdStartEntry!Sequence_No & "'")"

your quotes are in the wrong place. it's expecting the end of statement.
Try this:
Code:
strWhere = "([Order_No]='" & RemoveFirstChar(Forms!frmProdStartEntry!Order_No) & "') AND ([Release_No]='" & Forms!frmProdStartEntry!Release_No & "') AND ([Sequence_No]='" & Forms!frmProdStartEntry!Sequence_No & "')"
 

pr2-eugin

Super Moderator
Local time
Today, 17:18
Joined
Nov 30, 2011
Messages
8,494
Yes TJPoorman, you are right.. How did I miss that.. :eek:
 

Cirrostratus

Registered User.
Local time
Today, 12:18
Joined
May 16, 2013
Messages
29
That fixed the compile error message, thanks!

But unfortunately the program still doesn't work. If the user enters into the lot_size field a qty larger than the "buildable" it still doesn't produce the msgbox. Does the "buildable" need to be defined? if so how? and can I use that then in the msgbox?

Also, should the RemoveFirstChar function work inside the strWhere?

Thanks again!! We are making progress!
 

TJPoorman

Registered User.
Local time
Today, 10:18
Joined
Jul 23, 2013
Messages
402
You should define Buildable. Try setting a breakpoint on your If statement and hover over the 2 fields to make sure that they are setting correctly.
 

pr2-eugin

Super Moderator
Local time
Today, 17:18
Joined
Nov 30, 2011
Messages
8,494
Okay lets do some debugging..
Code:
Private Sub Lot_Size_AfterUpdate()
    Dim strWhere As String, Buildable As Long

    strWhere = "([Order_No]='" & RemoveFirstChar(Me.Order_No) & "') AND ([Release_No]='" & Me.Release_No & "') AND ([Sequence_No]='" & Me.Sequence_No & "')"

    Buildable = Nz(DLookup("[UnitsOpen]", "SQ_ProdUnitsBuildable", strWhere), 0)
    [COLOR=Red][B]MsgBox Buildable[/B][/COLOR]
    
    If Forms!frmProdStartEntry!Lot_Size > Buildable Then
        MsgBox "You can only build " & Buildable & "pcs, please revise qty!", vbInformation, ""
        Cancel = True
        Me.Lot_Size.SetFocus
        Exit Sub
    End If
    btnStart.SetFocus
End Sub

Public Function RemoveFirstChar(RemFstChar As String) As String
    Dim TempString As String
    TempString = RemFstChar
    If Left(RemFstChar, 1) = "0" Then
        If Len(RemFstChar) > 1 Then
            TempString = Right(RemFstChar, Len(RemFstChar) - 1)
        End If
    End If
    RemoveFirstChar = TempString
End Function
See what you get in the MsgBox..
 

Cirrostratus

Registered User.
Local time
Today, 12:18
Joined
May 16, 2013
Messages
29
Ok, I tried defining "Buildable" but it came back with an error message on the Buildable = where it said incorrect use of Null or something like that.

Public Function RemoveFirstChar(RemFstChar As String) As String
Dim TempString As String

TempString = RemFstChar
If Left(RemFstChar, 1) = "0" Then
If Len(RemFstChar) > 1 Then
TempString = Right(RemFstChar, Len(RemFstChar) - 1)
End If
End If
RemoveFirstChar = TempString
End Function


Private Sub Lot_Size_AfterUpdate()

Dim strWhere As String
Dim Buildable As Long

strWhere = "([Order_No]='" & RemoveFirstChar(Forms!frmProdStartEntry!Order_No) & "') AND ([Release_No]='" & Forms!frmProdStartEntry!Release_No & "') AND ([Sequence_No]='" & Forms!frmProdStartEntry!Sequence_No & "')"

Buildable = DLookup("[UnitsOpen]", "SQ_ProdUnitsBuildable", strWhere)

If Forms!frmProdStartEntry!Lot_Size > Buildable Then
MsgBox "You can only build " & Buildable & "pcs, please revise qty!", vbInformation, ""
Cancel = True
Me.Lot_Size.SetFocus
Exit Sub
End If

btnStart.SetFocus
End Sub
 

TJPoorman

Registered User.
Local time
Today, 10:18
Joined
Jul 23, 2013
Messages
402
not sure if it will change anything but try removing the brackets in your DLookup:

Buildable = DLookup("UnitsOpen", "SQ_ProdUnitsBuildable", strWhere)
 

pr2-eugin

Super Moderator
Local time
Today, 17:18
Joined
Nov 30, 2011
Messages
8,494
If you had used the code I have in post#16 you should have atleast got a 0 in msgbox.. Could you copy the code as such and try again please..
 

Users who are viewing this thread

Top Bottom