Field type vbscript.


I run VBScript from Outlook to populate a text field.  I'm grabbing a value from a SQL table:

ctls("lblPartDesc").Caption = "Part Desc: " & rst.Fields("description")

When the field in SQL is a text field, it gives a data type mismatch error.  But when it is a varchar type it's fine.  I've tried cstr() and it still doesn't work.  Also, I'm not allow to change the field in the sql table to varchar because it's not my database.

Thanks, Joel
Who is Participating?
Genius123Connect With a Mentor Author Commented:
This did not work, however, when I convert the field to a varchar it works.  Here's the code:

sSQL = "SELECT compID,part_number, CONVERT(varchar(255), description) as descrip FROM framing_components WHERE part_number LIKE '%"& Trim(Item.UserProperties("PartNameSearch").value) & "%' or description LIKE '%"& Trim(Item.UserProperties("PartNameSearch").value) & "%' ORDER BY part_number"
David Johnson, CD, MVPOwnerCommented:
in the table what is the data type? given that what do you need?
Anthony PerkinsCommented:
When the field in SQL is a text field, it gives a data type mismatch error.
Can you confirm that the "description" column is in fact using the deprecated text data type?

If so, we may need to change your code where you SELECT it.  I suspect you are using SELECT *, right?

In any case:
1,  Post your code including the SELECT statement.
2. Tell us what data provider you are using.  If you don't know what that means just post the connection string (obviously obfuscating any sensitive info)
3. Post the schema to your table (CREATE TABLE statement).
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

Genius123Author Commented:
Certainly.  Please see the attached code.  The SQL field called "description" is a text data type.

select Case Name
case "PartName"

Dim DesciptionSQL

  Set ins = Item.GetInspector
  Set pgs = ins.ModifiedFormPages
  Set pg = pgs("Inventory")
  Set ctls = pg.Controls


Set oADOConn = Nothing
Set oADOConn = CreateObject("ADODB.Connection")
If TypeName(oADOConn) = "Nothing" Then
	'MsgBox "Error"
	'Exit Function
End if

sConnectionString = "Driver={SQL Server}" & _
";Server=SQL02" & _
";Database=TGPInventory" & _
";User ID=inventoryadmin;Password=Wirelite!NT" & _

'oADOConn.Mode = adModeWrite
oADOConn.ConnectionString = sConnectionString
If err.Number <> 0 Then
	'MsgBox "(Error: " & err.Number & "/" & err.Description & ")"
	'Exit Function
End If

PartNameParse = split(Item.UserProperties("PartName"), " - ")

    sSQL = "SELECT * FROM framing_components WHERE compID = '" & PartNameParse(0) & "'"

Set rst = Nothing
Set rst = CreateObject("ADODB.Recordset")

rst.Open sSQL, oADOConn, adOpenForwardOnly	'for very fast reads

msgbox "asdf" & rst.Fields("description") & "asdf"

ctls("lblPartDesc").Caption = cstr(rst.Fields("description"))
'ctls("lblPartDesc").Caption = "Part Desc: " & rst.Fields("description")
ctls("lblQTY").Caption = "Part Desc: " & FormatCurrency(rst.Fields("l_cost"))

Set rst = Nothing
Set sSQL = Nothing
Set oADOConn = Nothing
end select

Open in new window

Anthony PerkinsCommented:
Exactly, as I guessed, you are using SELECT *.  This is never a good idea and as you have discovered is fatal with BLOBs.

Solution:  Change SELECT * to SELECT Col1, Col2, Col3 and place all your BLOBs at the end.  So in your case instead of this:
sSQL = "SELECT * FROM framing_components WHERE compID = '" & PartNameParse(0) & "'"

Do this:
sSQL = "SELECT l_cost, description FROM framing_components WHERE compID = '" & PartNameParse(0) & "'"
Genius123Author Commented:
Solved myself.  Thanks to others for help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.