Solved

Code for this

Posted on 2004-09-07
6
186 Views
Last Modified: 2010-05-02
Hi,

Could some one help me in coding here. I am not very good at these stuff. So anyone could help.
I tried, but I am getting either a wrong value or a zero.

strData = "Location1|22.2|Location2|22.3|Location3|22.4|Location4|22.5"

'This is a string that I retrieve from a url. The string is always the same except for the values and name of location.
'The separator is always "|" character.
'The "Location1|22.2" is one data. And "Location2|22.3" is the second data. Like that four data.

'What I want to do is, I want to write a function in which when I call to this function and pass the location (1, 2, 3 or 4)
'I want to get the respetive value for that location.

Function Get_Value(Location as Integer)

'I want to know the code here...

End Function
0
Comment
Question by:warheat001
[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
6 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 30 total points
ID: 11997889
Hi warheat001,

Fairly straightforward if you are using VB6:

Get_Value = Split(MyURLString,"|")((Location * 2) -1)

Is the line of code you need.

Tim Cottee
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 

Author Comment

by:warheat001
ID: 11997954
Hey,

That is so cooool...
I never knew that there was a function called SPLIT.

Thanx TimCottee
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11997976
Option Explicit

Private Sub Command1_Click()
    Dim strData As String
    Dim dblData As Double
   
    strData = "Location1|22.2|Location2|22.3|Location3|22.4|Location4|22.5"
    dblData = Get_Value(strData, 4)
   
    MsgBox dblData
End Sub

Private Function Get_Value(ByVal strData As String, ByVal Location As Integer) As Double
    On Error GoTo badConversion
   
    Dim values As Variant
    Dim index As Integer
   
    index = (Location * 2) - 1
    values = Split(strData, "|")
   
    If index >= LBound(values) And index <= UBound(values) Then
        Get_Value = CDbl(values(index))
    Else
        MsgBox "Location: " & Location & vbCrLf & vbCrLf & strData, vbCritical, "Invalid Location for Data"
    End If
    Exit Function

badConversion:
    MsgBox "Value: " & values(index), vbCritical, "Unable to convert value to Double"
End Function
0
Industry Leaders: 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!

 
LVL 15

Expert Comment

by:unknown_routine
ID: 11997993
Private Function GetLocation(ByVal location As Integer, strdata As String) As String
Dim arr

arr = Split(strdata, "|")

 GetLocation = arr(location + 1)

End Function



As an example:

Dim result

strdata = "Location1|22.2|Location2|22.3|Location3|22.4|Location4|22.5"
result = GetLocation(2, strdata)

result now contains 22.3


Regards.

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11998003
Lol...guess I'm a little sloooooow today.

=)
Idle_Mind
0
 
LVL 1

Expert Comment

by:za3_za3
ID: 11998621
it may b helpful 2 visit msdn website 4 getting up2date with all the functions in vb
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

752 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