Discussion Locked

Data types & fields in Access 2000

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Is it possible to make a "virtual" data type conversion in Access 2000? E.g. if a field is of the "long integer" data type and we want to relate it to a "text" (string) field in another table, in order to be able to join and query both tables, can we do that programmatically without changing the data types of the two related fields?

3 replies

by steved

Unlike SQL Server, Access won't automatically convert data types when you write a join on a string field with a numeric field. It also doesn't seem to allow the VBA function CStr in Access SQL, but it does allow user-defined functions. So here's onesolution.

Say you have these two tables:

TableA
------
KeyA text (10)
Data1 text (50)

TableB
------
KeyB long
DataB text (50)

First, create a VBA module in your Access db. Create a function like this:

Function ConvStr(aAs Long)
ConvStr = CStr(a)
End Function

Then you can use the function in a query to create what amounts to a new view of TableB, with the KeyB field converted to a string, like this:

QueryB
------
SELECT ConvStr([KeyB]) AS StrB, DataBFROM TableB;

Finally, create a second query to join the first query with TableA:

QueryA
------
SELECT TableA.KeyA, TableA.Data1, QueryB.DataB
FROM TableA INNER JOIN QueryB
ON TableA.KeyA = QueryB.StrB;

I hope this helps.

Steve Diamond

by Former member

This question was closed by the author

This discussion is closed to new replies.