Moving a string into a User defined type

I am dealing with a file with a variable length record.   I can't use a get to load the record into a user defined type because the file is variable length.   I read the 1st 2 char of the string and then I enough what type of record I am getting.   I So I want to move the string I  just read to a User defined type, of course you get "Type mismatch" error.  Is there a way around this without doing
gJournalIn.field1 = Left(Lsrecord,2)
gJournalIn.field2 =Mid(Lsrecord,3, 42)
gJournalIn.field2 =Mid(Lsrecord,44, 1).....


All my fields are strings and all have the length defined.  Any ideas?  I am using vb 5 but if vb 6 can do this I would change is a heartbeat.


Type InputRec
    RecType as String * 2
    Doc1Stuff As String * 42
    SpecialHandle As String * 1
    Pipe1 As String * 1
    Name As String * 40
    Pipe2 As String * 1
    Street As String * 40
    Pipe3 As String * 1
    City As String * 30
    Pipe4 As String * 1
    Province As String * 2
    Pipe5 As String * 1
    PostalCode As String * 7
    Pipe6 As String * 1
    WalkCode As String * 7
    Pipe7 As String * 1
    EndofRec As String * 2
End Type

Dim gJournalIn As InputRec
Sub Main()
Dim lsRecordType As String * 2
Dim lstemp As String
llRecordCount = 0

Open "D:\Projects\PostalSort\Centra\CentraBill1.txt" For Input As #1

Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, lstemp ' Read line into variable.
    llRecordCount = llRecordCount + 1
    lsRecordType = Left(LsRecord, 2)
 
   Select Case lsRecordType
    Case "05"
        gJournalIn = LsRecord
    Case "25"
        UserType2 = LsRecord
   ...
    End Select
   
Loop

Close #1

End Sub
LVL 1
jdesharnaisAsked:
Who is Participating?
 
Brendt HessConnect With a Mentor Senior DBACommented:
Yes - you can use the LSet function and a cheat - define your input buffer as a user defined type.

Normally, you can use LSet from one user defined variable to another, or one string to another.  However, you can't go from String to User-Defined type or vice versa.

However, if you define an input buffer type, you can force the program to do what you need it to do.

NOTE:  If your data is not the correct length, you can end up with very odd results.  You should probably put in error checking to confirm length!

Here's your code, modified:


Type InputRec
    RecType as String * 2
    Doc1Stuff As String * 42
    SpecialHandle As String * 1
    Pipe1 As String * 1
    Name As String * 40
    Pipe2 As String * 1
    Street As String * 40
    Pipe3 As String * 1
    City As String * 30
    Pipe4 As String * 1
    Province As String * 2
    Pipe5 As String * 1
    PostalCode As String * 7
    Pipe6 As String * 1
    WalkCode As String * 7
    Pipe7 As String * 1
    EndofRec As String * 2
End Type

Type InputBuf
   sVal as String
End Type

Dim gJournalIn As InputRec
 
Sub Main()
Dim lsRecordType As String * 2
Dim lstemp As InputBuf

llRecordCount = 0

Open "D:\Projects\PostalSort\Centra\CentraBill1.txt" For Input As #1

Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, lstemp.sVal ' Read line into variable.
    llRecordCount = llRecordCount + 1
    lsRecordType = Left(lstemp.sVal, 2)
 
   Select Case lsRecordType
    Case "05"
        gJournalIn = LSet(lsTemp)
    Case "25"
        UserType2 = LSet(lsTemp)
   ...
    End Select
     
Loop

Close #1

End Sub
--------------------
0
 
FoyalCommented:
Type InputRec
    RecType as String * 2
    Doc1Stuff As String * 42
    SpecialHandle As String * 1
    Pipe1 As String * 1
    Name As String * 40
    Pipe2 As String * 1
    Street As String * 40
    Pipe3 As String * 1
    City As String * 30
    Pipe4 As String * 1
    Province As String * 2
    Pipe5 As String * 1
    PostalCode As String * 7
    Pipe6 As String * 1
    WalkCode As String * 7
    Pipe7 As String * 1
    EndofRec As String * 2
End Type

Dim gJournalIn As InputRec
Sub Main()
Dim lsRecordType As String * 2
Dim lstemp As String
llRecordCount = 0

Open "D:\Projects\PostalSort\Centra\CentraBill1.txt" For Input As #1

Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, lstemp ' Read line into variable.
    llRecordCount = llRecordCount + 1

'-----------------------------------
'    lsRecordType = Left(LsRecord, 2)
'-----------------------------------  
     
      lsRecordType = Left(lstemp, 2)
   

   Select Case lsRecordType
    Case "05"
        '-------------------------
        'gJournalIn = LsRecord
        '-------------------------
       
        With gJournalIn
            .RecType = Mid(lstemp,1,2)
            .Doc1Stuff = Mid(lstemp,3,42)
            'etc.


        End With        

    Case "25"
        '---------------------
        'UserType2 = LsRecord
        '---------------------
        With UserType2
           'etc.,
           'etc.

        End With
   ...
    End Select
     
Loop

Close #1

End Sub

You guessed it. That's the way.
See 'ya,
Foyal ;>)
0
 
Erick37Commented:
You can use CopyMemory to copy the string into a UDT.  Simple example:

Option Explicit

'Any UDT
Private Type sTYPE
    a As String * 5
    b As String * 5
    c As String * 2
End Type

Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
    (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Sub Command1_Click()
    Dim sBuffer As String
    Dim sUDT As sTYPE
   
    sBuffer = "123456789012"
    If Len(sBuffer) <> Len(sUDT) Then
        MsgBox "Lenght Mismatch"
        Exit Sub
    End If
    'Transfer sBuffer into sUDT
    Call CopyMemory(sUDT, ByVal sBuffer, LenB(sBuffer))
    Debug.Print sUDT.a '12345
    Debug.Print sUDT.b '67890
    Debug.Print sUDT.c '12
End Sub
0
 
jdesharnaisAuthor Commented:
Thanks bhess1

This is what I wanted. I have tried it out and it works great.
 I tried to answer yesterday but since EE was moving thier databases around, I could not get the question.

I still think Microsoft should let you go UDT to a string with lset.  It is kind of stupid to have to create a UDT with only a variable.  

JDesharnais
0
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.