Solved

Moving a string into a User defined type

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 119
How to produce a SHA-1 hash function in vb6 in order to save it to a table 8 42
Zip Folders Using Chilkat Routines 1 36
Excel Automation VBA 19 68
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

785 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