Erro on VB "Expecting an existing scalar var"

5.1k views Asked by At

I was trying to make an function using vb. This function is to collect data for a report. Follows the example:

Public Function HeaderData() As String
Dim dados As String
dados = ""  
If Not(frm.FormQuery.FieldByName("A").AsString  = "S" And _
     frm.FormQuery.FieldByName("B").AsString  = "S" And _
     frm.FormQuery.FieldByName("C").AsString  = "S" And _
     frm.FormQuery.FieldByName("D").AsString  = "S") Then

dados = dados + "Type: "

If frm.FormQuery.FieldByName(A).AsString = "S" Then
  dados = dados + "A"
End If

If frm.FormQuery.FieldByName(B).AsString = "S" Then
  If frm.FormQuery.FieldByName(A).AsString = "S" Then
    dados = dados + ", "
  End If
  dados = dados + "B"
End If

If frm.FormQuery.FieldByName(C).AsString = "S" Then
  If frm.FormQuery.FieldByName(A).AsString = "S" Or _
     frm.FormQuery.FieldByName(B).AsString = "S" Then
     dados = dados + ", "
  End If
  dados = dados + "C"
End If

 If frm.FormQuery.FieldByName(D).AsString = "S" Then
  If frm.FormQuery.FieldByName(A).AsString = "S" Or _
     frm.FormQuery.FieldByName(B).AsString = "S" Or _
     frm.FormQuery.FieldByName(C).AsString = "S" Then
     dados = dados + ", "
  End If
  dados = dados + "D"
End If

dados =  dados + "; "

And this function causes the error: Expecting an existing scalar var.

I did other functions with the same purpose, with similar code, using the same variable to collect the data (dados) and did not cause the error. What could be happening?

1

There are 1 answers

0
the_lotus On

Your logic doesn't make much sense. I highly suggest that you write just a few lines and debug. Make sure everything works properly and than write a few more lines. Writing to much at the beginning will cause trouble.

    Dim possibleFields As String() = {"A", "B", "C", "D"}
    Dim foundFields As New List(Of String)
    Dim dados As String = ""

    For Each field As String In possibleFields
        If frm.FormQuery.FieldByName(field).AsString = "S" Then
            foundFields.Add(field)
        End If
    Next

    If foundFields.Count > 0 Then
        dados = String.Format("Type: {0}; ", String.Join(", ", foundFields.ToArray()))
    End If