Solved

Moving a string into a User defined type

Posted on 2000-03-28
4
189 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

707 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

13 Experts available now in Live!

Get 1:1 Help Now