-1

I'm working on a project that displays text on a monitor as a reference but running into some logic trouble. Someone clicks a button which prompts an InputBox into which they insert a bar code with a scanner.

I have 2 types of part numbers, one is like this "11n11110mch" the other is something like this "12311110mch". I need code that tests whether the 3rd character is a number. The end goal is to display the number as "11.(some letter)111.10 MCH" and "123.111.10 MCH" in a TextBox. If I try "11n22210mch", I get an error that says

Conversion from string "n" to type 'Double' is not valid.

at

thirdChara = Mid$(VisPartID, 3, 1)

I am not sure how to correct this or accomplish what I am trying to do.

The code I have:

Public Sub btnScan_Click(sender As Object, e As EventArgs) Handles btnScan.Click
    Dim ScanIDRaw As Object

    'Clear Scan Value
    ScanIDRaw = Nothing
    'Display message, title, And default value.                         
    ScanIDRaw = InputBox("Scan CDI", "InputBox")
    Do Until ScanIDRaw IsNot ""
        ScanIDRaw = InputBox("Part Number Needed, Scan CDI", "InputBox")
    Loop

    lblCDIPart.Text = ScanIDRaw
    HUD.ReferenceCardDataPull()
End Sub

Public Async Sub ReferenceCardDataPull()
    Dim PartID As String
    Dim VisPartID As String
    Dim thirdChara As String

    'Other Code 
    'Something
    'Something
    VisPartID = Main.lblCDIPart.Text
    thirdChara = Mid$(VisPartID, 3, 1)
    If thirdChara = Not IsNumeric(thirdChara) Then
        VisPartID = VisPartID.Insert(2, ".")
        VisPartID = VisPartID.Insert(7, ".")
        VisPartID = VisPartID.Insert(10, " ")
        VisPartID = VisPartID.ToUpper
        lblPart.Text = VisPartID
    Else
        VisPartID = VisPartID.Insert(3, ".")
        VisPartID = VisPartID.Insert(8, ".")
        VisPartID = VisPartID.Insert(11, " ")
        VisPartID = VisPartID.ToUpper
        lblPart.Text = VisPartID
    End If
End Sub
Blackwood
  • 4,504
  • 16
  • 32
  • 41
holi4683
  • 101
  • 1
  • 4
  • 14

1 Answers1

0

If Not isNumeric(thirdChara) Then

But you really should be using VB.Net methods instead of the old VB6 style

dim position3 as integer
thirdChara = VisPartID.SubString(2,1) ' 2 because we're 0 based
if not integer.tryparse(thirdChara, position3) then
  'do stuff if not a number
else
  'it's a number
end if

The tryparse will return a boolean based upon the parse result. Upon successful parse, it will store the parsed value into the variable (in this case Position3)

I would also recommend turning on Option Strict as that would have informed you right away that you cannot implicitly convert a string to a boolean.

Meaning: The string thirdChara is being tested for equality against as Not (True|False) result from the IsNumeric method.

Charles May
  • 1,725
  • 1
  • 11
  • 18
  • How did you fix your If statement? because it sounds like you left it the same as you originally had instead of fixing it with TryParse or at the very least, fixing it to the first line of my answer. – Charles May Feb 23 '16 at 15:02
  • Got it! thank you. I am Im new to vb.net and used to vba, syntax is killing me on this project. Can you explain the whole `SubString(2,1)` thing? whats the 2 and 1 mean? – holi4683 Feb 23 '16 at 15:03
  • If you click on it and press F1 it will open an MSDN page expalinging everything. But it's basically just like the Mid$ function except that the first index is 0 instead of 1. So you are saying take a substring of VisPartID starting at the 2nd index (3rd position of the string) and take 1 character. – Charles May Feb 23 '16 at 15:05