Solved

sscanf function in VB?

Posted on 1998-05-18
8
1,731 Views
Last Modified: 2011-03-21
Is there a function in VB that is simmilar to the C function sscanf?
0
Comment
Question by:hubermar
  • 5
  • 2
8 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 1452832
There is no directly corresponding function but you can manipulate a string and convert text to numbers. If your string was of the form "FRED 1234 XYZ". you could write a sub such as

sub(text1 as string,numeric1 as double,text2 as string,target as string)
dim i as integer
dim length as integer
dim text3 as string

i = 1
length = len(target)

do until i > length or target(i) = " "
    text1 = text1 + target(i)
loop

i = i + 1

do until i > length or target(i) = " "
    text3 = text3 + target(i)
loop
numeric1 = val(text3)

i = i+1
     
do until i > length or target(i) = " "
    text2 = text2 + target(i)
loop
 
end sub
     
You'd have to tailor individual functions for individual circumstances - You could probably use variants for the variables and then select appropriate translation by 'chopping up'  a format string in the form "%d %s %d" etc

0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1452833
No huberma, there is no function in VB is similar to sscanf, and I didn't find any API function equivalent to it, but you can use the normal string manipulation functions instead (like InStr, Mid, Left...).

Bin Huwairib
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452834
CORRECTION TO STRING MANIPULATION

    sub(text1 as string,numeric1 as double,text2 as string,target as string)
    dim i as integer
    dim length as integer
    dim text3 as string

    i = 1
    length = len(target)

    do until i > length or mid(target,i,1) = " "
        text1 = text1 + mid(target,i,1)
    loop

    i = i + 1

    do until i > length or mid(target,i,1) = " "
        text3 = text3 + mid(target,i,1)
    loop
    numeric1 = val(text3)

    i = i+1
         
    do until i > length or mid(target,i,1) = " "
        text2 = text2 + mid(target,i,1)
    loop
     
    end sub
0
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.

 

Author Comment

by:hubermar
ID: 1452835
deighton,

That's the method that I'm currently using.  It's a real pain.  I appreciate your suggestion.

Thanks,
Mark
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452836
What are you trying to do exactly?  I can think of ways of improving my function.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452837
'Here's a more complcated example - hope it helps


Sub sscanf(ByVal sBuff As String, ByVal sFormat As String, Optional a, Optional b, Optional c, Optional d)

'Attempt to bring Sscanf function into VB
'uses optional variant parameters
'MAX four parameters in scanf string - but this COULD BE INCREASED

'EXAMPLE sscanf("1234 1.2 6","%i %d %i",a,b,c)
' puts 1234 in a (a is integer)
'        1.2 in b (d is a double)
'       6   in c (c is an integer)

Dim sTemp As String
Dim sID As String
Dim i As Integer
Dim i2 As Integer

'Extract paramater a

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, a)

If IsMissing(b) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter b

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, b)

If IsMissing(c) Then Exit Sub

'You would repeat the sections to add new parameters e,f,g,... and put them in the parameter list


sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter c

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, c)

If IsMissing(d) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sID = Right(sFormat, Len(sFormat) - i2 - 1)

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, d)

End Sub

Sub strip(sBuff As String, iReturn As Integer, sStrip As String)
    'Strip the next value or control code out of the string

    Dim i As Integer

    i = 1
    Do Until i > Len(sBuff) Or Mid(sBuff, i, 1) = " "
        i = i + 1
    Loop
   
    sStrip = Left(sBuff, i - 1)
    iReturn = i - 1

End Sub


Sub value(sExtract As String, sFormat As String, vResult)
   
    If sFormat = "%i" Then
        vResult = Int(Val(sExtract))
    ElseIf sFormat = "%s" Then
        vResult = sExtract
    ElseIf sFormat = "%d" Then
        vResult = Val(sExtract)
    End If
 
End Sub


'EXAMPLE

Private Sub Command1_Click()

    'example function call

    Dim i As Integer
    Dim s As String
    Dim d As Single
    Dim d2 As Double
    Call sscanf("1234 FRED 1592654 1", "%i %s %d %d", i, s, d, d2)
    MsgBox i
    MsgBox s
    MsgBox d
    MsgBox d2
End Sub

0
 

Author Comment

by:hubermar
ID: 1452838
deighton,

Very impressive.  I thank you for your help.  I will include this in my code.  I wasn't expecting so much help.  You went the extra mile to answer my question.  

Is it permissible to notify you when I ask questions in the future?  

Thanks again,
Mark
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452839
E-Mail

DEIGHA@GUSCO.COM
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

805 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