Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Moving a string into a User defined type

Posted on 2000-03-28
4
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:
Brendt Hess earned 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

618 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