×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How do you map a user-defined data type to a string
3

How do you map a user-defined data type to a string

How do you map a user-defined data type to a string

(OP)
I've defined a user-defined data type as per the online help example:

Type EmployeeRecord ' Create user-defined type.
ID As Integer ' Define elements of data type.
Name As String * 20
Address As String * 30
Phone As Long
HireDate As Date
End Type

and declared a variable:


Dim MyRecord As EmployeeRecord

Now I want to map the entire structure to a string variable (the string variable is required by a subsequent function)

Dim Somestring as String

This statement:

Somestring = MyRecord

Yields (as one would expect) a run-time type mismatch error. Is there a method of coding this to avoid such an error?

Thanks.

RE: How do you map a user-defined data type to a string

Microsoft sez...
Only public user defined types defined in public object modules can be used as parameters or return types for public procedures of class modules or as fields of public user defined types.
You attempted to use a public user defined type as a parameter or return type for a public procedure of a class module, or as a field of a public user defined type.
Only public user defined types that are defined in a public
object module can be used in this manner.

These restrictions can be bypassed with the slow and ungainly disk "method":

Dim MyRecord As EmployeeRecord
Dim Somestring as String
Somestring=String$(Len(MyRecord),0)
ff = FreeFile
Open "Filename.Ext" For Binary As #ff
Put #ff, 1, MyRecord
Get #ff, 1, SomeString
Close #ff

But why would you want to do that when Microsoft makes the conversion so "straightforward" and "easy"?

RE: How do you map a user-defined data type to a string

VB does not support your problem directly. However, there is a Windows API -lstrcpyn - that does. The following example comes from a working application:


Declare Function CpyUdt2Str Lib "Kernel32" Alias "lstrcpyn" (ByVal lpString1 As String, lpString2 As Any, ByVal cChars As Integer) As Integer

Type CntrlRec
AcctCd As String * 10
PlanNum As String * 10
PlanEffDt As String * 8
PlanExpDt As String * 8
ValDt As String * 8
CalcDt As String * 8
EPDt As String * 8
StatusInd As String * 1
AdjTypeCd As String * 1
AdjNum As String * 2
LastModDt As String * 8
ModSectNo As String * 20
Comments As String * 50
AnalystInit As String * 3
gstrDOSFilename As String * 12
filler As String * 15
End Type

Global SelectedCntlRec As CntrlRec

Static TempKey As String * 66, SearchKey As String * 66

retval = CpyUdt2Str(SearchKey, SelectedCntlRec, 53)


in this example, the first 53 bytes of the user defined data typpe are copied to a string variable. We also use the API to copy into strings and to copy user data types to user data types. The function statements follow:

Declare Function CpyStr2Udt Lib "Kernel32" Alias "lstrcpyn" (lpString1 As Any, ByVal lpString2 As String, ByVal cChars As Integer) As Integer


Declare Function CpyUdt2Udt Lib "Kernel32" Alias "lstrcpyn" (lpString1 As Any, lpString2 As Any, ByVal cChars As Integer) As Integer

RE: How do you map a user-defined data type to a string

You have my vote for tipmaster but.... have your stopwatch ready? The crude and unpure method seems to be faster when dealing with less than 100,000 records.

RE: How do you map a user-defined data type to a string

You can create another user data type (UDT) that has one string field that is as long as the UDT you're using then copy your UDT to the new one.

Type EmployeeData
myData as String * n
End Type

In your code, you would declare two variables - one for each UDT:

Dim EmpRecord as EmployeeRecord
Dim EmpData as EmployeeData

Once you have your data in EmpRecord, do the following to copy it to a string:

LSet EmpData = EmpRecord
myString = EmpData.myData

Don't forget to declare myString:
Dim myString as String

The trick is to determine the length of your UDT.

' String = 2 bytes
' Long = 4 bytes = String * 2
' Integer = 2 bytes = String * 1
' Double = 8 bytes = String * 4
' Date = 8 bytes = String * 4
' Currency = 8 bytes = String * 4
' Boolean = 1 byte = String * 1

' Add all non-String lengths and divide by 2 (round up)
' then add to length of all string fields

In your case, myData As String * 57

I hope this helps!

RE: How do you map a user-defined data type to a string


What I've done in the past is not really use public 'type's anymore -- I've switched to using classes. So I would declare a class like this:

Option Explicit

Private m_ID As Integer ' Define elements of data type.
Private m_Name As String * 20
Private m_Address As String * 30
Private m_Phone As Long
Private m_HireDate As Date

Public Property Let ID(RHS As Integer)
m_ID = RHS
End Property
Public Property Get ID() As Integer
ID = m_ID
End Property
'
' other get/lets go here
'

Public Property Get AsString() As String
Dim szTemp As String

On Error GoTo AsStringError

szTemp = Trim$(CStr(m_ID))
szTemp = szTemp & m_Name
szTemp = szTemp & m_Address
szTemp = szTemp & Trim$(CStr(m_Phone))
szTemp = szTemp & Trim$(CStr(m_HireDate))

AsString = szTemp
Exit Property

AsStringError:
AsString = ""
End Property

You probably need to decide on a format for the HireDate, since the CStr will return it in the format that the machine's locale is set to. I would suggest YYYYMMDD, since it sorts well, is Y2K ready, and is fairly obvious that it's a date.

Chip H.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close