2013-07-20 48 views
0

如何檢查是否文本框包含scaned因此,如果是的話,我會自動增加量是這樣的前一個項目的同一條碼:VISUAL BASIC - 掃描同一項目

Dim nextItemBarcode As String = Me.txtBarkodi.Text 
Dim quantity As Integer = 1 

If Me.txtBarkodi.Text = nextItemBarcode Then 
    quantity += 1 
Else 
    quantity = 1 
End If 

你以爲我是缺少某些東西,或者這種情況下可能會有更好的算法?

+2

這看起來不像VBA –

+0

您是否將此標記爲Visual Basic?您將nextItemBarcode =設置爲文本框的值,然後測試它們是否相等,這將始終爲真。您需要從保存最後一個項目的文本框以外的地方獲取nextItemBarcode。一般來說,您希望將掃描值存儲在文本框中,然後將lastItemScanned =設置爲文本框值,然後當發生新掃描並存儲在文本框中時,將其與lastItemScanned進行比較。類似的東西。 –

回答

1

你錯過了什麼。 :-)

您需要存儲最後條碼的值,而不是下一個條碼。如果新的與最後一個相同,則增加數量。如果不相同,則將數量重置爲1,並將新條碼存儲爲最後一個條碼。

Dim lastItemBarcode As String = "" 
Dim quantity As Integer 

' Scan now. If this is the first bar code, 
' quantity will be set to 1 in the Else branch below 
' Your scan should work in a loop starting here, so 
' it keeps going and doesn't reset lastItemBarcode 
' or quantity 
If Me.txtBarkodi.Text = lastItemBarcode Then 
    ' bar code same as last. Just increase quantity 
    quantity += 1 
Else 
    ' Either the first item scanned, or a different 
    ' item. Save this bar code as the current one, 
    ' and start our queantity at 1 
    lastItemBarcode = me.txtBarkodi.Text 
    quantity = 1 
End If 
+0

非常感謝您,先生,這真的很有幫助 – Milot25