Question : How can I convert or cast data in a query to an Access Database?

I need to query an Access database and the query involves joining two tables.
The joining column on table1 (named ID) has a datatype of 'Number: Long integer'.  The joining column on table2 (named PartInfo)has a datatype of 'Text'.  The PartInfo contains data that is the string representation of the ID column.  I'm not sure why the PartInfo column uses a text datatype but the db is output from another application so I have no control over it.  When I write my query, if I use CAST, then I get an error "IErrorInfo.GetDescription failed with E_FAIL(0x80004005)"..  If I use CONVERT, then I get an
error "Undefined function 'CONVERT' in expression."  I've researched and found that the E_FAIL error is thrown when a Keyword is used in the query, but as you can see, I've used brackets around all of my table and field names.  I've also found that the Jet engine does not support the CONVERT function.  How can I write this query so that I can join the two tables by these two columns?  The error is thrown on the line: MyReader = MyConmand.ExecuteReader

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
'Using CONVERT:
                MyCommand = New OleDbCommand("", MyConnection)
                MyCommand.CommandText = String.Format("Select CONVERT(smallint, [Label Sequence:{0}].[PartInfo]) as pi, From [{0}] Inner Join [Label Sequence:{0}] On [{0}].[ID] = [Label Sequence:{0}].[ID]", s)
                MyReader = MyCommand.ExecuteReader
 
'Using CAST:
                MyCommand = New OleDbCommand("", MyConnection)
                MyCommand.CommandText = String.Format("Select CAST([Label Sequence:{0}].[PartInfo] AS smallint) as pi, From [{0}] Inner Join [Label Sequence:{0}] On [{0}].[ID] = [Label Sequence:{0}].[ID]", s)
                MyReader = MyCommand.ExecuteReader
 
'Using CAST (with a comma instead of AS):
                MyCommand = New OleDbCommand("", MyConnection)
                MyCommand.CommandText = String.Format("Select CAST([Label Sequence:{0}].[PartInfo], smallint) as pi, From [{0}] Inner Join [Label Sequence:{0}] On [{0}].[ID] = [Label Sequence:{0}].[ID]", s)
                MyReader = MyCommand.ExecuteReader

Answer : How can I convert or cast data in a query to an Access Database?

It might be best to use CStr (ID 21063117 above), so that you won't get a 'Data type mismatch' error if [Label Sequence:{0}] happens to have records with non-numeric [ID] values.
Random Solutions  
 
programming4us programming4us