Compare String to make label visible true based on result (1 Viewer)

murray83

Games Collector
Local time
Today, 10:04
Joined
Mar 31, 2017
Messages
728
Morning you lovely people, have been looking at this StrComp Function

and i think i understand it but i cant seem to get it to work, please see my code and explanation below

Code:
Private Sub cmbSKU_AfterUpdate()

Dim LResult As Integer
Dim LResult2 As Integer

LResult = StrComp("me.txtOwnerLookUp.Value", "me.txtSupplierLookUp.Value")
LResult2 = StrComp("me.txtSupplierLookUp.Value", "me.txtOwnerLookUp.Value")

If LResult = 0 Then
Me.lblVendorOwned.Visible = True
Me.lblJSOwned.Visible = False
Else
If LResult2 = 1 Then
Me.lblVendorOwned.Visible = False
Me.lblJSOwned.Visible = True
End If
    End If

Me.Requery

End Sub

so what i want to happen is when the criteria is met like in the example shown below it would show a label saying whether its a JS owned or vendor owned, the criteria for this is. if supplier and owner are different then its JS Owned but as can see that in the example they are both the same so should show the Vendor Owned label

cheers for reading and hope all of us are well
 

Attachments

  • should say vendor not js.jpg
    should say vendor not js.jpg
    4.9 KB · Views: 83

CJ_London

Super Moderator
Staff member
Local time
Today, 10:04
Joined
Feb 19, 2013
Messages
16,607
number of problems with your code

you don't use double quotes here
StrComp("me.txtOwnerLookUp.Value", "me.txtSupplierLookUp.Value")

you don't need to reference the value property or me since these are the defaults

StrComp(txtOwnerLookUp, txtSupplierLookUp)


not sure what your intention is with displaying the labels since 0 means the two lookups are equal and 1 means the supplier lookup has a value greater than the owner lookup - which is a very specific form of 'different'

and you say

they are both the same so should show the Vendor Owned label

but your code says

If LResult = 0 Then ---i.e. they are the same
Me.lblVendorOwned.Visible = True
Me.lblJSOwned.Visible = False

however result1 with your code as it is will return -1

suspect all you need is

lblVendorOwned.Visible=txtOwnerLookUp=txtSupplierLookUp

lblJSOwned.Visible=txtOwnerLookUp<>txtSupplierLookUp
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:04
Joined
May 7, 2009
Messages
19,234
you only need 1 comparison (the other is redundant, you just change their place).
Code:
Private Sub cmbSKU_AfterUpdate()

Dim LResult As Integer

LResult = StrComp(Me.txtOwnerLookUp.Value & "",  me.txtSupplierLookUp.Value & "")

Me.lblVendorOwned.Visible = Not LResult
Me.lblJSOwned.Visible = LResult

'Requery is to fetch "fresh" records.
'Me.Requery
Me.Refresh
End Sub
 

murray83

Games Collector
Local time
Today, 10:04
Joined
Mar 31, 2017
Messages
728
you only need 1 comparison (the other is redundant, you just change their place).
Code:
Private Sub cmbSKU_AfterUpdate()

Dim LResult As Integer

LResult = StrComp(Me.txtOwnerLookUp.Value & "",  me.txtSupplierLookUp.Value & "")

Me.lblVendorOwned.Visible = Not LResult
Me.lblJSOwned.Visible = LResult

'Requery is to fetch "fresh" records.
'Me.Requery
Me.Refresh
End Sub

cheers for that it half works as it shows correct when its vendor owned but when they be different in each it shows both, see example image "Should just say JS Owned"
 

Attachments

  • should just say JS Owned.jpg
    should just say JS Owned.jpg
    6.9 KB · Views: 87
  • thanks this works.jpg
    thanks this works.jpg
    5.6 KB · Views: 89

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:04
Joined
May 7, 2009
Messages
19,234
there are 2 combos? then use two AfterUpdate event on each combo
with same code.

also is the form bound?
if it is, use the Form's Current event:

Private Sub Form_Current()
Call cmbSKU_AfterUpdate
End Sub
 

murray83

Games Collector
Local time
Today, 10:04
Joined
Mar 31, 2017
Messages
728
there are 2 combos? then use two AfterUpdate event on each combo
with same code.
see example attached, no only one combo box
 

Attachments

  • example.accdb
    656 KB · Views: 103

CJ_London

Super Moderator
Staff member
Local time
Today, 10:04
Joined
Feb 19, 2013
Messages
16,607
I'll drop out since my post has been ignored.
 

murray83

Games Collector
Local time
Today, 10:04
Joined
Mar 31, 2017
Messages
728
number of problems with your code

you don't use double quotes here
StrComp("me.txtOwnerLookUp.Value", "me.txtSupplierLookUp.Value")

you don't need to reference the value property or me since these are the defaults

StrComp(txtOwnerLookUp, txtSupplierLookUp)


not sure what your intention is with displaying the labels since 0 means the two lookups are equal and 1 means the supplier lookup has a value greater than the owner lookup - which is a very specific form of 'different'

and you say

they are both the same so should show the Vendor Owned label

but your code says

If LResult = 0 Then ---i.e. they are the same
Me.lblVendorOwned.Visible = True
Me.lblJSOwned.Visible = False

however result1 with your code as it is will return -1

suspect all you need is

lblVendorOwned.Visible=txtOwnerLookUp=txtSupplierLookUp

lblJSOwned.Visible=txtOwnerLookUp<>txtSupplierLookUp
not been ignored working on both but got called away

thanks for the input :)
 

murray83

Games Collector
Local time
Today, 10:04
Joined
Mar 31, 2017
Messages
728
I'll drop out since my post has been ignored.

your code works then it dint, says error and then when i debug it says Null in the lookup section

would i need to refresh the form after each update please see attached copy of db with your code

edit

so compacted and repaired it again ( twice lol ) and it seems to work if you choose from the list but if you where to copy and paste a SKU in then it errors
 

Attachments

  • example 1.accdb
    656 KB · Views: 97

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:04
Joined
May 7, 2009
Messages
19,234
I did not Hide/Unhide the label, instead only 1 label and
change it's Caption.
 

Attachments

  • example.accdb
    648 KB · Views: 102

Users who are viewing this thread

Top Bottom