Solved

Moving a string into a User defined type

Posted on 2000-03-28
4
190 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:jdesharnais
4 Comments
 
LVL 3

Expert Comment

by:Foyal
ID: 2664994
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
 
LVL 32

Accepted Solution

by:
bhess1 earned 100 total points
ID: 2665010
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2665052
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
 
LVL 1

Author Comment

by:jdesharnais
ID: 2670552
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now