Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save integer within a string ?

Status
Not open for further replies.

DickDavies

IS-IT--Management
Jan 4, 2002
22
0
0
GB
Hi,

Is it possible to embed an integer within a string (save and retrieve from a data record) without altering the record definition? - i.e. divide a string field into sub fields from within my program.

I can't use Cstr as I only have 2 bytes available.
e.g. MyStr = "abcdefghijk" + <2 byte integer here> + "lmn..."

Thanks in advance
 
You could (if that's the only way you make it work for the app - 'course this hack will be a maintenance nightmare down the line! :)) by using the chr function to return a character for each byte, something like:

MyStr = "abcdefghijk" + chr(<high byte>) + chr(<low byte>) + "lmn..."

assuming you can figure out how to get the individual bytes from the integer. I've seen examples on this site using user defined types - try searching for them.


"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Hmmm...
I wonder what happens if the low or high byte is a zero character?

Might it be possible to have add and link to a database of your own.
You can link on the primary key of the original table, and hold the values in your table instead of theirs..

eg: theirtable holds 9876,abcdefghijklmn
yourtable holds 9876,<the integer value>

and you return data by creating a view across the two tables.
 
Jeff,
Did you actually try adding Chr(0) into the middle of a string?
The answer may surprise you!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Here's an example of a potential solution using UDTs, as referenced by ArtieChoke:
Code:
[COLOR=blue]Option Explicit

' Just an example. You record may have a different layout and length
Private Type proxieRecordUDT
    FirstString As String * 7 ' 7 unicode chars
    myInteger As Integer ' 2 bytes = 1 unicode char
    SecondString As String * 7 ' 7 unicode chars
    ' total= 15 unicode chars
End Type

Private Type realRecordUDT
    MainString As String * 15 ' 15 unicode chars
End Type

Private Sub Command1_Click()

    Dim wombat As proxieRecordUDT
    Dim gopher As realRecordUDT

    wombat.FirstString = "Testing"
    wombat.myInteger = 5
    wombat.SecondString = "Example"
    
    LSet gopher = wombat
    
    'At this point gopher.MainString contains the string you are interested in
    

End Sub
[/color]
 
Thanks very much for the suggestions. I will 'give it a go' using the UDT's.

Dick D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top